9 Commits

Author SHA1 Message Date
Andrej730
47c3e8d77f 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
2025-11-24 15:07:46 +01:00
William Harrell
6bfa4062d7 Support for int, float types in BlendFileBlock.set
---

The blendfile module within BAT supports reading data from structs, such
as the Count property on an array modifier. However, blendfile only
supports modifying structs with type "char". This patch adds support for
writing structs of more types in blendfile blocks. Now, writing is
supported for ushort, short, uint, int, float, and ulong types.

The use case that inspired this patch was an instance where a file had
several array modifiers that prevented the file from being opened in
Blender on machines without large amounts of RAM. A solution using the
blendfile module may look like:

```
from blender_asset_tracer import blendfile
from pathlib import Path

b = blendfile.open_cached(Path('flag.blend'), mode='rb+')

for block in b.blocks:
    if 'ArrayModifierData' in block.__str__():
        try:
            print('previous:', block.get(b'count'))
            block.set(b'count', 1)
            print('current:', block.get(b'count'))
        except KeyError:
            continue

b.close()
```

This would fail with the exception
`blender_asset_tracer.blendfile.exceptions.NoWriterImplemented: Setting
type Struct(b'int') is not supported for ArrayModifierData.count`. With
this patch, the above code succeeds and the count struct can be set to
a lower number that allows the file to be opened.

This solution implements missing functionality without adding any new
interfaces. A few details are:
* When deciding what type to write to the struct, the value is inferred
from what is given by the caller. If the caller gives a Python int, the
exact type is inferred from the DNA type ID. If they give a float, a
float is written. Otherwise, the existing logic is used to determine
whether to write a string or byte sequence.
* A \_write method was added to dna\_io.py that takes a Python struct
object and a value to write a byte sequence to the file object. This
method is used by public methods appropriately named to indicate what
type they will write.
* The check for whether the caller is trying to write an unsupported
type is left in place, but it has been changed to include types which
are now supported.
* Tests have been added that provide a mock file object, call the new
methods, and confirm that the correct bytes were written.

Reviewed By: sybren

Differential Revision: https://developer.blender.org/D14374
2022-03-25 12:07:06 +01:00
Sybren A. Stüvel
803c38dac1 Cleanup: reformat with Black 2021-07-22 10:14:43 +02:00
Sybren A. Stüvel
59c0b6df4c Allow block.get() to return the dna.Field
This is needed by the upcoming dependency tracer.
2018-02-26 18:15:14 +01:00
Sybren A. Stüvel
8d32ab513f Recursively add offsets of Struct.field_from_path() 2018-02-23 12:24:50 +01:00
Sybren A. Stüvel
63c7e4fcef Ported iteration over properties 2018-02-22 17:44:50 +01:00
Sybren A. Stüvel
87300df6a3 Fixed issue reading array items and renamed some stuff 2018-02-22 16:14:10 +01:00
Sybren A. Stüvel
e66b1edaf4 Implemented + tested dna.Struct.field_get 2018-02-22 14:50:24 +01:00
Sybren A. Stüvel
dcda85c3e6 Ported dna.Name, dna.Field, and started with dna.Struct 2018-02-22 14:50:24 +01:00