WIP: GPv3: Python API for frame, drawing and builtin geometry attributes #122094

Draft
Sietse Brouwer wants to merge 9 commits from SietseB/blender:gpv3-rna-attribute-array into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This PR implements the GPv3 RNA/Python API for frame, drawing and builtin geometry attributes.

Data structure in Python:

  • frame = layer.frames[index]
  • drawing = frame.drawing

Layer functions:

  • frame = layer.get_frame_at(frame_number)

Frame properties:

  • frame.drawing
  • frame.frame_number
  • frame.select: the selection state of the keyframe in the Dope sheet

Frame functions:

  • layer.frames.new(frame_number)
  • layer.frames.remove(frame_number)
  • layer.frames.copy(from, to)

Accessing stroke and point attributes in drawing:

  • drawing.cyclic[stroke_index]
  • drawing.start_caps[stroke_index]
  • drawing.positions[point_index]
  • drawing.radii[point_index]
  • etc...

Get the start and stop point index of a stroke:

point_range = drawing.strokes[stroke_index]
start_index = point_range.start
stop_index = point_range.stop
# For use in ranges and slices
range(point_range.start, point_range.stop)
drawing.radii[point_range.start:point_range.stop]

Foreach_get and foreach_set (using numpy):

positions = np.ndarray((drawing.num_points, 3), dtype=np.float32)
drawing.positions.foreach_get(positions)
positions[:, 0] += 1.0
drawing.positions.foreach_set(positions)

Fill an attribute:

drawing.radii.fill(0.1)  # Set the radius of all points in the drawing
drawing.radii.fill(0.1, [0, 1, 2, 3])  # Using indices: set the radius of the first four points

# Select all points with a radius between 0.1 and 0.2
radii = np.ndarray(drawing.num_points, dtype=np.float32)
drawing.radii.foreach_get(radii)
indices = np.nonzero((radii > 0.1) & (radii < 0.2))[0]
drawing.point_selections.fill(True, indices)

Stroke attributes:

  • drawing.cyclic[stroke_index]
  • drawing.start_caps[stroke_index]
  • drawing.end_caps[stroke_index]
  • drawing.hardnesses[stroke_index]
  • drawing.material_indices[stroke_index]
  • drawing.stroke_selections[stroke_index]
  • drawing.fill_colors[stroke_index]
  • drawing.aspect_ratios[stroke_index]
  • drawing.init_times[stroke_index]

Point attributes:

  • drawing.positions[point_index]
  • drawing.radii[point_index]
  • drawing.opacities[point_index]
  • drawing.vertex_colors[point_index]
  • drawing.point_selections[point_index]
  • drawing.rotations[point_index]
  • drawing.delta_times[point_index]

Drawing functions:

  • drawing.add_strokes([num_points_stroke_1, num_points_stroke_2, ...])
  • drawing.tag_redraw() for updating the drawing in the UI/viewport

Part of this PR is a python template with Grease Pencil examples using the Python API. This template can be opened in the text editor in Blender.


Technical notes

