Files
test2/source/blender/python/intern/bpy_rna_driver.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.3 KiB
C++
Raw Permalink Normal View History

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup pythonintern
*
* This file defines utility functions that use the RNA API, from PyDrivers.
*/
#include <Python.h>
#include "DNA_anim_types.h"
#include "BKE_fcurve_driver.h"
#include "RNA_access.hh"
#include "bpy_rna.hh"
#include "bpy_rna_driver.hh" /* own include */
PyObject *pyrna_driver_get_variable_value(const AnimationEvalContext *anim_eval_context,
ChannelDriver *driver,
DriverVar *dvar,
DriverTarget *dtar)
{
PointerRNA ptr;
PropertyRNA *prop = nullptr;
int index;
switch (driver_get_variable_property(
anim_eval_context, driver, dvar, dtar, true, &ptr, &prop, &index))
{
case DRIVER_VAR_PROPERTY_SUCCESS:
/* object only */
if (!prop) {
return pyrna_struct_CreatePyObject(&ptr);
}
/* object, property & index */
if (index >= 0) {
return pyrna_array_index(&ptr, prop, index);
}
/* object & property (enum) */
if (RNA_property_type(prop) == PROP_ENUM) {
/* Note that enum's are converted to strings by default,
* we want to avoid that, see: #52213 */
return PyLong_FromLong(RNA_property_enum_get(&ptr, prop));
}
/* object & property */
return pyrna_prop_to_py(&ptr, prop);
case DRIVER_VAR_PROPERTY_FALLBACK:
return PyFloat_FromDouble(dtar->fallback_value);
case DRIVER_VAR_PROPERTY_INVALID:
case DRIVER_VAR_PROPERTY_INVALID_INDEX:
/* can't resolve path, pass */
return nullptr;
}
return nullptr;
}
PyObject *pyrna_driver_self_from_anim_rna(PathResolvedRNA *anim_rna)
{
return pyrna_struct_CreatePyObject(&anim_rna->ptr);
}
bool pyrna_driver_is_equal_anim_rna(const PathResolvedRNA *anim_rna, const PyObject *py_anim_rna)
{
if (BPy_StructRNA_Check(py_anim_rna)) {
const PointerRNA *ptr_a = &anim_rna->ptr;
RNA: Make the `PointerRNA` struct non-trivial. For now, PointerRNA is made non-trivial by giving explicit default values to its members. Besides of BPY python binding code, the change is relatively trivial. The main change (besides the creation/deletion part) is the replacement of `memset` by zero-initialized assignment (using `{}`). makesrna required changes are quite small too. The big piece of this PR is the refactor of the BPY RNA code. It essentially brings back allocation and deletion of the BPy_StructRNA, BPy_Pointer etc. python objects into 'cannonical process', using `__new__`, and `__init__` callbacks (and there matching CAPI functions). Existing code was doing very low-level manipulations to create these data, which is not really easy to understand, and AFAICT incompatible with handling C++ data that needs to be constructed and destructed. Unfortunately, similar change in destruction code (using `__del__` and matching `tp_finalize` CAPI callback) is not possible, because of technical low-level implementation details in CPython (see [1] for details). `std::optional` pointer management is used to encapsulate PointerRNA data. This allows to keep control on _when_ actual RNA creation is done, and to have a safe destruction in `tp_dealloc` callbacks. Note that a critical change in Blender's Python API will be that classes inherinting from `bpy_struct` etc. will now have to properly call the base class `__new__` and/or `__init__`if they define them. Implements #122431. [1] https://discuss.python.org/t/cpython-usage-of-tp-finalize-in-c-defined-static-types-with-no-custom-tp-dealloc/64100
2024-10-30 15:08:37 +01:00
const PointerRNA *ptr_b = &reinterpret_cast<const BPy_StructRNA *>(py_anim_rna)->ptr.value();
if ((ptr_a->owner_id == ptr_b->owner_id) && (ptr_a->type == ptr_b->type) &&
(ptr_a->data == ptr_b->data))
{
return true;
}
}
return false;
}