Commit Graph

7212 Commits

Author SHA1 Message Date
Nick Alberelli
e2872c0bfe Core: de-duplicate bpy.context and context logging
Merges the existing `bpy.context` logging with the new `context` logging
added in [0]. In most cases `bpy.context` & `context` log the same thing.
Where the new `context` logging produces more accurate results.

Ref !146407

[0]: 439fe8a1a0
2025-10-07 23:45:16 +11:00
Campbell Barton
62d72bd0b5 UV: initial implementation of UV sync select
Support sync selection in the UV editor, with face-corner selection,
so it's possible to select individual UV vertices/edges in the UV editor
without UV's attached to the same underlying edge also becoming selected.

There is limited support for maintaining the UV selection when selecting
from the 3D viewport, common operations such as picking &
box/circle/lasso select support this, however other selection operations
such as "Select Random" or "Select Similar" will clear this data,
causing all UV's connected to selected mesh elements to become selected.
We may add support for additional operators as needed.

Details:

- UV Sync Selection is now enabled by default.
- In edit-mode the UV selection is stored in BMLoop/BMFace which are
  written to custom-data layers when converted to a Mesh.
- To avoid unnecessary overhead - this data is created on demand.
  Operators may clear this data - selecting all or none do so,
  as there is no reason to store this data for a uniform selection.
- The Python API includes functions to synchronize the selection to/from
  UV's as well as flushing based on the mode.
- Python scripts that manipulate the selection will either need to clear
  this synchronized state or maintain it.

See:
- Design task: #78393.
- Implementation task: #131642.

Ref !138197
2025-10-07 01:41:16 +00:00
Campbell Barton
01806a62e3 Cleanup: spelling (make check_spelling_*) 2025-10-07 10:19:46 +11:00
Sybren A. Stüvel
d33a6a1723 Python: add function for file iteration/remapping bpy.data.file_path_foreach()
Add a function `bpy.data.file_path_foreach()` with a callback function
`visit_path_fn`:

```py
def visit_path_fn(
    owner_id: bpy.types.ID,
    file_path: str,
    _meta: typing.Any,
) -> str | None:
    return file_path.replace('xxx', 'yyy') or None

bpy.data.file_path_foreach(
    visit_path_fn,
    *,
    subset=None,
    visit_types=None,
    flags={},
)
```

When the function returns `None`, nothing happens. When it returns a
string, the reported file path is replaced with the returned string.

`subset` and `visit_types` have the same meaning as `subset` resp.
`key_types` in `bpy.data.file_path_map()`. Since the latter parameter
doesn't affect keys in a map, but rather the visited ID types, I named
it `visit_types` instead.

`flags` wraps most of `eBPathForeachFlag` as `set[str]`, with their
prefixes removed. `BKE_BPATH_FOREACH_PATH_RESOLVE_TOKEN` becomes
`RESOLVE_TOKEN`, etc. `BKE_BPATH_FOREACH_PATH_ABSOLUTE` is excluded,
as it only affects a field in the C++ callback structure that is not
passed to Python at all. If it turns out to be useful at some point,
we can always add it.

`_meta` is for future use, and is intended to give metadata of the
path (like whether it's an input or an output path, a single file or
the first file in a sequence, supports variables, etc.). By adding
this to the API now, we avoid a breaking change when this feature is
actually implemented.

Design task: #145734

Pull Request: https://projects.blender.org/blender/blender/pulls/146261
2025-10-06 17:10:49 +02:00
Nathan Vegdahl
e6f1cd6a29 Anim: remove deprecated XYZ_TO_RGB flag on keyframe_insert()
The `INSERTKEY_XYZ_TO_RGB` flag on `datablock.keyframe_insert()` in
Blender's Python API was changed to do nothing in #115297 and #119625,
in favor of determining fcurve coloring exclusively from user preferences.

This PR removes that now-useless `INSERTKEY_XYZ_TO_RGB` flag entirely.

Pull Request: https://projects.blender.org/blender/blender/pulls/147262
2025-10-06 03:46:54 +02:00
Campbell Barton
c5a317789d PyAPI: rename BMesh.select_flush_mode "down" argument to "flush_down"
Based on the Python API for UV selection (!138197),
some methods that set the selection need a flush down argument too,
name the argument more clearly so the same name can be used by
all selection functions.
2025-10-05 10:38:41 +11:00
Campbell Barton
d990026fd6 PyAPI: add option for BMesh.select_flush_mode() to flush down
Support optionally flushing down:
- faces -> edges & verts.
- edges -> verts.

For C++ selection logic flushing down is avoided as it's an extra step
and instead, selection logic must take care to de-select edges/faces
before selection, so any shared vertices or edges are selected.

Flushing down means scripts can set the selection on faces or edges,
then flush this to all other elements without being concerned with
the order they have been set.

