Python API support for creating CurveMapping, CurveProfile and ColorRamp properties. #105781

Open
opened 2023-03-15 05:14:18 +01:00 by Kévin Dietrich · 1 comment

Motivation

Add-on developers sometimes make use of curve mapping, color ramp, and curve profile properties. However, there is no way to create such properties in Python. To deal with this limitation, developers use hacks 1 which mostly revolve around creating a shader or texture node tree containing a node which has a curve mapping or color ramp property and use said nodes property for UI drawing and data evaluation. Besides limiting or even preventing development of procedural features based on curve or color functions, this can clutter the main database with many irrevelant node trees, and cause unrelated UI/data updates when the property is modified by the user.

Solution

The proposed solution is to allow creating curve mapping, color ramps, and curves profiles properties via the Python API, i.e. allowing my_curve: bpy.props.CurveMappingProperty(...)

Proposed implementation

Specific RNA property types for CurveMapping, CurveProfile, and ColorBand (ColorRamp in Python) are added (e.g. CurveMappingPropertyRNA, enum PROP_CURVE_MAPPING). The property types replace the current PROP_POINTER (e.g. a CurveMapping RNA property is now a PROP_CURVE_MAPPING instead of PROP_POINTER).

Since Python properties are stored using IDProperties, specific IDProperties types for curve mapping, curve profile, and color ramp are also added. IDProperties for those new types store a pointer to some CurveMapping, CurveProfile, or ColorBand object which is dynamically allocated; this mimics how such properties are stored in DNA.

We could consider other ways of storing the data in IDProperties, e.g. with a scalar arrays which describe the curves, instead of a pointer to some heap object, but this would mean adding conversion functions between the UI data and the IDProperty's data. Such conversion functions may not be too intrusive, but still a potential source of complexity and divergent behaviors (leading to bugs) between "native" mapping/profile/ramp and Python ones. Therefore, to keep things simple, I opt to simply store a pointer to some CurveMapping/CurvePorfile/ColorBand object (as could be seen in my original set of patches ([2], [3], & [4])).

API Future Proofing

By allowing creating such property types, some implementation details or limitations specific to the mapping/profile/ramp are exposed in the API. For example:

  1. Curve mappings can only have a fixed number of curves (1, 3, or 4) which has to be set during creation. The behaviour is kept for the Python equivalent, albeit by using an enumeration on the type "scalar", "rgb", etc.

  2. Color ramps only have a limited number of slots (32). However, this is not exposed in any API (C or otherwise), but could be the source of API breakage if exposed.

  3. Curve profiles need a preset at creation time, again this could change in the future.

This list is non-exhaustive, as there might be other sources of API compatibility issues which I fail to consider.

Since add-on developpers are currently only using built-in mapping/profile/ramp widgets, no attempt was made to remove these limitations; the current work merely exposes the various C API for creating such widgets to Python as is. This all begs a few design consideration for future proofing the API.

Notes

Data storage in IDProperties and API future proofing seem to be the main points of contention.

Links

[2]: Python API: support creating Curve Mapping properties (https://archive.blender.org/developer/D15824)
[3]: Python API: support creating Curve Profile properties (https://archive.blender.org/developer/D15825)
[4]: Python API: support creating Color Ramp properties (https://archive.blender.org/developer/D15826)

**Motivation** Add-on developers sometimes make use of curve mapping, color ramp, and curve profile properties. However, there is no way to create such properties in Python. To deal with this limitation, developers use hacks [1] which mostly revolve around creating a shader or texture node tree containing a node which has a curve mapping or color ramp property and use said nodes property for UI drawing and data evaluation. Besides limiting or even preventing development of procedural features based on curve or color functions, this can clutter the main database with many irrevelant node trees, and cause unrelated UI/data updates when the property is modified by the user. **Solution** The proposed solution is to allow creating curve mapping, color ramps, and curves profiles properties via the Python API, i.e. allowing `my_curve: bpy.props.CurveMappingProperty(...)` **Proposed implementation** Specific RNA property types for CurveMapping, CurveProfile, and ColorBand (ColorRamp in Python) are added (e.g. `CurveMappingPropertyRNA`, enum `PROP_CURVE_MAPPING`). The property types replace the current `PROP_POINTER` (e.g. a CurveMapping RNA property is now a `PROP_CURVE_MAPPING` instead of `PROP_POINTER`). Since Python properties are stored using *IDProperties*, specific *IDProperties* types for curve mapping, curve profile, and color ramp are also added. *IDProperties* for those new types store a pointer to some CurveMapping, CurveProfile, or ColorBand object which is dynamically allocated; this mimics how such properties are stored in DNA. We could consider other ways of storing the data in *IDProperties*, e.g. with a scalar arrays which describe the curves, instead of a pointer to some heap object, but this would mean adding conversion functions between the UI data and the *IDProperty*'s data. Such conversion functions may not be too intrusive, but still a potential source of complexity and divergent behaviors (leading to bugs) between "native" mapping/profile/ramp and Python ones. Therefore, to keep things simple, I opt to simply store a pointer to some CurveMapping/CurvePorfile/ColorBand object (as could be seen in my original set of patches ([2], [3], & [4])). **API Future Proofing** By allowing creating such property types, some implementation details or limitations specific to the mapping/profile/ramp are exposed in the API. For example: 1. Curve mappings can only have a fixed number of curves (1, 3, or 4) which has to be set during creation. The behaviour is kept for the Python equivalent, albeit by using an enumeration on the type "scalar", "rgb", etc. 2. Color ramps only have a limited number of slots (32). However, this is not exposed in any API (C or otherwise), but could be the source of API breakage if exposed. 3. Curve profiles need a preset at creation time, again this could change in the future. This list is non-exhaustive, as there might be other sources of API compatibility issues which I fail to consider. Since add-on developpers are currently only using built-in mapping/profile/ramp widgets, no attempt was made to remove these limitations; the current work merely exposes the various C API for creating such widgets to Python as is. This all begs a few design consideration for future proofing the API. **Notes** Data storage in *IDProperties* and API future proofing seem to be the main points of contention. **Links** [1]: https://blender.stackexchange.com/questions/61618/add-a-custom-curve-mapping-property-for-an-add-on [2]: Python API: support creating Curve Mapping properties (https://archive.blender.org/developer/D15824) [3]: Python API: support creating Curve Profile properties (https://archive.blender.org/developer/D15825) [4]: Python API: support creating Color Ramp properties (https://archive.blender.org/developer/D15826)
Kévin Dietrich added the
Type
Design
Interest
Core
Interest
Python API
labels 2023-03-15 05:14:19 +01:00
Kévin Dietrich added this to the Python API project 2023-03-15 05:14:19 +01:00

Are there any chances that this gets reviewed and implemented after all? This would be really powerful for add-on development! I know there is a workaround, but custom curve mapping properties would still be much more elegant!

Are there any chances that this gets reviewed and implemented after all? This would be really powerful for add-on development! I know there is a workaround, but custom curve mapping properties would still be much more elegant!
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#105781
No description provided.