This PR implements the GPv3 RNA/Python API for frame, drawing and builtin geometry attributes. Data structure in Python: - `frame = layer.frames[index]` - `drawing = frame.drawing` Layer functions: - `frame = layer.get_frame_at(frame_number)` Frame properties: - `frame.drawing` - `frame.frame_number` - `frame.select`: the selection state of the keyframe in the Dope sheet Frame functions: - `layer.frames.new(frame_number)` - `layer.frames.remove(frame_number)` - `layer.frames.copy(from, to)` Accessing stroke and point attributes in `drawing`: - `drawing.cyclic[stroke_index]` - `drawing.start_caps[stroke_index]` - `drawing.positions[point_index]` - `drawing.radii[point_index]` - etc... Get the start and stop point index of a stroke: ```python point_range = drawing.strokes[stroke_index] start_index = point_range.start stop_index = point_range.stop # For use in ranges and slices range(point_range.start, point_range.stop) drawing.radii[point_range.start:point_range.stop] ``` Foreach_get and foreach_set (using numpy): ```python positions = np.ndarray((drawing.num_points, 3), dtype=np.float32) drawing.positions.foreach_get(positions) positions[:, 0] += 1.0 drawing.positions.foreach_set(positions) ``` Fill an attribute: ```python drawing.radii.fill(0.1) # Set the radius of all points in the drawing drawing.radii.fill(0.1, [0, 1, 2, 3]) # Using indices: set the radius of the first four points # Select all points with a radius between 0.1 and 0.2 radii = np.ndarray(drawing.num_points, dtype=np.float32) drawing.radii.foreach_get(radii) indices = np.nonzero((radii > 0.1) & (radii < 0.2))[0] drawing.point_selections.fill(True, indices) ``` Stroke attributes: - `drawing.cyclic[stroke_index]` - `drawing.start_caps[stroke_index]` - `drawing.end_caps[stroke_index]` - `drawing.hardnesses[stroke_index]` - `drawing.material_indices[stroke_index]` - `drawing.stroke_selections[stroke_index]` - `drawing.fill_colors[stroke_index]` - `drawing.aspect_ratios[stroke_index]` - `drawing.init_times[stroke_index]` Point attributes: - `drawing.positions[point_index]` - `drawing.radii[point_index]` - `drawing.opacities[point_index]` - `drawing.vertex_colors[point_index]` - `drawing.point_selections[point_index]` - `drawing.rotations[point_index]` - `drawing.delta_times[point_index]` Drawing functions: - `drawing.add_strokes([num_points_stroke_1, num_points_stroke_2, ...])` - `drawing.tag_redraw()` for updating the drawing in the UI/viewport --- Part of this PR is a [python template with Grease Pencil examples](https://projects.blender.org/SietseB/blender/src/commit/1e7bbc69a5839db9cc91ef10f76010ab4f734498/scripts/templates_py/grease_pencil_examples.py) using the Python API. This template can be opened in the text editor in Blender. --- **Technical notes** - When changing attributes of a drawing, the changes are not immediately reflected in the viewport. Use `drawing.tag_redraw()` to update the drawing. - This is a PR on top of [#122091: Python API: Direct access to attribute arrays of meshes, curves, point clouds and GP drawings](https://projects.blender.org/blender/blender/pulls/122091). It is included in this PR for now, to be able to build and test the code.
Sietse Brouwer added 8 commits 2024-05-22 12:03:57 +02:00
Sietse Brouwer added this to the Grease Pencil project 2024-05-22 12:04:14 +02:00
Author
Member

For now, to be able to build and test the Python API, this PR includes #122091: Python API: Direct access to attribute arrays of meshes, curves, point clouds and GP drawings.

Only relevant files for review are:

  • scripts/templates_py/grease_pencil_examples.py
  • source/blender/makesrna/intern/rna_grease_pencil.cc
For now, to be able to build and test the Python API, this PR includes [#122091: Python API: Direct access to attribute arrays of meshes, curves, point clouds and GP drawings](https://projects.blender.org/blender/blender/pulls/122091). Only relevant files for review are: - `scripts/templates_py/grease_pencil_examples.py` - `source/blender/makesrna/intern/rna_grease_pencil.cc`
Sietse Brouwer added 1 commit 2024-05-23 00:02:51 +02:00
Use switch statement for attribute type functions
All checks were successful
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
c4a716f781
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR122094) when ready.
All checks were successful
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
This pull request has changes conflicting with the target branch.
  • source/blender/makesrna/intern/rna_grease_pencil.cc

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u gpv3-rna-attribute-array:SietseB-gpv3-rna-attribute-array
git checkout SietseB-gpv3-rna-attribute-array
Sign in to join this conversation.
No reviewers
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
3 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#122094
No description provided.