Ref !147351
2025-10-04 22:45:42 +10:00
Campbell Barton
a1967f1974 Fix: newly created BMesh from Python left select_mode set to zero
The BMesh::selectmode is expected to be set when performing any
selection operations on the BMesh. While it wasn't possible to set
the select_mode to zero from Python, this meant assigning the attribute
from one BMesh to another could fail with an exception.
Aside from this, selection logic wouldn't work as expected.

Ref !147278
2025-10-03 21:38:58 +10:00
Hans Goudey
a68d39e9d9 Cleanup: Formatting
Run `make format` after the library update in the previous commit.
2025-10-02 12:55:42 -04:00
Sebastian Parborg
df7273930f PyAPI: Add "path_from_module" function to get full bpy path to structs and props
This is to make it easy to get the bpy paths to bpy.context.property for
example. Before this change, the easiest way was to use the clipboard
bpy.ops function. However this would make the python scripts using this
fragile as it we do not have full control of what is stored in the
system clipboard.

Pull Request: https://projects.blender.org/blender/blender/pulls/147116
2025-10-02 17:05:43 +02:00
Oxicid
44d04ad857 PyAPI: add mathutils.geometry.intersect_point_line_segment function
Add a *_line_segment equivalent of the *_line function,
which clamps to the end-points.

Ref !146490
2025-10-02 11:53:02 +10:00
Oxicid
9f45b479fd PyAPI: use METH_FASTCALL to improve performance for intersect_point_line
Use METH_FASTCALL to improve performance by about 1.3x,
useful as this function may be used on large datasets.

Ref !146490
2025-10-02 11:52:24 +10:00
Campbell Barton
38e5c875c5 PyDoc: correct doc-string for intersect_point_line & minor cleanup
Apply some minor changes from !146490.
2025-10-02 11:48:19 +10:00
Pratik Borhade
c627ea9aca Fix #147067: gpu.types.GPUFrameBuffer absent in latest 5.0 build
Mistake in 9d0fe5573b

Pull Request: https://projects.blender.org/blender/blender/pulls/147099
2025-10-01 11:18:26 +02:00
Campbell Barton
f8c4d743bc PyDoc: use set[Literal[...]] as part of the type definition
Move literal values into the type definition in BMesh PyAPI docs.
2025-10-01 17:16:44 +10:00
Campbell Barton
f29330c7b2 PyDoc: correct type links in bmesh Python module
Type links without a leading module weren't recognized,
links to the types were not created in generated docs.
2025-10-01 17:05:54 +10:00
Jacques Lucke
4d91f5c8ef Fix #146588: raise exception when attempting invalid idproperty renaming in Python
Each name has to be unique within a group, so when renaming an idproperty, one
has to make sure that the parent group does not contain duplicates afterwards.
This patch raises a `NameError` when setting the name to one that exists
already. Alternatively, one could delete the already-existing property, but that
seems unexpected and the user should rather do that explicitly.

This also adds a new unit test for this case.

Pull Request: https://projects.blender.org/blender/blender/pulls/146892
2025-09-29 13:54:08 +02:00
Casey Bianco-Davis
f8d2a3af38 Python: Rename bpy.types.GreasePencilv3 to bpy.types.GreasePencil
This renames python Grease Pencil type
from `bpy.types.GreasePencilv3` to `bpy.types.GreasePencil`.

Part of #125058.

Pull Request: https://projects.blender.org/blender/blender/pulls/146902
2025-09-29 12:25:23 +02:00
Campbell Barton
c5d7d58209 Cleanup: correct terminating macro
Harmless but the begin/end macros didn't match.
2025-09-26 21:58:34 +10:00
Brecht Van Lommel
712c507519 Logging: Do not print Python API errors that will throw an exception
Python API functions would both report an error through CLOG and throw an
exception. However this is a problem when --debug-exit-on-error is used,
as tests and other scripts will not have the opportunity to catch the exception
before Blender exits.

Now don't print such reports through CLOG. Note that these usages of
BKE_reports_init have their print level set to error, so were already not
printing info and warning reports (which is not something we generally
want Python API calls to do, they should be silent).

Ref #146256

Pull Request: https://projects.blender.org/blender/blender/pulls/146801
2025-09-26 12:57:39 +02:00
Jacques Lucke
4e4976804e Core: Add packed linked data-blocks
This adds support for packed linked data. This is a key part of an improved
asset workflow in Blender.

Packed IDs remain considered as linked data (i.e. they cannot be edited),
but they are stored in the current blendfile. This means that they:
* Are not lost in case the library data becomes unavailable.
* Are not changed in case the library data is updated.

These packed IDs are de-duplicated across blend-files, so e.g. if a shot
file and several of its dependencies all use the same util geometry node,
there will be a single copy of that geometry node in the shot file.

