This repository has been archived on 2023-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-asset-manager/blendfile.py

660 lines
20 KiB
Python
Raw Normal View History

2014-10-14 09:02:39 +02:00
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
#
# (c) 2009, At Mind B.V. - Jeroen Bakker
2014-10-14 09:03:23 +02:00
# 06-10-2009:
2014-10-14 09:02:39 +02:00
# jbakker - adding support for python 3.0
# 26-10-2009:
# jbakker - adding caching of the SDNA records.
# jbakker - adding caching for file block lookup
# jbakker - increased performance for readstring
# 27-10-2009:
# jbakker - remove FileBlockHeader class (reducing memory print,
# increasing performance)
# 28-10-2009:
# jbakker - reduce far-calls by joining setfield with encode and
# getfield with decode
# 02-11-2009:
# jbakker - python 3 compatibility added
######################################################
# Importing modules
######################################################
import os
import struct
import logging
import gzip
import tempfile
import sys
log = logging.getLogger("blendfile")
2014-10-14 09:34:09 +02:00
FILE_BUFFER_SIZE = 1024 * 1024
2014-10-14 09:02:39 +02:00
######################################################
# module global routines
######################################################
# read routines
# open a filename
# determine if the file is compressed
# and returns a handle
2014-10-14 11:42:54 +02:00
def open_blend(filename, access="rb"):
2014-10-14 09:02:39 +02:00
"""Opens a blend file for reading or writing pending on the access
supports 2 kind of blend files. Uncompressed and compressed.
Known issue: does not support packaged blend files
"""
handle = open(filename, access)
2014-10-14 11:42:54 +02:00
magic = read_string(handle, 7)
2014-10-14 09:02:39 +02:00
if magic == "BLENDER":
log.debug("normal blendfile detected")
handle.seek(0, os.SEEK_SET)
res = BlendFile(handle)
res.is_compressed = False
res.filepath_orig = filename
2014-10-14 09:02:39 +02:00
return res
else:
log.debug("gzip blendfile detected?")
handle.close()
log.debug("decompressing started")
fs = gzip.open(filename, "rb")
handle = tempfile.TemporaryFile()
2014-10-14 09:03:23 +02:00
data = fs.read(FILE_BUFFER_SIZE)
while data:
handle.write(data)
data = fs.read(FILE_BUFFER_SIZE)
2014-10-14 09:02:39 +02:00
log.debug("decompressing finished")
fs.close()
log.debug("resetting decompressed file")
handle.seek(os.SEEK_SET, 0)
res = BlendFile(handle)
res.is_compressed = True
res.filepath_orig = filename
2014-10-14 09:02:39 +02:00
return res
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
######################################################
# Write a string to the file.
######################################################
2014-10-14 11:42:54 +02:00
def write_string(handle, astring, fieldlen):
2014-10-14 11:21:30 +02:00
assert(isinstance(astring, str))
2014-10-14 09:34:09 +02:00
stringw = ""
2014-10-14 09:02:39 +02:00
if len(astring) >= fieldlen:
2014-10-14 09:34:09 +02:00
stringw = astring[0:fieldlen]
2014-10-14 09:02:39 +02:00
else:
2014-10-14 09:34:09 +02:00
stringw = astring + '\0'
2014-10-14 11:21:30 +02:00
handle.write(stringw.encode('utf-8'))
2014-10-14 11:42:54 +02:00
def write_bytes(handle, astring, fieldlen):
2014-10-14 11:21:30 +02:00
assert(isinstance(astring, (bytes, bytearray)))
stringw = b''
if len(astring) >= fieldlen:
stringw = astring[0:fieldlen]
else:
stringw = astring + b'\0'
print(stringw)
print(handle)
handle.write(stringw)
2014-10-14 09:02:39 +02:00
2014-10-14 11:42:54 +02:00
def _string_struct(length):
if length < len(_string_struct.STRING):
st = _string_struct.STRING[length]
else:
st = struct.Struct("%ds" % length)
return st
_string_struct.STRING = [struct.Struct("%ds" % i) for i in range(0, 2048)]
2014-10-14 09:03:23 +02:00
2014-10-14 09:02:39 +02:00
2014-10-14 11:42:54 +02:00
def read_string(handle, length):
"""
read_string reads a String of given length from a file handle
"""
st = _string_struct(length)
return st.unpack(handle.read(st.size))[0].decode('utf-8')
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
2014-10-14 11:42:54 +02:00
def read_string0(data, offset):
"""
read_string0 reads a zero terminating String from a file handle
"""
2014-10-14 09:02:39 +02:00
add = 0
2014-10-14 11:42:54 +02:00
# TODO, faster method!
while data[offset + add] != 0:
2014-10-14 09:34:09 +02:00
add += 1
2014-10-14 11:42:54 +02:00
st = _string_struct(add)
result = st.unpack_from(data, offset)[0].decode('utf-8')
return result
2014-10-14 09:02:39 +02:00
######################################################
# ReadUShort reads an unsigned short from a file handle
######################################################
2014-10-14 09:34:09 +02:00
USHORT = [struct.Struct("<H"), struct.Struct(">H")]
2014-10-14 09:02:39 +02:00
def ReadUShort(handle, fileheader):
us = USHORT[fileheader.endian_index]
2014-10-14 09:02:39 +02:00
return us.unpack(handle.read(us.size))[0]
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
######################################################
# ReadUInt reads an unsigned integer from a file handle
######################################################
2014-10-14 09:34:09 +02:00
UINT = [struct.Struct("<I"), struct.Struct(">I")]
2014-10-14 09:02:39 +02:00
def ReadUInt(handle, fileheader):
us = UINT[fileheader.endian_index]
2014-10-14 09:02:39 +02:00
return us.unpack(handle.read(us.size))[0]
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
def ReadInt(handle, fileheader):
return struct.unpack(fileheader.endian_str + "i", handle.read(4))[0]
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
def ReadFloat(handle, fileheader):
return struct.unpack(fileheader.endian_str + "f", handle.read(4))[0]
2014-10-14 09:02:39 +02:00
2014-10-14 09:34:09 +02:00
SSHORT = [struct.Struct("<h"), struct.Struct(">h")]
2014-10-14 09:02:39 +02:00
def ReadShort(handle, fileheader):
us = SSHORT[fileheader.endian_index]
2014-10-14 09:02:39 +02:00
return us.unpack(handle.read(us.size))[0]
2014-10-14 09:34:09 +02:00
ULONG = [struct.Struct("<Q"), struct.Struct(">Q")]
2014-10-14 09:02:39 +02:00
def ReadULong(handle, fileheader):
us = ULONG[fileheader.endian_index]
2014-10-14 09:02:39 +02:00
return us.unpack(handle.read(us.size))[0]
######################################################
# ReadPointer reads an pointerfrom a file handle
# the pointersize is given by the header (BlendFileHeader)
######################################################
def ReadPointer(handle, header):
if header.pointer_size == 4:
us = UINT[header.endian_index]
2014-10-14 09:02:39 +02:00
return us.unpack(handle.read(us.size))[0]
if header.pointer_size == 8:
us = ULONG[header.endian_index]
2014-10-14 09:02:39 +02:00
return us.unpack(handle.read(us.size))[0]
2014-10-14 09:03:23 +02:00
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
######################################################
# Align alligns the filehandle on 4 bytes
2014-10-14 09:02:39 +02:00
######################################################
def align(offset, by):
n = by - 1
return (offset + n) & ~n
2014-10-14 09:02:39 +02:00
######################################################
# module classes
######################################################
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
class BlendFile:
"""
Blend file.
"""
__slots__ = (
# file (result of open())
"handle",
# str (original name of the file path)
"filepath_orig",
# BlendFileHeader
"header",
# struct.Struct
"block_header_struct",
# FileBlockHeader
"blocks",
# DNACatalog
"catalog",
# int
"code_index",
# bool (did we make a change)
"is_modified",
# bool (is file gzipped)
"is_compressed",
)
2014-10-14 09:03:23 +02:00
2014-10-14 09:02:39 +02:00
def __init__(self, handle):
log.debug("initializing reading blend-file")
2014-10-14 09:34:09 +02:00
self.handle = handle
self.header = BlendFileHeader(handle)
self.block_header_struct = self.header.create_block_header_struct()
self.blocks = []
self.code_index = {}
block = BlendFileBlock(handle, self)
while block.code != "ENDB":
if block.code == "DNA1":
self.catalog = DNACatalog(self.header, block, handle)
2014-10-14 09:02:39 +02:00
else:
handle.seek(block.size, os.SEEK_CUR)
2014-10-14 09:03:23 +02:00
self.blocks.append(block)
self.code_index.setdefault(block.code, []).append(block)
2014-10-14 09:03:23 +02:00
block = BlendFileBlock(handle, self)
self.is_modified = False
self.blocks.append(block)
2014-10-14 09:03:23 +02:00
def find_blocks_from_code(self, code):
2014-10-14 09:02:39 +02:00
if len(code) == 2:
code = code
if code not in self.code_index:
2014-10-14 09:02:39 +02:00
return []
return self.code_index[code]
2014-10-14 09:03:23 +02:00
def find_block_from_offset(self, offset):
for block in self.blocks:
if block.addr_old == offset:
2014-10-14 09:34:09 +02:00
return block
return None
2014-10-14 09:03:23 +02:00
2014-10-14 09:02:39 +02:00
def close(self):
2014-10-14 11:42:54 +02:00
"""
Close the blend file
writes the blend file to disk if changes has happened
"""
if not self.is_modified:
2014-10-14 09:02:39 +02:00
self.handle.close()
else:
2014-10-14 11:42:54 +02:00
handle = self.handle
if self.is_compressed:
log.debug("close compressed blend file")
handle.seek(os.SEEK_SET, 0)
log.debug("compressing started")
fs = gzip.open(self.filepath_orig, "wb")
data = handle.read(FILE_BUFFER_SIZE)
while data:
fs.write(data)
data = handle.read(FILE_BUFFER_SIZE)
fs.close()
log.debug("compressing finished")
handle.close()
2014-10-14 09:03:23 +02:00
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
class BlendFileBlock:
"""
Instance of a struct.
"""
__slots__ = (
# file handle
"file",
"code",
"size",
"addr_old",
"sdna_index",
"count",
"file_offset",
)
2014-10-14 09:02:39 +02:00
def __init__(self, handle, afile):
self.file = afile
header = afile.header
2014-10-14 09:02:39 +02:00
data = handle.read(afile.block_header_struct.size)
2014-10-14 09:34:09 +02:00
# header size can be 8, 20, or 24 bytes long
# 8: old blend files ENDB block (exception)
# 20: normal headers 32 bit platform
# 24: normal headers 64 bit platform
if len(data) > 15:
blockheader = afile.block_header_struct.unpack(data)
self.code = blockheader[0].decode().split("\0")[0]
if self.code != "ENDB":
self.size = blockheader[1]
self.addr_old = blockheader[2]
self.sdna_index = blockheader[3]
self.count = blockheader[4]
self.file_offset = handle.tell()
2014-10-14 09:02:39 +02:00
else:
self.size = 0
self.addr_old = 0
self.sdna_index = 0
self.count = 0
self.file_offset = 0
2014-10-14 09:02:39 +02:00
else:
blockheader = OLDBLOCK.unpack(data)
self.code = blockheader[0].decode().split("\0")[0]
self.size = 0
self.addr_old = 0
self.sdna_index = 0
self.count = 0
self.file_offset = 0
def get(self, path):
dna_index = self.sdna_index
dna_struct = self.file.catalog.structs[dna_index]
self.file.handle.seek(self.file_offset, os.SEEK_SET)
return dna_struct.field_get(self.file.header, self.file.handle, path)
def set(self, path, value):
dna_index = self.sdna_index
dna_struct = self.file.catalog.structs[dna_index]
self.file.handle.seek(self.file_offset, os.SEEK_SET)
self.file.is_modified = True
return dna_struct.field_set(self.file.header, self.file.handle, path, value)
######################################################
# magic = str
# pointer_size = int
# is_little_endian = bool
# version = int
2014-10-14 09:02:39 +02:00
######################################################
2014-10-14 09:34:09 +02:00
BLOCKHEADERSTRUCT = {}
2014-10-14 09:02:39 +02:00
BLOCKHEADERSTRUCT["<4"] = struct.Struct("<4sIIII")
BLOCKHEADERSTRUCT[">4"] = struct.Struct(">4sIIII")
BLOCKHEADERSTRUCT["<8"] = struct.Struct("<4sIQII")
BLOCKHEADERSTRUCT[">8"] = struct.Struct(">4sIQII")
FILEHEADER = struct.Struct("7s1s1s3s")
2014-10-14 09:34:09 +02:00
OLDBLOCK = struct.Struct("4sI")
2014-10-14 09:02:39 +02:00
class BlendFileHeader:
"""
BlendFileHeader allocates the first 12 bytes of a blend file
it contains information about the hardware architecture
"""
__slots__ = (
# str
"magic",
# int 4/8
"pointer_size",
# bool
"is_little_endian",
# int
"version",
# str, used to pass to 'struct'
"endian_str",
# int, used to index common types
"endian_index",
)
2014-10-14 09:02:39 +02:00
def __init__(self, handle):
log.debug("reading blend-file-header")
values = FILEHEADER.unpack(handle.read(FILEHEADER.size))
self.magic = values[0]
pointer_size_id = values[1].decode()
if pointer_size_id == "-":
self.pointer_size = 8
elif pointer_size_id == "_":
self.pointer_size = 4
else:
assert(0)
endian_id = values[2].decode()
if endian_id == "v":
self.is_little_endian = True
self.endian_str = "<"
self.endian_index = 0
elif endian_id == "V":
self.is_little_endian = False
self.endian_index = 1
self.endian_str = ">"
else:
assert(0)
2014-10-14 09:02:39 +02:00
tVersion = values[3].decode()
self.version = int(tVersion)
2014-10-14 09:03:23 +02:00
def create_block_header_struct(self):
return BLOCKHEADERSTRUCT[self.endian_str + str(self.pointer_size)]
2014-10-14 09:03:23 +02:00
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
class DNACatalog:
"""
DNACatalog is a catalog of all information in the DNA1 file-block
"""
__slots__ = (
"header",
"names",
"types",
"structs",
)
2014-10-14 09:02:39 +02:00
def __init__(self, header, block, handle):
log.debug("building DNA catalog")
shortstruct = USHORT[header.endian_index]
shortstruct2 = struct.Struct(str(USHORT[header.endian_index].format.decode() + 'H'))
intstruct = UINT[header.endian_index]
data = handle.read(block.size)
self.names = []
self.types = []
self.structs = []
2014-10-14 09:03:23 +02:00
2014-10-14 09:34:09 +02:00
offset = 8
names_len = intstruct.unpack_from(data, offset)[0]
2014-10-14 09:02:39 +02:00
offset += 4
2014-10-14 09:03:23 +02:00
log.debug("building #%d names" % names_len)
for i in range(names_len):
2014-10-14 11:42:54 +02:00
tName = read_string0(data, offset)
2014-10-14 09:02:39 +02:00
offset = offset + len(tName) + 1
self.names.append(DNAName(tName))
del names_len
2014-10-14 09:02:39 +02:00
offset = align(offset, 4)
2014-10-14 09:02:39 +02:00
offset += 4
types_len = intstruct.unpack_from(data, offset)[0]
2014-10-14 09:02:39 +02:00
offset += 4
log.debug("building #"+str(types_len)+" types")
for i in range(types_len):
2014-10-14 11:42:54 +02:00
tType = read_string0(data, offset)
self.types.append([tType, 0, None])
offset += len(tType) + 1
2014-10-14 09:02:39 +02:00
offset = align(offset, 4)
2014-10-14 09:02:39 +02:00
offset += 4
log.debug("building #%d type-lengths" % types_len)
for i in range(types_len):
2014-10-14 09:02:39 +02:00
tLen = shortstruct.unpack_from(data, offset)[0]
offset = offset + 2
self.types[i][1] = tLen
del types_len
2014-10-14 09:02:39 +02:00
offset = align(offset, 4)
2014-10-14 09:02:39 +02:00
offset += 4
2014-10-14 09:03:23 +02:00
structs_len = intstruct.unpack_from(data, offset)[0]
2014-10-14 09:02:39 +02:00
offset += 4
log.debug("building #%d structures" % structs_len)
for struct_index in range(structs_len):
2014-10-14 09:02:39 +02:00
d = shortstruct2.unpack_from(data, offset)
struct_type_index = d[0]
2014-10-14 09:02:39 +02:00
offset += 4
dna_type = self.types[struct_type_index]
structure = DNAStructure(dna_type)
self.structs.append(structure)
2014-10-14 09:02:39 +02:00
fields_len = d[1]
2014-10-14 09:02:39 +02:00
for field_index in range(fields_len):
2014-10-14 09:02:39 +02:00
d2 = shortstruct2.unpack_from(data, offset)
field_type_index = d2[0]
field_name_index = d2[1]
2014-10-14 09:02:39 +02:00
offset += 4
fType = self.types[field_type_index]
fName = self.names[field_name_index]
if fName.is_pointer or fName.is_method_pointer:
fsize = header.pointer_size * fName.array_size
2014-10-14 09:02:39 +02:00
else:
fsize = fType[1] * fName.array_size
structure.fields.append([fType, fName, fsize])
2014-10-14 09:02:39 +02:00
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
class DNAName:
"""
DNAName is a C-type name stored in the DNA
"""
__slots__ = (
"name",
"name_short",
"is_pointer",
"is_method_pointer",
"array_size",
"_SN", # TODO, investigate why this is needed!
)
2014-10-14 09:02:39 +02:00
def __init__(self, aName):
self.name = aName
self.name_short = self.calc_name_short()
self.is_pointer = self.calc_is_pointer()
self.is_method_pointer = self.calc_is_method_pointer()
self.array_size = self.calc_array_size()
2014-10-14 09:03:23 +02:00
def as_reference(self, parent):
2014-10-14 09:34:09 +02:00
if parent is None:
result = ""
2014-10-14 09:02:39 +02:00
else:
result = parent + "."
2014-10-14 09:03:23 +02:00
result = result + self.name_short
return result
2014-10-14 09:02:39 +02:00
def calc_name_short(self):
result = self.name
result = result.replace("*", "")
result = result.replace("(", "")
result = result.replace(")", "")
index = result.find("[")
if index != -1:
result = result[:index]
self._SN = result
return result
2014-10-14 09:03:23 +02:00
def calc_is_pointer(self):
return self.name.find("*") > -1
2014-10-14 09:02:39 +02:00
def calc_is_method_pointer(self):
return self.name.find("(*") > -1
2014-10-14 09:02:39 +02:00
def calc_array_size(self):
result = 1
temp = self.name
index = temp.find("[")
2014-10-14 09:02:39 +02:00
while index != -1:
index_2 = temp.find("]")
result *= int(temp[index + 1:index_2])
temp = temp[index_2 + 1:]
index = temp.find("[")
2014-10-14 09:03:23 +02:00
return result
2014-10-14 09:02:39 +02:00
2014-10-14 09:34:09 +02:00
2014-10-14 09:02:39 +02:00
class DNAStructure:
"""
DNAType is a C-type structure stored in the DNA
"""
__slots__ = (
"dna_type",
"fields",
)
2014-10-14 09:02:39 +02:00
def __init__(self, aType):
self.dna_type = aType
2014-10-14 09:02:39 +02:00
aType[2] = self
self.fields = []
2014-10-14 09:03:23 +02:00
def field_get(self, header, handle, path):
2014-10-14 09:02:39 +02:00
splitted = path.partition(".")
name = splitted[0]
rest = splitted[2]
2014-10-14 09:34:09 +02:00
offset = 0
for field in self.fields:
2014-10-14 09:02:39 +02:00
fname = field[1]
if fname.name_short == name:
2014-10-14 09:02:39 +02:00
handle.seek(offset, os.SEEK_CUR)
ftype = field[0]
if len(rest) == 0:
2014-10-14 09:03:23 +02:00
if fname.is_pointer:
2014-10-14 09:02:39 +02:00
return ReadPointer(handle, header)
2014-10-14 09:34:09 +02:00
elif ftype[0] == "int":
2014-10-14 09:02:39 +02:00
return ReadInt(handle, header)
2014-10-14 09:34:09 +02:00
elif ftype[0] == "short":
2014-10-14 09:02:39 +02:00
return ReadShort(handle, header)
2014-10-14 09:34:09 +02:00
elif ftype[0] == "float":
2014-10-14 09:02:39 +02:00
return ReadFloat(handle, header)
2014-10-14 09:34:09 +02:00
elif ftype[0] == "char":
2014-10-14 11:42:54 +02:00
return read_string(handle, fname.array_size)
2014-10-14 09:02:39 +02:00
else:
return ftype[2].field_get(header, handle, rest)
2014-10-14 09:03:23 +02:00
2014-10-14 09:02:39 +02:00
else:
offset += field[2]
return None
2014-10-14 09:03:23 +02:00
def field_set(self, header, handle, path, value):
2014-10-14 09:02:39 +02:00
splitted = path.partition(".")
name = splitted[0]
rest = splitted[2]
2014-10-14 09:34:09 +02:00
offset = 0
for field in self.fields:
2014-10-14 09:02:39 +02:00
fname = field[1]
if fname.name_short == name:
2014-10-14 09:02:39 +02:00
handle.seek(offset, os.SEEK_CUR)
ftype = field[0]
2014-10-14 09:34:09 +02:00
if len(rest) == 0:
if ftype[0] == "char":
2014-10-14 11:21:30 +02:00
if type(value) is str:
2014-10-14 11:42:54 +02:00
return write_string(handle, value, fname.array_size)
2014-10-14 11:21:30 +02:00
else:
2014-10-14 11:42:54 +02:00
return write_bytes(handle, value, fname.array_size)
2014-10-14 09:02:39 +02:00
else:
return ftype[2].field_set(header, handle, rest, value)
2014-10-14 09:02:39 +02:00
else:
offset += field[2]
return None
2014-10-14 09:03:23 +02:00
2014-10-14 09:02:39 +02:00
class DNAField:
"""
DNAField is a coupled DNAType and DNAName
"""
__slots__ = (
"name",
"dna_type",
)
def __init__(self, dna_type, name):
self.dna_type = dna_type
self.name = name
def size(self, header):
if self.name.is_pointer or self.name.is_method_pointer:
return header.pointer_size * self.name.array_size
2014-10-14 09:02:39 +02:00
else:
return self.dna_type.size * self.name.array_size
2014-10-14 09:02:39 +02:00
2014-10-14 09:34:09 +02:00
# determine the relative production location of a blender path.basename
2014-10-14 09:02:39 +02:00
def blendPath2AbsolutePath(productionFile, blenderPath):
2014-10-14 09:34:09 +02:00
productionFileDir = os.path.dirname(productionFile)
2014-10-14 09:02:39 +02:00
if blenderPath.startswith("//"):
2014-10-14 09:34:09 +02:00
relpath = blenderPath[2:]
2014-10-14 09:02:39 +02:00
abspath = os.path.join(productionFileDir, relpath)
return abspath
2014-10-14 09:03:23 +02:00
2014-10-14 09:02:39 +02:00
return blenderPath