Bevel mod: cleanup flags and extra data.
[blender.git] / source / blender / python / intern / bpy_rna_driver.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup pythonintern
19  *
20  * This file defines utility functions that use the RNA API, from PyDrivers.
21  */
22
23 #include <Python.h>
24
25 #include "MEM_guardedalloc.h"
26
27 #include "BLI_utildefines.h"
28
29 #include "BKE_fcurve.h"
30
31 #include "RNA_access.h"
32
33 #include "bpy_rna.h"
34
35 #include "bpy_rna_driver.h"  /* own include */
36
37
38 /**
39  * A version of #driver_get_variable_value which returns a PyObject.
40  */
41 PyObject *pyrna_driver_get_variable_value(
42         struct ChannelDriver *driver, struct DriverTarget *dtar)
43 {
44         PyObject *driver_arg = NULL;
45         PointerRNA ptr;
46         PropertyRNA *prop = NULL;
47         int index;
48
49         if (driver_get_variable_property(driver, dtar, &ptr, &prop, &index)) {
50                 if (prop) {
51                         if (index != -1) {
52                                 if (index < RNA_property_array_length(&ptr, prop) && index >= 0) {
53                                         /* object, property & index */
54                                         driver_arg = pyrna_array_index(&ptr, prop, index);
55                                 }
56                                 else {
57                                         /* out of range, pass */
58                                 }
59                         }
60                         else {
61                                 /* object & property */
62                                 PropertyType type = RNA_property_type(prop);
63                                 if (type == PROP_ENUM) {
64                                         /* Note that enum's are converted to strings by default,
65                                          * we want to avoid that, see: T52213 */
66                                         driver_arg = PyLong_FromLong(RNA_property_enum_get(&ptr, prop));
67                                 }
68                                 else {
69                                         driver_arg = pyrna_prop_to_py(&ptr, prop);
70                                 }
71                         }
72                 }
73                 else {
74                         /* object only */
75                         driver_arg = pyrna_struct_CreatePyObject(&ptr);
76                 }
77         }
78         else {
79                 /* can't resolve path, pass */
80         }
81
82         return driver_arg;
83 }
84
85 PyObject *pyrna_driver_self_from_anim_rna(PathResolvedRNA *anim_rna)
86 {
87         return pyrna_struct_CreatePyObject(&anim_rna->ptr);
88 }
89
90 bool pyrna_driver_is_equal_anim_rna(const PathResolvedRNA *anim_rna, const PyObject *py_anim_rna)
91 {
92         if (BPy_StructRNA_Check(py_anim_rna)) {
93                 const PointerRNA *ptr_a = &anim_rna->ptr;
94                 const PointerRNA *ptr_b = &(((const BPy_StructRNA *)py_anim_rna)->ptr);
95
96                 if ((ptr_a->id.data == ptr_b->id.data) &&
97                     (ptr_a->type == ptr_b->type) &&
98                     (ptr_a->data == ptr_b->data))
99                 {
100                         return true;
101                 }
102         }
103         return false;
104 }