In case there are several versions of a same ID (e.g. linked at different
moments from a same library, which has been modified in-between), there
will be several packed IDs.

Name collisions are averted by storing these packed IDs into a new type of
'archive' libraries (and their namespaces). These libraries:
* Only contain packed IDs.
* Are owned and managed by their 'real' library data-block, called an
  'archive parent'.

For more in-depth, technical design: #132167
UI/UX design: #140870

Co-authored-by: Bastien Montagne <bastien@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/133801
2025-09-26 10:53:40 +02:00
Campbell Barton
2f3e6e69df Cleanup: rename BM_mesh_{validate=>is_valid}
The term validate is often used to make the data valid
(e.g. BKE_mesh_validate) where is this only checks the mesh is valid.
2025-09-25 11:22:23 +10:00
Campbell Barton
f313540686 Cleanup: rename flushing functions for clarity
It wasn't clear how `*_flush` and `*_flush_mode` were different,
rename `*_flush` to `*_flush_from_verts`.
2025-09-24 14:26:28 +10:00
Campbell Barton
30485e6122 Cleanup: grammar, confusing wording 2025-09-24 03:06:11 +00:00
Oxicid
a714472194 PyAPI: Implement vectorcall support for rna_func
Use vectorcall for ~20-30% speedup calling Python/RNA functions.

Ref !146366
2025-09-24 02:41:08 +00:00
Campbell Barton
580af50709 Cleanup: minor naming changes for RNA functions
Split out from !146366.
2025-09-24 02:41:07 +00:00
Campbell Barton
4b82edd53c Cleanup: minor changes to the BMesh Python API
- Add typed array access functions, avoiding the need for casting.
- Share error prefix for exceptions.
- Use "num" suffix instead of "len".
2025-09-23 13:35:33 +10:00
Oxicid
3dffbb7e35 Cleanup: correct misleading PyError message formatting
Removed duplicated "Error" in `PyErr_SetString` messages

Pull Request: https://projects.blender.org/blender/blender/pulls/146473
2025-09-22 15:20:17 +02:00
Brecht Van Lommel
bb57ab3598 Fix #146448: Object copy/paste has wrong colors with working space
Write proper colorspace information into copy buffer blend files.

Pull Request: https://projects.blender.org/blender/blender/pulls/146450
2025-09-22 13:17:49 +02:00
Campbell Barton
d857c48216 Cleanup: replace select/deselect flush functions with a select argument
The naming was confusing as only some selection flushing functions
were intended to be used when elements had been selected or de-selected.

Replace these with a single function that takes a "select" argument.
2025-09-21 20:32:27 +10:00
Nick Alberelli
439fe8a1a0 PyAPI: support logging for Context.temp_override
Adds comprehensive logging system for temp_override context manager to
help developers debug "context is incorrect" operator poll failures.
The logging tracks all context member access during temp_override
sessions and provides detailed summaries to identify context
availability issues.

Features:
- Command-line logging: `./blender --log-level trace --log "context" `
- Python programmatic control: `temp_override_handle.logging_set(True)`
- C-level API: CTX_temp_override_logging_set/get() functions
- Tracks individual member access
- Uses CLOG logging infrastructure

The logging helps identify which context members are accessed during
temp_override sessions and whether they return valid data, making it
easier to debug operator poll functions that fail with context errors.

Ref !144810
2025-09-19 06:48:07 +00:00
Campbell Barton
c2766d2058 Cleanup: use a typed enum for context data type
Ensure all types are handled in switch statements (see !144810).
2025-09-19 08:21:30 +10:00
Clément Foucault
9d0fe5573b GPU: FrameBuffer: Remove GPUFrameBuffer wrapper opaque type
This is the first step into merging DRW_gpu_wrapper.hh into
the GPU module.

This is very similar to #119825.

Pull Request: https://projects.blender.org/blender/blender/pulls/146372
2025-09-16 17:50:48 +02:00
Sean Kim
7eb84ebd90 Python: Print script errors with CLOG
Currently, when a python error is encountered when rendering the UI, the
corresponding message is printed to stdout / stderr via `PyErr_Print`,
this patch modifies behavior so that a cursory message is also printed
with CLOG

This has the benefit of allowing for testing via
`--debug-exit-on-error`, which aborts Blender when an error message is
printed.

Pull Request: https://projects.blender.org/blender/blender/pulls/146296
2025-09-16 14:34:14 +02:00
Campbell Barton
96d7c5c4cd Cleanup: use braces in mathutils switch statements
Also move error checking before variable declarations.
2025-09-16 14:53:58 +10:00
Oxicid
b2176bfdd7 PyAPI: use the vectorcall protocol for mathutils types
The Vectorcall protocol avoids creating a tuple, and also provides the
number of arguments in advance, providing a ~1.6x speedup for creation
of mathutils types.

