Added BlendFileBlock.id_name property

Evaluated only once, so safe to call multiple times without producing
excessive disk I/O. Returns None instead of raising KeyError when there is
no (b'id', b'name') field.
This commit is contained in:
Sybren A. Stüvel
2018-03-02 11:26:02 +01:00
parent 2bb9cffa49
commit 7167d51730
4 changed files with 26 additions and 11 deletions
+19 -2
View File
@@ -326,6 +326,7 @@ class BlendFileBlock:
Points to the data after the block header.
"""
self.endian = bfile.header.endian
self._id_name = ... # see the id_name property
header_struct = bfile.block_header_struct
data = bfile.fileobj.read(header_struct.size)
@@ -382,6 +383,20 @@ class BlendFileBlock:
def dna_type_name(self) -> str:
return self.dna_type.dna_type_id.decode('ascii')
@property
def id_name(self) -> typing.Optional[bytes]:
"""Same as block[b'id', b'name']; None if there is no such field.
Evaluated only once, so safe to call multiple times without producing
excessive disk I/O.
"""
if self._id_name is ...:
try:
self._id_name = self[b'id', b'name']
except KeyError:
self._id_name = None
return self._id_name
def refine_type_from_index(self, sdna_index: int):
"""Change the DNA Struct associated with this block.
@@ -543,7 +558,8 @@ class BlendFileBlock:
-> typing.Iterator['BlendFileBlock']:
"""Dereference pointers from an array-of-pointers field.
Use this function when you have a field like materials: `Mat **mat`
Use this function when you have a field like Mesh materials:
`Mat **mat`
:param path: The array-of-pointers field.
:param array_size: Number of items in the array. If None, the
@@ -571,7 +587,8 @@ class BlendFileBlock:
-> typing.Iterator['BlendFileBlock']:
"""Yield blocks from a fixed-size array field.
Use this function when you have a field like lamp textures: `MTex *mtex[18]`
Use this function when you have a field like lamp textures:
`MTex *mtex[18]`
The size of the array is determined automatically by the size in bytes
of the field divided by the pointer size of the blend file.
+1 -3
View File
@@ -5,7 +5,6 @@ From a Blend file data block, iter_assts() yields all the referred-to assets.
import functools
import logging
import sys
import typing
from blender_asset_tracer import blendfile, bpathlib
@@ -117,10 +116,9 @@ def movie_clip(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockU
def object_block(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Object data blocks."""
# 'ob->modifiers[...].filepath'
ob_idname = block[b'id', b'name']
mods = block.get_pointer((b'modifiers', b'first'))
for mod_idx, block_mod in enumerate(iterators.listbase(mods, next_path=(b'modifier', b'next'))):
block_name = b'%s.modifiers[%d]' % (ob_idname, mod_idx)
block_name = b'%s.modifiers[%d]' % (block.id_name, mod_idx)
mod_type = block_mod[b'modifier', b'type']
log.debug('Tracing modifier %s, type=%d', block_name.decode(), mod_type)