Final merge of HEAD (bf-blender) into the orange branch.
[blender.git] / source / blender / python / api2_2x / NLA.c
1 /* 
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * This is a new part of Blender.
27  *
28  * Contributor(s): Joseph Gilbert
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 */
32
33 #include "NLA.h" /*This must come first*/
34
35 #include "BKE_action.h"
36 #include "BKE_global.h"
37 #include "BKE_main.h"
38 #include "BKE_library.h"
39 #include "BLI_blenlib.h"
40 #include "Object.h"
41 #include "Ipo.h"
42 #include "gen_utils.h"
43
44 /*****************************************************************************/
45 /* Python API function prototypes for the NLA module.                    */
46 /*****************************************************************************/
47 static PyObject *M_NLA_NewAction( PyObject * self, PyObject * args );
48 static PyObject *M_NLA_CopyAction( PyObject * self, PyObject * args );
49 static PyObject *M_NLA_GetActions( PyObject * self );
50
51 /*****************************************************************************/
52 /* The following string definitions are used for documentation strings.    */
53 /* In Python these will be written to the console when doing a           */
54 /* Blender.Armature.NLA.__doc__                                  */
55 /*****************************************************************************/
56 char M_NLA_doc[] =
57         "The Blender NLA module -This module provides control over  Armature keyframing in Blender.";
58 char M_NLA_NewAction_doc[] =
59         "(name) - Create new action for linking to an object.";
60 char M_NLA_CopyAction_doc[] = "(name) - Copy action and return copy.";
61 char M_NLA_GetActions_doc[] = "(name) - Returns a dictionary of actions.";
62
63 /*****************************************************************************/
64 /* Python method structure definition for Blender.Armature.NLA module:   */
65 /*****************************************************************************/
66 struct PyMethodDef M_NLA_methods[] = {
67         {"NewAction", ( PyCFunction ) M_NLA_NewAction, METH_VARARGS,
68          M_NLA_NewAction_doc},
69         {"CopyAction", ( PyCFunction ) M_NLA_CopyAction, METH_VARARGS,
70          M_NLA_CopyAction_doc},
71         {"GetActions", ( PyCFunction ) M_NLA_GetActions, METH_NOARGS,
72          M_NLA_GetActions_doc},
73         {NULL, NULL, 0, NULL}
74 };
75 /*****************************************************************************/
76 /* Python BPy_Action methods declarations:                              */
77 /*****************************************************************************/
78 static PyObject *Action_getName( BPy_Action * self );
79 static PyObject *Action_setName( BPy_Action * self, PyObject * args );
80 static PyObject *Action_setActive( BPy_Action * self, PyObject * args );
81 static PyObject *Action_getChannelIpo( BPy_Action * self, PyObject * args );
82 static PyObject *Action_verifyChannel( BPy_Action * self, PyObject * args );
83 static PyObject *Action_removeChannel( BPy_Action * self, PyObject * args );
84 static PyObject *Action_getAllChannelIpos( BPy_Action * self );
85
86 /*****************************************************************************/
87 /* Python BPy_Action methods table:                                      */
88 /*****************************************************************************/
89 static PyMethodDef BPy_Action_methods[] = {
90         /* name, method, flags, doc */
91         {"getName", ( PyCFunction ) Action_getName, METH_NOARGS,
92          "() - return Action name"},
93         {"setName", ( PyCFunction ) Action_setName, METH_VARARGS,
94          "(str) - rename Action"},
95         {"setActive", ( PyCFunction ) Action_setActive, METH_VARARGS,
96          "(str) -set this action as the active action for an object"},
97         {"getChannelIpo", ( PyCFunction ) Action_getChannelIpo, METH_VARARGS,
98          "(str) -get the Ipo from a named action channel in this action"},
99         {"verifyChannel", ( PyCFunction ) Action_verifyChannel, METH_VARARGS,
100          "(str) -verify the channel in this action"},
101         {"removeChannel", ( PyCFunction ) Action_removeChannel, METH_VARARGS,
102          "(str) -remove the channel from the action"},
103         {"getAllChannelIpos", ( PyCFunction ) Action_getAllChannelIpos,
104          METH_NOARGS,
105          "() - Return a dict of (name:ipo)-keys containing each channel in the object's action"},
106         {NULL, NULL, 0, NULL}
107 };
108
109 /*****************************************************************************/
110 /* Python TypeAction callback function prototypes:                          */
111 /*****************************************************************************/
112 static void Action_dealloc( BPy_Action * bone );
113 static PyObject *Action_getAttr( BPy_Action * bone, char *name );
114 static int Action_setAttr( BPy_Action * bone, char *name, PyObject * v );
115 static PyObject *Action_repr( BPy_Action * bone );
116
117 /*****************************************************************************/
118 /* Python TypeAction structure definition:                               */
119 /*****************************************************************************/
120 PyTypeObject Action_Type = {
121         PyObject_HEAD_INIT( NULL ) 
122         0,      /* ob_size */
123         "Blender Action",       /* tp_name */
124         sizeof( BPy_Action ),   /* tp_basicsize */
125         0,                      /* tp_itemsize */
126         /* methods */
127         ( destructor ) Action_dealloc,  /* tp_dealloc */
128         0,                      /* tp_print */
129         ( getattrfunc ) Action_getAttr, /* tp_getattr */
130         ( setattrfunc ) Action_setAttr, /* tp_setattr */
131         0,                      /* tp_compare */
132         ( reprfunc ) Action_repr,       /* tp_repr */
133         0,                      /* tp_as_number */
134         0,                      /* tp_as_sequence */
135         0,                      /* tp_as_mapping */
136         0,                      /* tp_as_hash */
137         0, 0, 0, 0, 0, 0,
138         0,                      /* tp_doc */
139         0, 0, 0, 0, 0, 0,
140         BPy_Action_methods,     /* tp_methods */
141         0,                      /* tp_members */
142 };
143
144 //-------------------------------------------------------------------------
145 static PyObject *M_NLA_NewAction( PyObject * self, PyObject * args )
146 {
147         char *name_str = "DefaultAction";
148         BPy_Action *py_action = NULL;   /* for Action Data object wrapper in Python */
149         bAction *bl_action = NULL;      /* for actual Action Data we create in Blender */
150
151         if( !PyArg_ParseTuple( args, "|s", &name_str ) ) {
152                 EXPP_ReturnPyObjError( PyExc_AttributeError,
153                                        "expected string or nothing" );
154                 return NULL;
155         }
156         //Create new action globally
157         bl_action = alloc_libblock( &G.main->action, ID_AC, name_str );
158         bl_action->id.flag |= LIB_FAKEUSER;
159         bl_action->id.us++;
160
161         // now create the wrapper obj in Python
162         if( bl_action )
163                 py_action =
164                         ( BPy_Action * ) PyObject_NEW( BPy_Action,
165                                                        &Action_Type );
166         else {
167                 EXPP_ReturnPyObjError( PyExc_RuntimeError,
168                                        "couldn't create Action Data in Blender" );
169                 return NULL;
170         }
171
172         if( py_action == NULL ) {
173                 EXPP_ReturnPyObjError( PyExc_MemoryError,
174                                        "couldn't create Action Data object" );
175                 return NULL;
176         }
177
178         py_action->action = bl_action;  // link Python action wrapper with Blender Action
179
180         Py_INCREF( py_action );
181         return ( PyObject * ) py_action;
182 }
183
184 static PyObject *M_NLA_CopyAction( PyObject * self, PyObject * args )
185 {
186         BPy_Action *py_action = NULL;
187         bAction *copyAction = NULL;
188
189         if( !PyArg_ParseTuple( args, "O!", &Action_Type, &py_action ) ) {
190                 EXPP_ReturnPyObjError( PyExc_AttributeError,
191                                        "expected python action type" );
192                 return NULL;
193         }
194         copyAction = copy_action( py_action->action );
195         return Action_CreatePyObject( copyAction );
196 }
197
198 static PyObject *M_NLA_GetActions( PyObject * self )
199 {
200         PyObject *dict = PyDict_New(  );
201         bAction *action = NULL;
202
203         for( action = G.main->action.first; action; action = action->id.next ) {
204                 PyObject *py_action = Action_CreatePyObject( action );
205                 if( py_action ) {
206                         // Insert dict entry using the bone name as key
207                         if( PyDict_SetItemString
208                             ( dict, action->id.name + 2, py_action ) != 0 ) {
209                                 Py_DECREF( py_action );
210                                 Py_DECREF( dict );
211
212                                 return EXPP_ReturnPyObjError
213                                         ( PyExc_RuntimeError,
214                                           "NLA_GetActions: couldn't set dict item" );
215                         }
216                         Py_DECREF( py_action );
217                 } else {
218                         Py_DECREF( dict );
219                         return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
220                                                         "NLA_GetActions: could not create Action object" ) );
221                 }
222         }
223         return dict;
224 }
225
226 /*****************************************************************************/
227 /* Function:    NLA_Init                                                 */
228 /*****************************************************************************/
229 PyObject *NLA_Init( void )
230 {
231         PyObject *submodule;
232
233         Action_Type.ob_type = &PyType_Type;
234
235         submodule = Py_InitModule3( "Blender.Armature.NLA",
236                                     M_NLA_methods, M_NLA_doc );
237
238         return ( submodule );
239 }
240
241 //----------------------------------------------------------------------
242 static PyObject *Action_getName( BPy_Action * self )
243 {
244         PyObject *attr = NULL;
245
246         if( !self->action )
247                 ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
248                                          "couldn't get attribute from a NULL action" ) );
249
250         attr = PyString_FromString( self->action->id.name + 2 );
251
252         if( attr )
253                 return attr;
254
255         return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
256                                         "couldn't get Action.name attribute" ) );
257 }
258
259 //----------------------------------------------------------------------
260 static PyObject *Action_setName( BPy_Action * self, PyObject * args )
261 {
262         char *name;
263
264         if( !self->action )
265                 ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
266                                          "couldn't get attribute from a NULL action" ) );
267
268         if( !PyArg_ParseTuple( args, "s", &name ) )
269                 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
270                                                 "expected string argument" ) );
271
272         //change name
273         strcpy( self->action->id.name + 2, name );
274
275         Py_INCREF( Py_None );
276         return Py_None;
277 }
278
279 static PyObject *Action_setActive( BPy_Action * self, PyObject * args )
280 {
281         BPy_Object *object;
282
283         if( !self->action )
284                 ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
285                                          "couldn't get attribute from a NULL action" ) );
286
287         if( !PyArg_ParseTuple( args, "O!", &Object_Type, &object ) )
288                 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
289                                                 "expected python object argument" ) );
290
291         if( object->object->type != OB_ARMATURE ) {
292                 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
293                                                 "object not of type armature" ) );
294         }
295         //set the active action to object
296         object->object->action = self->action;
297
298         Py_INCREF( Py_None );
299         return Py_None;
300 }
301
302 static PyObject *Action_getChannelIpo( BPy_Action * self, PyObject * args )
303 {
304         char *chanName;
305         bActionChannel *chan;
306
307         if( !PyArg_ParseTuple( args, "s", &chanName ) ) {
308                 EXPP_ReturnPyObjError( PyExc_AttributeError,
309                                        "string expected" );
310                 return NULL;
311         }
312
313         chan = get_action_channel( self->action, chanName );
314         if( chan == NULL ) {
315                 EXPP_ReturnPyObjError( PyExc_AttributeError,
316                                        "no channel with that name..." );
317                 return NULL;
318         }
319         //return IPO
320         return Ipo_CreatePyObject( chan->ipo );
321 }
322
323 //----------------------------------------------------------------------
324 static PyObject *Action_verifyChannel( BPy_Action * self, PyObject * args )
325 {
326         char *chanName;
327         bActionChannel *chan;
328
329         if( !self->action )
330                 ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
331                                          "couldn't create channel for a NULL action" ) );
332
333         if( !PyArg_ParseTuple( args, "s", &chanName ) )
334                 return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
335                                                 "expected string argument" ) );
336
337         chan = verify_action_channel(self->action, chanName);
338
339         Py_INCREF( Py_None );
340         return Py_None;
341 }
342
343
344 static PyObject *Action_removeChannel( BPy_Action * self, PyObject * args )
345 {
346         char *chanName;
347         bActionChannel *chan;
348
349         if( !PyArg_ParseTuple( args, "s", &chanName ) ) {
350                 EXPP_ReturnPyObjError( PyExc_AttributeError,
351                                        "string expected" );
352                 return NULL;
353         }
354
355         chan = get_action_channel( self->action, chanName );
356         if( chan == NULL ) {
357                 EXPP_ReturnPyObjError( PyExc_AttributeError,
358                                        "no channel with that name..." );
359                 return NULL;
360         }
361         //release ipo
362         if( chan->ipo )
363                 chan->ipo->id.us--;
364
365         //remove channel
366         BLI_freelinkN( &self->action->chanbase, chan );
367
368         Py_INCREF( Py_None );
369         return ( Py_None );
370 }
371
372 static PyObject *Action_getAllChannelIpos( BPy_Action * self )
373 {
374         PyObject *dict = PyDict_New(  );
375         bActionChannel *chan = NULL;
376
377         for( chan = self->action->chanbase.first; chan; chan = chan->next ) {
378                 PyObject *ipo_attr = Ipo_CreatePyObject( chan->ipo );
379                 if( ipo_attr ) {
380                         // Insert dict entry using the bone name as key
381                         if( PyDict_SetItemString( dict, chan->name, ipo_attr )
382                             != 0 ) {
383                                 Py_DECREF( ipo_attr );
384                                 Py_DECREF( dict );
385
386                                 return EXPP_ReturnPyObjError
387                                         ( PyExc_RuntimeError,
388                                           "Action_getAllChannelIpos: couldn't set dict item" );
389                         }
390                         Py_DECREF( ipo_attr );
391                 } else {
392                         Py_DECREF( dict );
393                         return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
394                                                         "Action_getAllChannelIpos: could not create Ipo object" ) );
395                 }
396         }
397         return dict;
398 }
399
400 //----------------------------------------------------------------------
401 static void Action_dealloc( BPy_Action * self )
402 {
403         PyObject_DEL( self );
404 }
405
406 //----------------------------------------------------------------------
407 static PyObject *Action_getAttr( BPy_Action * self, char *name )
408 {
409         PyObject *attr = Py_None;
410
411         if( strcmp( name, "name" ) == 0 )
412                 attr = Action_getName( self );
413         else if( strcmp( name, "__members__" ) == 0 ) {
414                 attr = Py_BuildValue( "[s]", "name" );
415         }
416
417         if( !attr )
418                 return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
419                                                 "couldn't create PyObject" ) );
420
421         if( attr != Py_None )
422                 return attr;    /* member attribute found, return it */
423
424         /* not an attribute, search the methods table */
425         return Py_FindMethod( BPy_Action_methods, ( PyObject * ) self, name );
426 }
427
428 //----------------------------------------------------------------------
429 static int Action_setAttr( BPy_Action * self, char *name, PyObject * value )
430 {
431         PyObject *valtuple;
432         PyObject *error = NULL;
433
434         valtuple = Py_BuildValue( "(O)", value );       /* the set* functions expect a tuple */
435
436         if( !valtuple )
437                 return EXPP_ReturnIntError( PyExc_MemoryError,
438                                             "ActionSetAttr: couldn't create tuple" );
439
440         if( strcmp( name, "name" ) == 0 )
441                 error = Action_setName( self, valtuple );
442         else {                  /* Error */
443                 Py_DECREF( valtuple );
444
445                 /* ... member with the given name was found */
446                 return ( EXPP_ReturnIntError
447                          ( PyExc_KeyError, "attribute not found" ) );
448         }
449
450         Py_DECREF( valtuple );
451
452         if( error != Py_None )
453                 return -1;
454
455         Py_DECREF( Py_None );   /* was incref'ed by the called Action_set* function */
456         return 0;               /* normal exit */
457 }
458
459 //----------------------------------------------------------------------
460 static PyObject *Action_repr( BPy_Action * self )
461 {
462         if( self->action )
463                 return PyString_FromFormat( "[Action \"%s\"]",
464                                             self->action->id.name + 2 );
465         else
466                 return PyString_FromString( "NULL" );
467 }
468
469 //----------------------------------------------------------------------
470 PyObject *Action_CreatePyObject( struct bAction * act )
471 {
472         BPy_Action *blen_action;
473
474         if(!act)
475                 return EXPP_incr_ret(Py_None);
476
477         blen_action =
478                 ( BPy_Action * ) PyObject_NEW( BPy_Action, &Action_Type );
479
480         if( !blen_action) {
481                 return ( EXPP_ReturnPyObjError
482                          ( PyExc_RuntimeError, "failure to create object!" ) );
483         }
484         blen_action->action = act;
485         return ( ( PyObject * ) blen_action );
486 }
487
488 //-------------------------------------------------------------------------------------------------------------------------------
489 int Action_CheckPyObject( PyObject * py_obj )
490 {
491         return ( py_obj->ob_type == &Action_Type );
492 }
493
494 //----------------------------------------------------------------------
495 struct bAction *Action_FromPyObject( PyObject * py_obj )
496 {
497         BPy_Action *blen_obj;
498
499         blen_obj = ( BPy_Action * ) py_obj;
500         return ( blen_obj->action );
501 }