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