Commit Graph

2992 Commits

Author SHA1 Message Date
Bastien Montagne
6b0ad2e16b Refactor: Replace RNA_POINTER_INVALIDATE RNA/BPY macro usages.
Add `PointerRNA::reset()` and `PointerRNA::invalidate()` utils functions
(both simply reset the PointerRNA data to empty state).

Replace `RNA_POINTER_INVALIDATE` macro by `PointerRNA::invalidate()`.

Follow-up to !134393 and e55d478c64.
2025-02-18 12:43:43 +01:00
Andrej730
48abc7aabc PyAPI: document never none pointer/collection properties
Set never null for pointer props automatically during RNA generation.

Ref: !126755
2025-02-18 11:27:19 +11:00
Campbell Barton
9b0df7c60a Merge branch 'blender-v4.4-release' 2025-02-16 18:32:17 +11:00
Campbell Barton
c80980d10b Fix #125376: Standalone bpy module can crash on exit 2025-02-16 18:27:02 +11:00
Brecht Van Lommel
4786fbe774 Refactor: Remove extern "C" from most headers
The only remaining code in source/blender that must be compiled as C
is now datatoc generated code and the DNA defaults that use designated
initializers.

Pull Request: https://projects.blender.org/blender/blender/pulls/134469
2025-02-13 18:58:08 +01:00
Campbell Barton
e52f85b33c Cleanup: move Python's GIL ensure/release to the function start/end
Avoid errors accessing state without the GIL (#127767) by moving
GIL ensure/release to the star/end of the function body.
2025-02-13 17:56:42 +11:00
Campbell Barton
886e40f136 Merge branch 'blender-v4.4-release' 2025-02-13 17:47:20 +11:00
Campbell Barton
39e23e29fe Cleanup: remove unnecessary call to PyGILState_Ensure
There is no need to aquire the GIL for Python methods.
2025-02-13 17:09:10 +11:00
Campbell Barton
516b39ad22 Merge branch 'blender-v4.4-release' 2025-02-13 16:57:40 +11:00
Campbell Barton
9222f157cb Fix #127767: bpy.app.handlers could run in a read-only state
Reading & restoring RNA "writable" state wasn't working reliably when
Python was called from multiple threads.

- Resolve by acquiring the GIL before calling `pyrna_write_*` functions.
- Assert `pyrna_write_*` has the GIL to prevent this happening again.
- Move duplicate checks from bpy_props.cc into utility functions.
2025-02-13 16:53:27 +11:00
Campbell Barton
e569417798 PyAPI: check registered classes don't depend on registered classes
When registering a class, warn if it's base-classes or sub-classes
are already registered as this is bad practice.

Currently the check only runs when the `--debug-python` argument is used
to avoid overhead on startup.
2025-02-13 13:04:50 +11:00
Campbell Barton
d0ac35eff4 Cleanup: add doxy-sections to bpy_rna.cc 2025-02-13 13:04:50 +11:00
Campbell Barton
c5a65ad9ed Merge branch 'blender-v4.4-release' 2025-02-13 12:01:00 +11:00
Campbell Barton
4c67c78452 PyAPI: correct unregister/register mix up in exception message
Use a shared prefix for messages to avoid copy-paste errors.

Also include class name in the exception if the class can't be
unregistered.
2025-02-13 11:59:57 +11:00
Campbell Barton
754fa4b02f Merge branch 'blender-v4.4-release' 2025-02-13 11:41:14 +11:00
Campbell Barton
09cabadcfb PyAPI: prevent built-in RNA types being unregistered
`bpy.utils.unregister_class(bpy.types.Menu)` would remove `bl_rna`
type information from the menu (also Panels & other built-int types).

Prevent unregistering built-in types since this will only cause problems.
2025-02-13 11:39:17 +11:00
Bastien Montagne
c898c5c575 Merge branch 'blender-v4.4-release' 2025-02-12 13:57:19 +01:00
Bastien Montagne
c46f19a6c2 BPY: Fix weak access to Main data from bpy.data API.
Use `self` parameter to retrieve the actual 'owner' Main.

Previous code was using G_MAIN, which _should_ be fine in current usage
context, but would for sure break if these functions were to be used
from e.g. a temp Main.

Pull Request: https://projects.blender.org/blender/blender/pulls/134440
2025-02-12 13:56:06 +01:00
Campbell Barton
b4b3696d01 Merge branch 'blender-v4.4-release' 2025-02-12 22:39:37 +11:00
Campbell Barton
5638977db2 Docs: clarify reference ownership in bpy_rna.cc 2025-02-12 22:35:39 +11:00
Campbell Barton
8ff9f2b036 Fix assert with debug builds on exit when built as a Python module 2025-02-12 22:17:13 +11:00
Bastien Montagne
7d8dbfcc7b Merge branch 'blender-v4.4-release' 2025-02-12 11:41:43 +01:00
Bastien Montagne
7901dc8216 Refactor: bpy.data.batch_remove: use set instead of ID tags.
Cleaner and more future-proof. Also refactored slightly the code to make
it more readable.

And removed the 'experimental' warning in docs, this should have been
removed a long time ago.
2025-02-12 11:39:05 +01:00
Campbell Barton
968909d57c Cleanup: rename RNA parameter to clarify its meaning
This flag is only used when classes are registered,
avoid confusion by renaming to `PARM_PYFUNC_REGISTER_OPTIONAL`.
2025-02-11 16:31:42 +11:00
Bastien Montagne
87a4c0d3a8 Refactor: Make Library.runtime an allocated pointer.
Move `Library.runtime` to be a pointer, move the related
`LibraryRuntime` struct to `BKE_library.hh`. Similar to e.g.
Mesh.runtime, that pointer is expected to always be valid, and is
allocated at readtime or when creating a new Library ID.

Related smaller changes:
* Write code now uses standard ID writing codepath for Library IDs too.
  * Runtime pointer is reset to nullptr before writing.
* Looking up a library by its absolute path is now handled through a
  dedicated utils, `search_filepath_abs`, instead of using
  `BLI_findstring`.

Pull Request: https://projects.blender.org/blender/blender/pulls/134188
2025-02-07 17:47:16 +01:00
Hans Goudey
4f833b0b5f Refactor: Add StringRef overloads to translation functions
Currently UI code always has to use char pointers when interacting with
the translation system. This makes benefiting from the use C++ strings
and StringRef more difficult. That means we're leaving some type safety
and performance on the table. This PR adds StringRef overloads to the
translation API functions and removes the few calls to `.c_str()` that
are now unnecessary.

Pull Request: https://projects.blender.org/blender/blender/pulls/133887
2025-02-06 17:47:52 +01:00
Bastien Montagne
45f231141d Core: Add info about chain of ancestors (owner data) of a PointerRNA.
The general idea is to store an array of (type, data) pointers of all
PointerRNA ancestors of the current one.

This will help solving cases in our code where the owner (or sometimes
even the owner of the owner) of a random PointerRNA needs to be
accessed. Current solution mainly relies on linear search from the owner
ID, which is sub-optimal at best, and may not even be possible in case a
same data is shared between different owners.

This lead to refactoring quite a bit of existing PointerRNA creation code.

At a high level (i.e. expected usages outside of RNA internals):
* Add `RNA_pointer_create_with_parent` and
  `RNA_pointer_create_id_subdata` to create RNA pointers with
  ancestors info.
* `RNA_id_pointer_create` and `RNA_main_pointer_create` remain
  unchanged, as they should never have ancestors currently.
* Add `RNA_pointer_create_from_ancestor` to re-create a RNA pointer
  from the nth ancestor of another PointerRNA.
* Add basic python API to access this new ancestors data.
* Update internal RNA/bpy code to handle ancestors generation in most
  common generic cases.
  - The most verbose change here is for collection code, as the owner of the
    collection property is now passed around, to allow collection items to get
    a valid ancestors chain.

Internally:
* `PointerRNA` now has an array of `AncestorPointerRNA` data to store
  the ancestors.
* `PointerRNA` now has constructors that take care of setting its data for
  most usual cases, including handling of the ancestor array data.
* Pointer type refining has been fully factorized into a small utils,
  `rna_pointer_refine`, that is now used from all code doing that operation.
* `rna_pointer_inherit_refine` has been replaced by
  `rna_pointer_create_with_ancestors` as the core function taking care of
  creating pointers with valid ancestors info.
  - Its usage outside of `rna_access` has been essentially reduced to custom
    collection lookup callbacks.

Implements #122431.

--------------

Some notes:
* The goal of this commit is _not_ to fully cover all cases creating
  PointerRNA that should also store the ancestors' chain info. It only
  tackles the most generic code paths (in bpyrna and RNA itself mainly).
  The remaining 'missing cases' can be tackle later, as needs be.
* Performances seem to be only marginally affected currently.
* Currently `AncestorPointerRNA` only stores PointerRNA-like data.
  This will help `StructPathFunc` callbacks to more efficiently generate
  an RNA paths when calling e.g. `RNA_path_from_ID_to_property`, but will
  not be enough info to build these paths without these callbacks. And some
  cases may still remain fuzzy. We'd have to add thinks like a `PropertyRNA`
  pointer, and for RNA collection ones, an index and string identifier, to store
  a complete unambiguous 'RNA path' info. This is probably not needed, nor
  worth the extra processing and memory footprint,  for now.

Pull Request: https://projects.blender.org/blender/blender/pulls/122427
2025-02-05 15:45:04 +01:00
Brecht Van Lommel
f5dd13826d Fix: Conflict between bpy.app.translations and PySide
Some versions of PySide give errors when imported together with
bpy.app.translations, because the latter uses a tp_name that does not
exist. The only other tp_name we have with a dot in it (bgl.Buffer)
does exist.

Pull Request: https://projects.blender.org/blender/blender/pulls/134033
2025-02-04 16:03:40 +01:00
Philipp Oeser
2a9b3573f5 Fix #131830: context.property missing prefix to struct
Caused by 59dc67974a

The behavior of `RNA_path_from_ID_to_property` /
`RNA_path_from_ID_to_property_index` is that it only gets the "prefix"
of the struct -- the one we are looking for, the one that is missing
here -- if we feed it the `newptr` since it calls
`RNA_path_from_ID_to_struct(ptr)` to get that prefix.
If we feed it the **ID** pointer it will be empty (makes sense it would
basically be "path from ID to ID"...we need it to be "path from ID to
constraint").

Pull Request: https://projects.blender.org/blender/blender/pulls/133977
2025-02-04 09:37:35 +01:00
Campbell Barton
4cd827870d Cleanup: quiet check_spelling_* targets
Also correct outdated references to `ghash`.
2025-02-02 13:58:34 +11:00
Brecht Van Lommel
27accea53e Cleanup: Various clang-tidy warnings in python
Pull Request: https://projects.blender.org/blender/blender/pulls/133734
2025-01-31 17:03:18 +01:00
Hans Goudey
e79859ab14 Refactor: Use Map instead of GHash for Python translations cache
This makes the code simpler, aligns it with the builtin translations in
messages.cc, and should improve performance as well. My motivation
for the change is to allow returning StringRef/StringRefNull from the
translation functions instead of char pointers.

To test the change I used the Sun Position addon which includes
translations and toggled different languages in the preferences.

Pull Request: https://projects.blender.org/blender/blender/pulls/133827
2025-01-31 14:10:14 +01:00
Hans Goudey
ce5bdecf89 Refactor: Use VectorSet for panels, menus, gizmos, lists, operators
Previously, the global storage of these types either used a GHash or a
blender::Map. VectorSet is preferrable to GHash because it's type safe,
clearer, and faster. It's preferrable to Map because the key doesn't
have to be duplicated and because iteration is faster.

This PR moves these registered types to VectorSet, just like the
node, node socket, and node tree types. Note that none of these types
use RAII for allocation, so freeing is still done manually.

Testing was manually interacting with each of these systems, including
with addons that register their own types.

Pull Request: https://projects.blender.org/blender/blender/pulls/133778
2025-01-30 15:35:00 +01:00
Jonathan Plasse
2459b3a049 PyDoc: add bpy.types.AddonPreferences to registration doc-strings
Ref: !133484
2025-01-28 17:35:42 +11:00
Bastien Montagne
9c237af041 Refactor: RNA: add discrete suffix to RNA_pointer_create.
This is a noisy preliminary step to the 'RNA ancestors' change. The
rename helps clearly tell what each `pointer_create` function does.

Pull Request: https://projects.blender.org/blender/blender/pulls/133475
2025-01-24 16:45:32 +01:00
Campbell Barton
18783c5699 Cleanup: correct RST syntax, spelling & adjust quotes 2025-01-21 16:51:40 +11:00
Hans Goudey
8cb4d6aaab Cleanup: Unused variable with asserts off 2025-01-20 12:37:25 -05:00
Bastien Montagne
2d7068a0d3 Add file_path_map function to bpy.data.
Similar to `bpy.data.user_map`, it returns a mapping of IDs to all the
filepaths they use.

Fairly basic still, may need to be refined with more options to
control which filepaths are included etc.

Mainly intended to make handling of a production blendfile dependencies
more easy.

Also adds some basic testing of the new feature.

Pull Request: https://projects.blender.org/blender/blender/pulls/127252
2025-01-20 17:39:25 +01:00
Campbell Barton
ad7a6495ac PyDoc: use "note" directive for bpy.props
Also minor rewording & adjust indentation.
2025-01-20 16:34:47 +11:00
Campbell Barton
4507c9fcc2 PyDoc: add bpy.types.PropertyGroup to registration doc-strings
Ref !133233

Co-authored-by: Jonathan Plasse <jonathan.plasse@eilbek-research.de>
2025-01-20 16:12:11 +11:00
Campbell Barton
ddae7cd966 PyAPI: expose bpy.types.bpy_prop_collection_idprop
Expose the IDProperty sub-class of `bpy.types.bpy_prop_collection`,
needed for API doc generation but should have been exposed for
completeness.
2025-01-20 15:47:08 +11:00
Brecht Van Lommel
a6b293daac Python Module: Bundle VFX library python bindings
When calling bpy.utils.expose_bundled_modules(), these modules are
added to sys.path.

This provides a solution/workaround to two problems:
* Using bpy together with packages like usd-core is problematic. Besides
  crashing due to C++ symbol conflicts, it's just impossible to import
  different versions of the same module, or to have distinct environment
  variables for both. (#127132)
* Blender add-ons using these VFX modules do not currently work with
  the bpy module.

This adds about 15MB to the bpy package.

Pull Request: https://projects.blender.org/blender/blender/pulls/133082
2025-01-17 10:13:31 +01:00
Campbell Barton
481a8b67d4 PyAPI: add bpy.app.portable, derived from WITH_INSTALL_PORTABLE
Support differentiating between portable & system installations,
useful to properly locate relative paths which would not work
on system installations.

Ref !133143
2025-01-16 21:07:29 +11:00
Sybren A. Stüvel
59585cf978 Cleanup: document difference between bpy.app.version and .version_file
Clarify the meaning `bpy.app.version_file`, by being more explicit in the
description of `bpy.app.version` and their differences.

### Old:

`bpy.app.version`
: The Blender version as a tuple of 3 numbers. eg. (2, 83, 1)

`bpy.app.version_file`
: The Blender version, as a tuple, last used to save a .blend file, compatible with ``bpy.data.version``. This value should be used for handling compatibility changes between Blender versions

### New:

`bpy.app.version`
: The Blender version as a tuple of 3 numbers (major, minor, micro). eg. (4, 3, 1)

`bpy.app.version_file`
: The Blender File version, as a tuple of 3 numbers (major, minor, file sub-version), that will be used to save a .blend file. The last item in this tuple indicates the file sub-version, which is different from the release micro version (the last item of the `bpy.app.version` tuple). The file sub-version can be incremented multiple times while a Blender version is under development. This value is, and should be, used for handling compatibility changes between Blender versions

Pull Request: https://projects.blender.org/blender/blender/pulls/133142
2025-01-16 11:06:59 +01:00
Bastien Montagne
484fa519fb Fix some issues with bpy.data.user_map implementation.
* In case a subset of IDs is given, it was not checked that all items
  are ID data.
* In case of error while processing the subset, there could be some
  python data 'leak' (the `user_map` would not be unreferenced).
* Improve performances in case there are many duplicates in given subset
  iterable of IDs, by not processing the duplicates.

Discovered in !127252 review.
2025-01-15 11:35:11 +01:00
Bastien Montagne
be936025ea Cleanup: Replace some asserts by static asserts. 2025-01-08 15:50:52 +01:00
Brecht Van Lommel
920e709069 Refactor: Make header files more clangd and clang-tidy friendly
When using clangd or running clang-tidy on headers there are
currently many errors. These are noisy in IDEs, make auto fixes
impossible, and break features like code completion, refactoring
and navigation.

This makes source/blender headers work by themselves, which is
generally the goal anyway. But #includes and forward declarations
were often incomplete.

* Add #includes and forward declarations
* Add IWYU pragma: export in a few places
* Remove some unused #includes (but there are many more)
* Tweak ShaderCreateInfo macros to work better with clangd

Some types of headers still have errors, these could be fixed or
worked around with more investigation. Mostly preprocessor
template headers like NOD_static_types.h.

Note that that disabling WITH_UNITY_BUILD is required for clangd to
work properly, otherwise compile_commands.json does not contain
the information for the relevant source files.

For more details see the developer docs:
https://developer.blender.org/docs/handbook/tooling/clangd/

Pull Request: https://projects.blender.org/blender/blender/pulls/132608
2025-01-07 12:39:13 +01:00
nutti
d42a9ae406 PyDoc: use argument "type" to list valid types for class registration
Ref: !132482
2025-01-07 20:09:35 +11:00
Campbell Barton
a404f521a3 Cleanup: remove typedef struct & function(void) in Python declarations 2025-01-07 11:27:20 +11:00
Campbell Barton
724e4fe937 Cleanup: move bgl header to C++ 2025-01-07 11:16:14 +11:00