Checking .blend metadata to see if it was created in 2.8?

How do I know before opening a .blend file which version it was created in?

Or is there metadata available I may access prior to attempting to open a .blend file?

I ask because I need to know which version of Blender I should supply as the path to open a .blend from a subprocess call:

allBlendFiles = glob.glob1(dest_directory, "*.blend") #get all .blends in a dir
blendFile = allBlendFiles[0] #for simplicity, just use the first .blend
src_directory = os.path.join(dest_directory, blendFile)#abs path to the .blend

LEGACY_MODE = blendFile.meta???.version < (2, 80, 0) #HOW CAN ACCESS VERSION OF .blend FILE?

if LEGACY_MODE:
    #Path to Blender 2.79
    blenderRuntimePath = r"C:\Program Files\Blender Foundation\Blender\blender.exe",
else:
    #Path to Blender 2.8
    blenderRuntimePath = r"C:\Program Files\Blender Foundation\Blender28\blender.exe",

subprocess.check_call([
    blenderRuntimePath, #THIS PATH CHANGES BASED ON THE VERSION OF .blend IT IS OPENING
    src_directory,
    r"--background",
    r"--python",
    r"C:\Projects\Automation\BuildCharacter.py"
])

Thanks

Yes, it’s possible, you can do it by hand, or by means of something like file command (common in Unix). Uncompressed files always start with ASCII bytes “BLENDER”, two bytes (32/64 bits and endianess markers), then 3 bytes that store version in ASCII. Examples “BLENDER-v264” or “BLENDER-v280”.

Using file magic tools should make it easier with compressed files (file -z example.blend), otherwise you have to uncompress yourself and examine the result as above. Python’s filemagic wraps libmagic, but quick read of docs makes no mention of compression support.

2 Likes

You are correct, how naive of me not to open one with a .txt editor :sweat_smile:

Anyways, just as you suggest after inspecting with txt editor I see the first few bytes of the file as ASCII:
BLENDER-v279RENDH -or- BLENDER-v280RENDH

Very simple, Thank you!!

1 Like

Beware, REND is really common (chunk headers are 4 bytes, H is “data”), but not always there, I have one example starting as “BLENDER-v279GLOB” and remember seeing “TEST” too. The part you want is just up to the 3 numbers (first 12 bytes).

You may want to check doc/blender_file_format/BlendFileReader.py, BTW. Some parts are what you want, ignore the rest.

1 Like