Open
Description
The following code could be used to open the .blend file and read the file header in order to determine the Blender version used to save it (as well as bitness and endianess):
import struct
import os.path
filepath = bpy.data.filepath
is_compressed = False
try:
file = open(filepath, "rb")
header = file.read(12)
if header[:2] == b"\x1f\x8b":
file.close()
import gzip
file = gzip.GzipFile(filepath, "rb")
header = file.read(12)
is_compressed = True
if header[:7] != b"BLENDER":
raise Exception("Not a .blend-file!")
bitness, endianess, major, minor = struct.unpack("sss2s", header[7:])
print("v{}.{}".format(major.decode(), minor.decode()))
print("64 bit" if bitness == b"-" else "32 bit")
print("little-endian" if endianess == b"v" else "big-endian")
print("compressed" if is_compressed else "uncompressed")
except IOError:
print("Can't read file '{}'".format(filepath))
except Exception as err:
print(err)
Drawbacks: would make it slightly slower, but reading a couple bytes shouldn't hurt that much. Could be made optional if in doubt. Wouldn't report the sub-version and version char (1
and a
in v2.75.1 / v2.75a).