documentation - brief descriptions for bpy api files.
[blender.git] / source / blender / python / intern / bpy_rna_anim.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  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_rna_anim.c
24  *  \ingroup pythonintern
25  *
26  * This file defines the animation related methods used in bpy_rna.c
27  */
28
29 #include <Python.h>
30 #include <float.h> /* FLT_MIN/MAX */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_string.h"
35
36 #include "DNA_scene_types.h"
37 #include "DNA_anim_types.h"
38 #include "ED_keyframing.h"
39
40 #include "BKE_report.h"
41 #include "BKE_context.h"
42 #include "BKE_animsys.h"
43 #include "BKE_fcurve.h"
44
45 #include "RNA_access.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "bpy_rna.h"
51 #include "bpy_util.h"
52 #include "bpy_rna_anim.h"
53
54 #define TRUE 1
55 #define FALSE 0
56
57 /* for keyframes and drivers */
58 static int pyrna_struct_anim_args_parse(PointerRNA *ptr, const char *error_prefix, const char *path,
59                                         const char **path_full, int *index)
60 {
61         const int is_idbase= RNA_struct_is_ID(ptr->type);
62         PropertyRNA *prop;
63         PointerRNA r_ptr;
64
65         if (ptr->data==NULL) {
66                 PyErr_Format(PyExc_TypeError,
67                              "%.200s this struct has no data, can't be animated",
68                              error_prefix);
69                 return -1;
70         }
71
72         /* full paths can only be given from ID base */
73         if (is_idbase) {
74                 int r_index= -1;
75                 if (RNA_path_resolve_full(ptr, path, &r_ptr, &prop, &r_index)==0) {
76                         prop= NULL;
77                 }
78                 else if (r_index != -1) {
79                         PyErr_Format(PyExc_ValueError,
80                                      "%.200s path includes index, must be a separate argument",
81                                      error_prefix, path);
82                         return -1;
83                 }
84                 else if (ptr->id.data != r_ptr.id.data) {
85                         PyErr_Format(PyExc_ValueError,
86                                      "%.200s path spans ID blocks",
87                                      error_prefix, path);
88                         return -1;
89                 }
90         }
91         else {
92                 prop= RNA_struct_find_property(ptr, path);
93                 r_ptr= *ptr;
94         }
95
96         if (prop==NULL) {
97                 PyErr_Format(PyExc_TypeError,
98                              "%.200s property \"%s\" not found",
99                              error_prefix, path);
100                 return -1;
101         }
102
103         if (!RNA_property_animateable(&r_ptr, prop)) {
104                 PyErr_Format(PyExc_TypeError,
105                              "%.200s property \"%s\" not animatable",
106                              error_prefix, path);
107                 return -1;
108         }
109
110         if (RNA_property_array_check(prop) == 0) {
111                 if ((*index) == -1) {
112                         *index= 0;
113                 }
114                 else {
115                         PyErr_Format(PyExc_TypeError,
116                                      "%.200s index %d was given while property \"%s\" is not an array",
117                                      error_prefix, *index, path);
118                         return -1;
119                 }
120         }
121         else {
122                 int array_len= RNA_property_array_length(&r_ptr, prop);
123                 if ((*index) < -1 || (*index) >= array_len) {
124                         PyErr_Format(PyExc_TypeError,
125                                      "%.200s index out of range \"%s\", given %d, array length is %d",
126                                      error_prefix, path, *index, array_len);
127                         return -1;
128                 }
129         }
130
131         if (is_idbase) {
132                 *path_full= BLI_strdup(path);
133         }
134         else {
135                 *path_full= RNA_path_from_ID_to_property(&r_ptr, prop);
136
137                 if (*path_full==NULL) {
138                         PyErr_Format(PyExc_TypeError,
139                                      "%.200s could not make path to \"%s\"",
140                                      error_prefix, path);
141                         return -1;
142                 }
143         }
144
145         return 0;
146 }
147
148 /* internal use for insert and delete */
149 static int pyrna_struct_keyframe_parse(PointerRNA *ptr, PyObject *args, PyObject *kw, const char *parse_str, const char *error_prefix,
150                                        const char **path_full, int *index, float *cfra, const char **group_name) /* return values */
151 {
152         static const char *kwlist[]= {"data_path", "index", "frame", "group", NULL};
153         const char *path;
154
155         /* note, parse_str MUST start with 's|ifs' */
156         if (!PyArg_ParseTupleAndKeywords(args, kw, parse_str, (char **)kwlist, &path, index, cfra, group_name))
157                 return -1;
158
159         if (pyrna_struct_anim_args_parse(ptr, error_prefix, path, path_full, index) < 0)
160                 return -1;
161
162         if (*cfra==FLT_MAX)
163                 *cfra= CTX_data_scene(BPy_GetContext())->r.cfra;
164
165         return 0; /* success */
166 }
167
168 char pyrna_struct_keyframe_insert_doc[] =
169 ".. method:: keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group=\"\")\n"
170 "\n"
171 "   Insert a keyframe on the property given, adding fcurves and animation data when necessary.\n"
172 "\n"
173 "   :arg data_path: path to the property to key, analogous to the fcurve's data path.\n"
174 "   :type data_path: string\n"
175 "   :arg index: array index of the property to key. Defaults to -1 which will key all indices or a single channel if the property is not an array.\n"
176 "   :type index: int\n"
177 "   :arg frame: The frame on which the keyframe is inserted, defaulting to the current frame.\n"
178 "   :type frame: float\n"
179 "   :arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.\n"
180 "   :type group: str\n"
181 "   :return: Success of keyframe insertion.\n"
182 "   :rtype: boolean\n"
183 ;
184 PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA *self, PyObject *args, PyObject *kw)
185 {
186         /* args, pyrna_struct_keyframe_parse handles these */
187         const char *path_full= NULL;
188         int index= -1;
189         float cfra= FLT_MAX;
190         const char *group_name= NULL;
191
192         PYRNA_STRUCT_CHECK_OBJ(self);
193
194         if (pyrna_struct_keyframe_parse(&self->ptr, args, kw,
195                                        "s|ifs:bpy_struct.keyframe_insert()", "bpy_struct.keyframe_insert()",
196                                        &path_full, &index, &cfra, &group_name) == -1)
197         {
198                 return NULL;
199         }
200         else {
201                 short result;
202                 ReportList reports;
203
204                 BKE_reports_init(&reports, RPT_STORE);
205
206                 result= insert_keyframe(&reports, (ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0);
207                 MEM_freeN((void *)path_full);
208
209                 if (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE) == -1)
210                         return NULL;
211
212                 return PyBool_FromLong(result);
213         }
214 }
215
216 char pyrna_struct_keyframe_delete_doc[] =
217 ".. method:: keyframe_delete(data_path, index=-1, frame=bpy.context.scene.frame_current, group=\"\")\n"
218 "\n"
219 "   Remove a keyframe from this properties fcurve.\n"
220 "\n"
221 "   :arg data_path: path to the property to remove a key, analogous to the fcurve's data path.\n"
222 "   :type data_path: string\n"
223 "   :arg index: array index of the property to remove a key. Defaults to -1 removing all indices or a single channel if the property is not an array.\n"
224 "   :type index: int\n"
225 "   :arg frame: The frame on which the keyframe is deleted, defaulting to the current frame.\n"
226 "   :type frame: float\n"
227 "   :arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.\n"
228 "   :type group: str\n"
229 "   :return: Success of keyframe deleation.\n"
230 "   :rtype: boolean\n"
231 ;
232 PyObject *pyrna_struct_keyframe_delete(BPy_StructRNA *self, PyObject *args, PyObject *kw)
233 {
234         /* args, pyrna_struct_keyframe_parse handles these */
235         const char *path_full= NULL;
236         int index= -1;
237         float cfra= FLT_MAX;
238         const char *group_name= NULL;
239
240         PYRNA_STRUCT_CHECK_OBJ(self);
241
242         if (pyrna_struct_keyframe_parse(&self->ptr, args, kw,
243                                        "s|ifs:bpy_struct.keyframe_delete()",
244                                        "bpy_struct.keyframe_insert()",
245                                        &path_full, &index, &cfra, &group_name) == -1)
246         {
247                 return NULL;
248         }
249         else {
250                 short result;
251                 ReportList reports;
252
253                 BKE_reports_init(&reports, RPT_STORE);
254
255                 result= delete_keyframe(&reports, (ID *)self->ptr.id.data, NULL, group_name, path_full, index, cfra, 0);
256                 MEM_freeN((void *)path_full);
257
258                 if (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE) == -1)
259                         return NULL;
260
261                 return PyBool_FromLong(result);
262         }
263
264 }
265
266 char pyrna_struct_driver_add_doc[] =
267 ".. method:: driver_add(path, index=-1)\n"
268 "\n"
269 "   Adds driver(s) to the given property\n"
270 "\n"
271 "   :arg path: path to the property to drive, analogous to the fcurve's data path.\n"
272 "   :type path: string\n"
273 "   :arg index: array index of the property drive. Defaults to -1 for all indices or a single channel if the property is not an array.\n"
274 "   :type index: int\n"
275 "   :return: The driver(s) added.\n"
276 "   :rtype: :class:`bpy.types.FCurve` or list if index is -1 with an array property.\n"
277 ;
278 PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args)
279 {
280         const char *path, *path_full;
281         int index= -1;
282
283         PYRNA_STRUCT_CHECK_OBJ(self);
284
285         if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index))
286                 return NULL;
287
288         if (pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_add():", path, &path_full, &index) < 0) {
289                 return NULL;
290         }
291         else {
292                 PyObject *ret= NULL;
293                 ReportList reports;
294                 int result;
295
296                 BKE_reports_init(&reports, RPT_STORE);
297
298                 result= ANIM_add_driver(&reports, (ID *)self->ptr.id.data, path_full, index, 0, DRIVER_TYPE_PYTHON);
299
300                 if (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE) == -1)
301                         return NULL;
302
303                 if (result) {
304                         ID *id= self->ptr.id.data;
305                         AnimData *adt= BKE_animdata_from_id(id);
306                         FCurve *fcu;
307
308                         PointerRNA tptr;
309                         PyObject *item;
310
311                         if (index == -1) { /* all, use a list */
312                                 int i= 0;
313                                 ret= PyList_New(0);
314                                 while ((fcu= list_find_fcurve(&adt->drivers, path_full, i++))) {
315                                         RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr);
316                                         item= pyrna_struct_CreatePyObject(&tptr);
317                                         PyList_Append(ret, item);
318                                         Py_DECREF(item);
319                                 }
320                         }
321                         else {
322                                 fcu= list_find_fcurve(&adt->drivers, path_full, index);
323                                 RNA_pointer_create(id, &RNA_FCurve, fcu, &tptr);
324                                 ret= pyrna_struct_CreatePyObject(&tptr);
325                         }
326                         
327                         WM_event_add_notifier(BPy_GetContext(), NC_ANIMATION|ND_FCURVES_ORDER, NULL);
328                 }
329                 else {
330                         /* XXX, should be handled by reports, */
331                         PyErr_SetString(PyExc_TypeError, "bpy_struct.driver_add(): failed because of an internal error");
332                         return NULL;
333                 }
334
335                 MEM_freeN((void *)path_full);
336
337                 return ret;
338         }
339 }
340
341
342 char pyrna_struct_driver_remove_doc[] =
343 ".. method:: driver_remove(path, index=-1)\n"
344 "\n"
345 "   Remove driver(s) from the given property\n"
346 "\n"
347 "   :arg path: path to the property to drive, analogous to the fcurve's data path.\n"
348 "   :type path: string\n"
349 "   :arg index: array index of the property drive. Defaults to -1 for all indices or a single channel if the property is not an array.\n"
350 "   :type index: int\n"
351 "   :return: Success of driver removal.\n"
352 "   :rtype: boolean\n"
353 ;
354 PyObject *pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args)
355 {
356         const char *path, *path_full;
357         int index= -1;
358
359         PYRNA_STRUCT_CHECK_OBJ(self);
360
361         if (!PyArg_ParseTuple(args, "s|i:driver_remove", &path, &index))
362                 return NULL;
363
364         if (pyrna_struct_anim_args_parse(&self->ptr, "bpy_struct.driver_remove():", path, &path_full, &index) < 0) {
365                 return NULL;
366         }
367         else {
368                 short result;
369                 ReportList reports;
370
371                 BKE_reports_init(&reports, RPT_STORE);
372
373                 result= ANIM_remove_driver(&reports, (ID *)self->ptr.id.data, path_full, index, 0);
374
375                 MEM_freeN((void *)path_full);
376
377                 if (BPy_reports_to_error(&reports, PyExc_RuntimeError, TRUE) == -1)
378                         return NULL;
379                 
380                 WM_event_add_notifier(BPy_GetContext(), NC_ANIMATION|ND_FCURVES_ORDER, NULL);
381
382                 return PyBool_FromLong(result);
383         }
384 }