Make blendfile.py reading/parsing slightly more robust/helpful in broken .blend files cases.
This commit is contained in:
@@ -123,6 +123,8 @@ class BlendFile:
|
|||||||
self.block_header_struct = self.header.create_block_header_struct()
|
self.block_header_struct = self.header.create_block_header_struct()
|
||||||
self.blocks = []
|
self.blocks = []
|
||||||
self.code_index = {}
|
self.code_index = {}
|
||||||
|
self.structs = []
|
||||||
|
self.sdna_index_from_id = {}
|
||||||
|
|
||||||
block = BlendFileBlock(handle, self)
|
block = BlendFileBlock(handle, self)
|
||||||
while block.code != b'ENDB':
|
while block.code != b'ENDB':
|
||||||
@@ -140,6 +142,9 @@ class BlendFile:
|
|||||||
self.is_modified = False
|
self.is_modified = False
|
||||||
self.blocks.append(block)
|
self.blocks.append(block)
|
||||||
|
|
||||||
|
if not self.structs:
|
||||||
|
raise Exception("No DNA1 block in file, this is not a valid .blend file!")
|
||||||
|
|
||||||
# cache (could lazy init, incase we never use?)
|
# cache (could lazy init, incase we never use?)
|
||||||
self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'}
|
self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'}
|
||||||
|
|
||||||
@@ -314,12 +319,21 @@ class BlendFileBlock:
|
|||||||
self.user_data = None
|
self.user_data = None
|
||||||
|
|
||||||
data = handle.read(bfile.block_header_struct.size)
|
data = handle.read(bfile.block_header_struct.size)
|
||||||
|
|
||||||
|
if len(data) != bfile.block_header_struct.size:
|
||||||
|
print("WARNING! Blend file seems to be badly truncated!")
|
||||||
|
self.code = b'ENDB'
|
||||||
|
self.size = 0
|
||||||
|
self.addr_old = 0
|
||||||
|
self.sdna_index = 0
|
||||||
|
self.count = 0
|
||||||
|
self.file_offset = 0
|
||||||
|
return
|
||||||
# header size can be 8, 20, or 24 bytes long
|
# header size can be 8, 20, or 24 bytes long
|
||||||
# 8: old blend files ENDB block (exception)
|
# 8: old blend files ENDB block (exception)
|
||||||
# 20: normal headers 32 bit platform
|
# 20: normal headers 32 bit platform
|
||||||
# 24: normal headers 64 bit platform
|
# 24: normal headers 64 bit platform
|
||||||
if len(data) > 15:
|
if len(data) > 15:
|
||||||
|
|
||||||
blockheader = bfile.block_header_struct.unpack(data)
|
blockheader = bfile.block_header_struct.unpack(data)
|
||||||
self.code = blockheader[0].partition(b'\0')[0]
|
self.code = blockheader[0].partition(b'\0')[0]
|
||||||
if self.code != b'ENDB':
|
if self.code != b'ENDB':
|
||||||
|
Reference in New Issue
Block a user