Add BlendFileBlock.raw_data() and .as_string() functions

Add functions to interpret the data in a `BlendFileBlock` as either `bytes`
or `string`. This is used to obtain the contents of a `char*` (instead
of an embedded `char[N]` array).
This commit is contained in:
Sybren A. Stüvel 2023-05-16 16:00:33 +02:00
parent d2b353d1bf
commit 055457ab67
2 changed files with 21 additions and 0 deletions

View File

@ -3,6 +3,10 @@
This file logs the changes that are actually interesting to users (new features,
changed functionality, fixed bugs).
# Version 1.16 (in development)
- Add `BlendFileBlock.raw_data()` and `.as_string()` functions. These functions interpret the data in a `BlendFileBlock` as either `bytes` or `string`. This can be used to obtain the contents of a `char*` (instead of the more common embedded `char[N]` array).
# Version 1.15 (2022-12-16)
- Add support for fluid simulation caches.

View File

@ -610,6 +610,23 @@ class BlendFileBlock:
return value, field
return value
def raw_data(self) -> bytes:
"""Read low-level raw data of this datablock."""
self.bfile.fileobj.seek(self.file_offset, os.SEEK_SET)
return self.bfile.fileobj.read(self.size)
def as_string(self) -> str:
"""Interpret the bytes of this datablock as null-terminated utf8 string."""
the_bytes = self.raw_data()
try:
first_null = the_bytes.index(0)
except ValueError:
pass
else:
the_bytes = the_bytes[:first_null]
return the_bytes.decode()
def get_recursive_iter(
self,
path: dna.FieldPath,