Commit Graph

29 Commits

Author SHA1 Message Date
Bastien Montagne
469f54f484 BPY: Implement get_transform and set_transform for runtime-defined RNA properties.
Improve handling of runtime defined python RNA properties. Mainly:
* Add `get_transform` and `set_transform` new callbacks.
  These allow to edit the value, while still using the default
  (IDProperty-based) storage system.
* Read-only properties should now be defined using a new `options` flag,
  `READ_ONLY`.
* `get`/`set` should only be used when storing data outside of the
  default system now.
  * Having a `get` without a `set` defined forces property to be
    read-only (same behavior as before).
  * Having a `set` without a `get` is now an error.
* Just like with existing `get/set` callbacks, `get_/set_transform`
  callbacks must always generate values matching the constraints defined
  by their `bpy.props` property definition (same type, within required
  range, same dimensions/sizes for the `Vector` properties, etc.).
* To simplify handling of non-statically sized strings, the relevant
  RNA API has been modified, to use `std::string` instead of
  (allocated) char arrays.

Relevant unittests and benchmarking have been added or updated as part
of this project.

Note: From initial benchmarking, 'transform' versions of get/set are
several times faster than 'real' get/set.

Implements #141042.

Pull Request: https://projects.blender.org/blender/blender/pulls/141303
2025-09-02 11:30:09 +02:00
Bastien Montagne
8bb8bff4bf Tests: bpy props: Add systematic tests for non-array types.
Fairly basic tests still, would need to be extended to check invalid
cases handling too.

Pull Request: https://projects.blender.org/blender/blender/pulls/145136
2025-08-25 18:47:37 +02:00
Bastien Montagne
7276b2009a Core: Add new 'system IDprops' storage for runtime-generated RNA properties.
This commit cleanly splits IDProperties storage for its two very different
usages:
* "User-defined" data, also known as "custom properties". Mostly exposed
  in UI and as 'dictionary' in Python scripting, these are unique to each data
  (each object can have its own set of custom properties).
* "System-defined" data, mainly as backend-storage for runtime RNA
  structures and properties. While these are not necessarily present in the
  storage, they are registered for a data type, and therefore always available
  to all data of that type through RNA access.

See #123232 for rationales, designs and alternative investigated solutions.

## User-facing Changes

When using Blender, the only noticeable change is that python-defined RNA
data are not listed anymore in the Custom Properties panels (e.g. Cycles
data).

From a Python API perspective, the main changes are:
* Runtime RNA structs defined by sub-classing `PropertyGroup` and
  registering them are no more accessible through the 'dict' syntax.
  * They remain accessible through a dedicated 'bl_system_properties_get()`
    callback, but its usages are only expected to be for testing and
    debugging.
  * The result of this call will be `None` by default when there has been
    nothing written into it yet, unless its optional `do_create` parameter
    is set to `True`.
* Some types (like `Node`, `UIList`, etc.) cannot store raw IDProperties
  anymore (using the 'dict' syntax).

## Technical Details

* Adds System idprops to some data types (IDs, ViewLayer...).
* Moves some other containers (e.g operator properties, or some UI types like
  UILists) to only have system-defined properties.
* For a few specific types (like `PropertyGroup`), the same storage is used,
  but exposed in RNA as both user and system properties.
* Adds RNA API accessor callback to system idprops.
* Adds a function `bl_system_properties_get()`, which wraps system-defined
  idprops and gives 'dict-like' access to them. Currently mainly used by some
  unittests.
* System IDProps are always ignored by RNA diffing code (and therefore
  liboverride processing), as their value is already exposed through RNA
  properties, and should always be processed through these RNA properties.
* Modifies bpy rna binding to use these new system idprops for property
  accesses, and keeps using user-defined idprops for 'dict-type' accesses.
* Handles versioning by copying 'user idprops' (existing ones) into new
  'system idprops'.

### IDProperties Split

These types keep their historic IDProperty storage for user properties,
and get a new `system_id_properties` storage for system properties:

`ID`, `ViewLayers`, `Bone`, `EditBone`, `BoneCollection`, `PoseBone`, `Strip`

These types can both be extended with registrable RNA properties, and
expose Custom Properties in the UI.

### IDProperties become System Ones

These types keep a single IDProperties storage (their DNA struct does not
change), but it is now exclusively used for system-defined properties.

