482508a8d857e340d80c8c147e49a00d108981f1
[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                                 driver_arg = pyrna_prop_to_py(&ptr, prop);
67                         }
68                 }
69                 else {
70                         /* object only */
71                         driver_arg = pyrna_struct_CreatePyObject(&ptr);
72                 }
73         }
74         else {
75                 /* can't resolve path, pass */
76         }
77
78         return driver_arg;
79 }