Skip to content
Snippets Groups Projects
Commit 4329f903 authored by Campbell Barton's avatar Campbell Barton
Browse files

Use tuples for multiple attribute and array access

Removes string mangling
parent dec03ceb
No related branches found
No related tags found
No related merge requests found
...@@ -168,7 +168,7 @@ def do_bblock_filter(filters, blend, block, meta_keyval, data_keyval): ...@@ -168,7 +168,7 @@ def do_bblock_filter(filters, blend, block, meta_keyval, data_keyval):
if fld is None: if fld is None:
continue continue
if fld.dna_name.is_pointer: if fld.dna_name.is_pointer:
paths = ([fld.dna_name.name_only + b"[" + str(i).encode() + b"]" for i in range(fld.dna_name.array_size)] paths = ([(fld.dna_name.name_only, i) for i in range(fld.dna_name.array_size)]
if fld.dna_name.array_size > 1 else [fld.dna_name.name_only]) if fld.dna_name.array_size > 1 else [fld.dna_name.name_only])
for p in paths: for p in paths:
child_block = block.get_pointer(p) child_block = block.get_pointer(p)
......
...@@ -416,7 +416,13 @@ class BlendFileBlock: ...@@ -416,7 +416,13 @@ class BlendFileBlock:
use_nil=True, use_str=True, use_nil=True, use_str=True,
base_index=0, base_index=0,
): ):
path_full = path_root + b"." + path if path_root else path if path_root:
path_full = (
(path_root if type(path_root) is tuple else (path_root, )) +
(path if type(path) is tuple else (path, )))
else:
path_full = path
try: try:
yield (path_full, self.get(path_full, default, sdna_index_refine, use_nil, use_str, base_index)) yield (path_full, self.get(path_full, default, sdna_index_refine, use_nil, use_str, base_index))
except NotImplementedError as ex: except NotImplementedError as ex:
...@@ -689,18 +695,27 @@ class DNAStruct: ...@@ -689,18 +695,27 @@ class DNAStruct:
self.user_data = None self.user_data = None
def field_from_path(self, header, handle, path): def field_from_path(self, header, handle, path):
assert(type(path) == bytes) """
# support 'id.name' Support lookups as bytes or a tuple of bytes and optional index.
name, _, name_tail = path.partition(b'.')
C style 'id.name' --> (b'id', b'name')
# support 'mtex[1].tex' C style 'array[4]' --> ('array', 4)
# note, multi-dimensional arrays not supported """
# FIXME: 'mtex[1]' works, but not 'mtex[1].tex', why is this??? if type(path) is tuple:
if name.endswith(b']'): name = path[0]
name, _, index = name[:-1].partition(b'[') if len(path) >= 2 and type(path[1]) is not bytes:
index = int(index) name_tail = path[2:]
index = path[1]
assert(type(index) is int)
else: else:
name_tail = path[1:]
index = 0 index = 0
else:
name = path
name_tail = None
index = 0
assert(type(name) is bytes)
field = self.field_from_name.get(name) field = self.field_from_name.get(name)
...@@ -713,7 +728,7 @@ class DNAStruct: ...@@ -713,7 +728,7 @@ class DNAStruct:
index_offset = field.dna_type.size * index index_offset = field.dna_type.size * index
assert(index_offset < field.dna_size) assert(index_offset < field.dna_size)
handle.seek(index_offset, os.SEEK_CUR) handle.seek(index_offset, os.SEEK_CUR)
if name_tail == b'': if not name_tail: # None or ()
return field return field
else: else:
return field.dna_type.field_from_path(header, handle, name_tail) return field.dna_type.field_from_path(header, handle, name_tail)
...@@ -722,8 +737,6 @@ class DNAStruct: ...@@ -722,8 +737,6 @@ class DNAStruct:
default=..., default=...,
use_nil=True, use_str=True, use_nil=True, use_str=True,
): ):
assert(type(path) == bytes)
field = self.field_from_path(header, handle, path) field = self.field_from_path(header, handle, path)
if field is None: if field is None:
if default is not ...: if default is not ...:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment