Skip to content

Commit

Permalink
Make blendfile.py reading/parsing slightly more robust/helpful in bro…
Browse files Browse the repository at this point in the history
…ken .blend files cases.
  • Loading branch information
Bastien Montagne committed Sep 26, 2016
1 parent 434fa71 commit c528bf7
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion io_blend_utils/blend/blendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def __init__(self, handle):
self.block_header_struct = self.header.create_block_header_struct()
self.blocks = []
self.code_index = {}
self.structs = []
self.sdna_index_from_id = {}

block = BlendFileBlock(handle, self)
while block.code != b'ENDB':
Expand All @@ -140,6 +142,9 @@ def __init__(self, handle):
self.is_modified = False
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?)
self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'}

Expand Down Expand Up @@ -311,12 +316,21 @@ def __init__(self, handle, bfile):
self.user_data = None

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
# 8: old blend files ENDB block (exception)
# 20: normal headers 32 bit platform
# 24: normal headers 64 bit platform
if len(data) > 15:

blockheader = bfile.block_header_struct.unpack(data)
self.code = blockheader[0].partition(b'\0')[0]
if self.code != b'ENDB':
Expand Down

0 comments on commit c528bf7

Please sign in to comment.