Refactor: Clarify code around scheduling composite nodes
[blender.git] / source / blender / python / intern / bpy_rna_driver.cc
blob93bce0903cebee662a24dded15f940e5ab5fad4c
1 /* SPDX-FileCopyrightText: 2023 Blender Authors
3 * SPDX-License-Identifier: GPL-2.0-or-later */
5 /** \file
6 * \ingroup pythonintern
8 * This file defines utility functions that use the RNA API, from PyDrivers.
9 */
11 #include <Python.h>
13 #include "DNA_anim_types.h"
15 #include "BKE_fcurve_driver.h"
17 #include "RNA_access.hh"
19 #include "bpy_rna.hh"
21 #include "bpy_rna_driver.hh" /* own include */
23 PyObject *pyrna_driver_get_variable_value(const AnimationEvalContext *anim_eval_context,
24 ChannelDriver *driver,
25 DriverVar *dvar,
26 DriverTarget *dtar)
28 PointerRNA ptr;
29 PropertyRNA *prop = nullptr;
30 int index;
32 switch (driver_get_variable_property(
33 anim_eval_context, driver, dvar, dtar, true, &ptr, &prop, &index))
35 case DRIVER_VAR_PROPERTY_SUCCESS:
36 /* object only */
37 if (!prop) {
38 return pyrna_struct_CreatePyObject(&ptr);
41 /* object, property & index */
42 if (index >= 0) {
43 return pyrna_array_index(&ptr, prop, index);
46 /* object & property (enum) */
47 if (RNA_property_type(prop) == PROP_ENUM) {
48 /* Note that enum's are converted to strings by default,
49 * we want to avoid that, see: #52213 */
50 return PyLong_FromLong(RNA_property_enum_get(&ptr, prop));
53 /* object & property */
54 return pyrna_prop_to_py(&ptr, prop);
56 case DRIVER_VAR_PROPERTY_FALLBACK:
57 return PyFloat_FromDouble(dtar->fallback_value);
59 case DRIVER_VAR_PROPERTY_INVALID:
60 case DRIVER_VAR_PROPERTY_INVALID_INDEX:
61 /* can't resolve path, pass */
62 return nullptr;
65 return nullptr;
68 PyObject *pyrna_driver_self_from_anim_rna(PathResolvedRNA *anim_rna)
70 return pyrna_struct_CreatePyObject(&anim_rna->ptr);
73 bool pyrna_driver_is_equal_anim_rna(const PathResolvedRNA *anim_rna, const PyObject *py_anim_rna)
75 if (BPy_StructRNA_Check(py_anim_rna)) {
76 const PointerRNA *ptr_a = &anim_rna->ptr;
77 const PointerRNA *ptr_b = &reinterpret_cast<const BPy_StructRNA *>(py_anim_rna)->ptr.value();
79 if ((ptr_a->owner_id == ptr_b->owner_id) && (ptr_a->type == ptr_b->type) &&
80 (ptr_a->data == ptr_b->data))
82 return true;
85 return false;