Ref !146237
2025-09-16 04:50:38 +00:00
Clément Foucault
680fec144c Cleanup: GPU: Remove prefix 'e' from enum types
_No response_

Pull Request: https://projects.blender.org/blender/blender/pulls/146034
2025-09-15 15:11:02 +02:00
Campbell Barton
62d791c8d6 Cleanup: only use "r_" prefix for return arguments 2025-09-14 23:03:01 +10:00
Namit Bhutani
2110391058 PyAPI: rename undo memory usage method, improve doc-string
Ref !146095
2025-09-12 22:05:53 +10:00
Campbell Barton
84511b8509 Core: add type checks to ID property accessors
Since moving the C++ ID property access macros cast "const" away,
replace with get/set accessors and add asserts that correct types
are used.
2025-09-12 06:29:42 +00:00
Campbell Barton
9dc6a2d7f3 Cleanup: correct misleading PyArg_Parser formatting 2025-09-11 15:20:20 +10:00
Jacques Lucke
c3f49cd24e Shader Nodes: add Python API for inlined shader nodes
This makes the shader node inlining from #141936 available to external renderers
which use the Python API. Existing external renderer add-ons need to be updated
to get the inlined node tree from a material like below instead of using the
original node tree of the material directly.

The main contribution are these three methods: `Material.inline_shader_nodes()`,
`Light.inline_shader_nodes()` and `World.inline_shader_nodes()`.

In theory, there could be an inlining API for node trees more generally, but
some aspects of the inlining are specific to shader nodes currently. For example
the detection of output nodes and implicit input handling. Furthermore, having
the method on e.g. `Material` instead of on the node tree might be more future
proof for the case when we want to store input properties of the material on the
`Material` which are then passed into the shader node tree.

Example from API docs:
```python
import bpy

# The materials should be retrieved from the evaluated object to make sure that
# e.g. edits of Geometry Nodes are applied.
depsgraph = bpy.context.view_layer.depsgraph
ob = bpy.context.active_object
ob_eval = depsgraph.id_eval_get(ob)
material_eval = ob_eval.material_slots[0].material

# Compute the inlined shader nodes.
# Important: Do not loose the reference to this object while accessing the inlined
#   node tree. Otherwise there will be a crash due to a dangling pointer.
inline_shader_nodes = material_eval.inline_shader_nodes()

# Get the actual inlined `bpy.types.NodeTree`.
tree = inline_shader_nodes.node_tree

for node in tree.nodes:
    print(node.name)
```

Pull Request: https://projects.blender.org/blender/blender/pulls/145811
2025-09-11 06:08:30 +02:00
Campbell Barton
1e7587f761 Cleanup: follow our API naming conventions
Minor changes to recently added undo API call.
2025-09-11 12:59:32 +10:00
Brecht Van Lommel
ea0fab21d4 Color Management: Builtin support for ACEScg and Rec.2020 linear spaces
With new functions to convert to/from scene linear in the Python API. ACEScg
in particular is common in USD and MaterialX files.

Pull Request: https://projects.blender.org/blender/blender/pulls/145755
2025-09-05 11:11:32 +02:00
Namit Bhutani
8536fd1223 Sculpt: Compress position undo step data
Stored undo step data for position changes in sculpt mode are now
automatically compressed. Compression is run in background threads,
reducing memory consumption during sculpting sessions while adding
little performance overhead.

For testing and benchmarks, memory usage is now available through
`bpy.app.undo_memory_info()`. Undo memory usage is now tracked by the
existing automated benchmark tests. Some changes to the web benchmark
visualization present the data a bit better.

ZSTD compression is run asynchronously in a backround task pool.
Compression is only blocking if the data is requested immediately for
undo/redo.

Co-authored-by: Hans Goudey <hans@blender.org>

Pull Request: https://projects.blender.org/blender/blender/pulls/141310
2025-09-03 19:15:05 +02:00
Campbell Barton
90764de24d Fix: Python memory leak when deferred property registration fails
Regression in [0], resolve by moving error handling
before creating new objects.

[0] a7b3047cef
2025-09-03 22:10:31 +10:00
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
Campbell Barton
9ff2ccd350 Fix: incorrect handling of 3x3 matrices with RNA get/set callbacks
Thanks to @mont29 for spotting the error.
2025-08-29 22:16:39 +10:00
Campbell Barton
38b13f7aa8 Fix: uninitialized memory use in RNA slice assignment
Resolve run-time error caught by UBSAN & script_pyapi_prop_array test.
2025-08-29 07:27:40 +00:00
Campbell Barton
6c7513b9ac Cleanup: spelling (make check_spelling_*) 2025-08-29 11:27:33 +10:00