`OperatorProperty`, `View3DShading`, `UIList`, `AddonPreferences`,
`KeyConfigPreferences`, `GizmoProperties`, `GizmoGroupProperties`,
`Node`, `NodeSocket`, `NodeTreeInterfaceSocket`, `TimelineMarker`,
`AssetMetaData``

Since user properties were never available in the UI for them, they lose
their 'dict-like' IDProperties access in the Python API.

### Single Storage, Exposed as Both in API

These types still use a single IDProperty storage, but expose it both as
user properties and as system ones through RNA API.

* `PropertyGroup`: They need both access APIs since they are both
  used for raw IDProperty groups (the 'dict-like' access), and
  internally to access data of runtime-defined RNA structs.
* `IDPropertyWrapPtr`: Done mainly to follow `PropertyGroup`.
* `NodesModifier`: cannot become purely system idprops currently, as
  there is no other access than the 'raw ID properties' paths to their
  values. This can be changed once #132129 is finally implemented.

Pull Request: https://projects.blender.org/blender/blender/pulls/135807
2025-06-23 18:25:24 +02:00
Campbell Barton
4a9d67df0e Cleanup: format tests 2024-09-05 13:07:03 +10:00
Bastien Montagne
b03a6bbd60 Cleanup: Remove left-over debug print in tests. 2024-06-13 20:50:13 +02:00
Bastien Montagne
5dfc197262 IDProp: BPY: Support assigning large int values to float properties.
Allow assigning integer values beyond int32 range to float/double
IDProperties. Extract the py object value into a temporary int64 value
in these cases.
2024-06-13 20:47:46 +02:00
Bastien Montagne
b278e37f70 IDProps: Make 'dynamic RNA' IDProperties statically typed.
All IDProperties generated as part of 'storage backend' for dynamic RNA
properties are now statically typed.

Also adds some basic unittests for the new statically typed IDProperty
system, based on py-defined RNA data.

Ref. #122743.
2024-06-13 19:58:22 +02:00
Bastien Montagne
65f68fe7fe BPY IDProp tests: Add basic tests for 'UI data' system.
This are not complete by far, mainly here to illustrate the issue
reported in #122843 (mismatch returned value for 'enum' idprops).
2024-06-10 15:54:29 +02:00
Bastien Montagne
a799c84946 BPY IDProp tests: test matching results from subscription and path_resolve.
Add (partially disabled) systematic tests that for all IDProp types, the
returned value from direct idprop subscription and RNA-based
`path_resolve` return the same data.

NOTE: This does not work currently for 'composite' types (`IDP_ARRAY` and
`IDP_GROUP`).
2024-06-10 14:27:16 +02:00
Campbell Barton
2119d271e0 Cleanup: remove "-noaudio" argument in background mode
This is no longer needed as background mode implies -noaudio.
2024-02-14 00:13:38 +11:00
Lukas Tönne
92cf9dd2f2 ID properties: Support enum values with items
Add support for enum values in ID properties.

This is needed for the "Menu Switch" node implementation (#113445) which
relies on ID properties for the top-level modifier UI.

Enums items can optionally be added to the UI data of integer
properties. Each property stores a full set of the enum items to keep
things simple.

Enum items can be added to properties using the `id_properties_ui`
function in the python API. A detailed example can be found in the
`bl_pyapi_idprop.py` test.

There is currently no support yet for editing enum items through the UI.
This is because the "Edit Property" feature is implemented entirely
through a single operator (`WM_OT_properties_edit`) and its properties.
Buttons to add/remove/move items would be operators changing another
operator's properties. A refactor of the custom properties UI is likely
required to make this work.

Pull Request: https://projects.blender.org/blender/blender/pulls/114362
2023-12-15 10:20:44 +01:00
Campbell Barton
8df5205028 Fix RNA path resolving failing to lookup empty string properties
An empty string is a valid ID property name, when set, RNA's
internal lookup function (rna_path_token_in_brackets), considered
the path invalid.

Now quoted tokens are allowed to be empty.

This fixes Python exceptions being thrown in the custom property editor
with empty custom property names.
2023-08-22 13:31:26 +10:00
Campbell Barton
e044c5f12b Tests: add ID-property TestRNAData.test_custom_properties_access
Ensures RNA/ID-property key lookups are compatible.
2023-08-22 13:31:05 +10:00
Campbell Barton
e955c94ed3 License Headers: Set copyright to "Blender Authors", add AUTHORS
Listing the "Blender Foundation" as copyright holder implied the Blender
Foundation holds copyright to files which may include work from many
developers.

While keeping copyright on headers makes sense for isolated libraries,
Blender's own code may be refactored or moved between files in a way
that makes the per file copyright holders less meaningful.

Copyright references to the "Blender Foundation" have been replaced with
"Blender Authors", with the exception of `./extern/` since these this
contains libraries which are more isolated, any changed to license
headers there can be handled on a case-by-case basis.

Some directories in `./intern/` have also been excluded:

- `./intern/cycles/` it's own `AUTHORS` file is planned.
- `./intern/opensubdiv/`.

An "AUTHORS" file has been added, using the chromium projects authors
file as a template.

Design task: #110784

Ref !110783.
2023-08-16 00:20:26 +10:00
Campbell Barton
65f99397ec License headers: use SPDX-FileCopyrightText in all sources 2023-06-15 13:35:34 +10:00
Campbell Barton
7571222a69 Cleanup: add trailing commas for multi-line collections
Avoid accidentally missing commas between strings, see: T101020.
Also use single quotes for enum identifiers.
2022-12-15 17:34:09 +11:00
Campbell Barton
39c341bf4a Cleanup: remove redundant braces from assert & raise
autopep8 v1.7 added a space after assert & raise,
remove the braces as they aren't needed.
2022-09-14 16:18:59 +10:00
Campbell Barton
3035235def Cleanup: run autopep8 on tests/ 2022-04-19 15:25:55 +10:00
Campbell Barton
27fb63381e Fix T94121: PyAPI: ID property group returns wrong type with iter()
Regression in 265d97556a.
Where iterating directly on a property group failed, e.g.:
`iter(group)`, tests missed this since only `group.keys()`
was checked.
2022-03-11 10:26:27 +11:00
Campbell Barton
c434782e3a File headers: SPDX License migration
Use a shorter/simpler license convention, stops the header taking so
much space.

Follow the SPDX license specification: https://spdx.org/licenses

- C/C++/objc/objc++
- Python
- Shell Scripts
- CMake, GNUmakefile

While most of the source tree has been included

- `./extern/` was left out.
- `./intern/cycles` & `./intern/atomic` are also excluded because they
  use different header conventions.

doc/license/SPDX-license-identifiers.txt has been added to list SPDX all
used identifiers.

See P2788 for the script that automated these edits.

Reviewed By: brecht, mont29, sergey

Ref D14069
2022-02-11 09:14:36 +11:00
Campbell Barton
3d35d4a9e5 Tests: support running ID-property test without numpy 2021-10-12 17:57:38 +11:00
Hans Goudey
8b9a3b94fc Refactor IDProperty UI data storage
The storage of IDProperty UI data (min, max, default value, etc) is
quite complicated. For every property, retrieving a single one of these
values involves three string lookups. First for the "_RNA_UI" group
property, then another for a group with the property's name, then for
the data value name. Not only is this inefficient, it's hard to reason
about, unintuitive, and not at all self-explanatory.

This commit replaces that system with a UI data struct directly in the
IDProperty. If it's not used, the only cost is of a NULL pointer. Beyond
storing the description, name, and RNA subtype, derived structs are used
to store type specific UI data like min and max.

Note that this means that addons using (abusing) the `_RNA_UI` custom
property will have to be changed. A few places in the addons repository
will be changed after this commit with D9919.

**Before**
Before, first the _RNA_UI subgroup is retrieved the _RNA_UI group,
then the subgroup for the original property, then specific UI data
is accessed like any other IDProperty.
```
prop = rna_idprop_ui_prop_get(idproperties_owner, "prop_name", create=True)
prop["min"] = 1.0
```

**After**
After, the `id_properties_ui` function for RNA structs returns a python
object specifically for managing an IDProperty's UI data.
```
ui_data = idproperties_owner.id_properties_ui("prop_name")
ui_data.update(min=1.0)
```
In addition to `update`, there are now other functions:
 - `as_dict`: Returns a dictionary of the property's UI data.
 - `clear`: Removes the property's UI data.
 - `update_from`: Copy UI data between properties,
   even if they have different owners.

Differential Revision: https://developer.blender.org/D9697
2021-08-27 08:27:24 -05:00
Campbell Barton
432bfbf7a3 Cleanup: pep8 2021-07-06 12:05:27 +10:00
Campbell Barton
265d97556a PyAPI: use iterators for ID property methods (keys, values & items)
- Matches changes in Python 3.x dictionary methods.

- Iterating now raises a run-time error if the property-group changes
  size during iteration.

- IDPropertyGroup.iteritems() has been removed.

- IDPropertyGroup View & Iterator types have been added.

- Some set functionality from dict_keys/values/items aren't yet
  supported (isdisjoint method and boolean set style operations).

Proposed as part of T85675.
2021-05-15 00:36:49 +10:00
Campbell Barton
3c09beb3b1 Fix memory leak in IDPropertyGroup.pop()
When popping ID-property groups/arrays,
ID-property was removed but not freed.

Now the value is converted to a native Python type and freed.
2021-05-14 19:25:29 +10:00
Howard Trickey
0af22625c9 Fix bl_pyapi_idprop unit test.
Scenes can now have a 'cycles' key when starting, so account for that.
2019-03-17 07:36:26 -04:00
Campbell Barton
8c15d612a5 Cleanup: pep8 2018-07-03 06:55:02 +02:00
Jacques Lucke
73a9ff0d2d PyAPI: Fast buffer access to id property arrays
Support Python's buffer protocol for ID-properties.
2017-04-10 21:12:02 +10:00
Jacques Lucke
56d3cc9341 PyAPI: ID Property tests 2017-03-19 03:57:40 +11:00