Struct.field_set - support setting subproperties (#92899)
Extend `Struct.field_set()` so that the `path` type can be a `dna.FieldPath`, to mirror `Struct.field_get`'s `path` type, and allow users to set file-block's subproperties. For example this allows setting `object_block[(b'id', 'name')]`. Also, along the way, added a test for getting subproperty value. Reviewed-on: https://projects.blender.org/blender/blender-asset-tracer/pulls/92899
This commit is contained in:
committed by
Sybren A. Stüvel
parent
c69612b264
commit
47c3e8d77f
@@ -726,7 +726,7 @@ class BlendFileBlock:
|
||||
hsh = zlib.adler32(str(value).encode(), hsh)
|
||||
return hsh
|
||||
|
||||
def set(self, path: bytes, value):
|
||||
def set(self, path: dna.FieldPath, value):
|
||||
dna_struct = self.bfile.structs[self.sdna_index]
|
||||
self.bfile.mark_modified()
|
||||
self.bfile.fileobj.seek(self.file_offset, os.SEEK_SET)
|
||||
@@ -835,7 +835,7 @@ class BlendFileBlock:
|
||||
def __getitem__(self, path: dna.FieldPath):
|
||||
return self.get(path)
|
||||
|
||||
def __setitem__(self, item: bytes, value) -> None:
|
||||
def __setitem__(self, item: dna.FieldPath, value) -> None:
|
||||
self.set(item, value)
|
||||
|
||||
def has_field(self, name: bytes) -> bool:
|
||||
|
||||
@@ -310,7 +310,7 @@ class Struct:
|
||||
self,
|
||||
file_header: header.BlendFileHeader,
|
||||
fileobj: typing.IO[bytes],
|
||||
path: bytes,
|
||||
path: FieldPath,
|
||||
value: typing.Any,
|
||||
):
|
||||
"""Write a value to the blend file.
|
||||
@@ -319,7 +319,6 @@ class Struct:
|
||||
struct on disk (e.g. the start of the BlendFileBlock containing the
|
||||
data).
|
||||
"""
|
||||
assert isinstance(path, bytes), "path should be bytes, but is %s" % type(path)
|
||||
|
||||
field, offset = self.field_from_path(file_header.pointer_size, path)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user