Frostie
Frostie

Reputation: 77

Python: Open .blend file as text

I'd like to open a .blend file as a text in python, like you would open a .blend file in a text editor. But I only can open it as binary with open(blend, "rb").read(), but then I get encrypted text and it needs very long to load.

How do I get just the text? open(blend, "r").read() doesn't work, because I get this error:

    File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 18726: character maps to <undefined>

Thanks for help!

Upvotes: 0

Views: 2038

Answers (2)

sambler
sambler

Reputation: 7079

While the blend file is a binary data file, you will also find that many of them will also be compressed.

The easiest way would be to let blender read the file. To automate the task you can start blender in background mode from the CLI and have it run a python script using the blender API that extracts the info you want.

blender -b --python extractor.py

If you are using python to do that, you can use subprocess.run().

subprocess.run(['blender', '-b', '--python', 'extractor.py'])

However if you need to do this on a machine without blender installed, it is possible to read a blend file from python without blender, depending on your needs you may find the answers here and here to be helpful.

Upvotes: 2

internet_user
internet_user

Reputation: 3279

Opening with "rb" is correct, and the "encrypted text" is how 3d models are stored in blender. I don't really understand your motive for trying to do this so I can't help further.

More information on error: In the encoding file.read uses, 0x81 is an undefined byte, so it errors when it sees that. However, when opened with "rb", it will just store the values, not try to convert them to characters (not completely accurate, but helps understand).

Upvotes: 0

Related Questions