34
35
#include <BKE_action.h>
35
36
#include <BKE_global.h>
36
37
#include <BKE_main.h>
39
42
/*****************************************************************************/
40
43
/* Python API function prototypes for the NLA module. */
41
44
/*****************************************************************************/
42
static PyObject *M_NLA_NewAction (PyObject * self, PyObject * args);
43
static PyObject *M_NLA_CopyAction (PyObject * self, PyObject * args);
44
static PyObject *M_NLA_GetActions(PyObject* self);
45
static PyObject *M_NLA_NewAction( PyObject * self, PyObject * args );
46
static PyObject *M_NLA_CopyAction( PyObject * self, PyObject * args );
47
static PyObject *M_NLA_GetActions( PyObject * self );
46
49
/*****************************************************************************/
47
/* The following string definitions are used for documentation strings. */
50
/* The following string definitions are used for documentation strings. */
48
51
/* In Python these will be written to the console when doing a */
49
52
/* Blender.Armature.NLA.__doc__ */
50
53
/*****************************************************************************/
51
char M_NLA_doc[] = "The Blender NLA module -This module provides control over Armature keyframing in Blender.";
52
char M_NLA_NewAction_doc[] = "(name) - Create new action for linking to an object.";
55
"The Blender NLA module -This module provides control over Armature keyframing in Blender.";
56
char M_NLA_NewAction_doc[] =
57
"(name) - Create new action for linking to an object.";
53
58
char M_NLA_CopyAction_doc[] = "(name) - Copy action and return copy.";
54
59
char M_NLA_GetActions_doc[] = "(name) - Returns a dictionary of actions.";
56
61
/*****************************************************************************/
57
/* Python method structure definition for Blender.Armature.NLA module: */
62
/* Python method structure definition for Blender.Armature.NLA module: */
58
63
/*****************************************************************************/
59
64
struct PyMethodDef M_NLA_methods[] = {
60
{"NewAction", (PyCFunction) M_NLA_NewAction, METH_VARARGS,
62
{"CopyAction", (PyCFunction) M_NLA_CopyAction, METH_VARARGS,
63
M_NLA_CopyAction_doc},
64
{"GetActions", (PyCFunction) M_NLA_GetActions, METH_NOARGS,
65
M_NLA_GetActions_doc},
65
{"NewAction", ( PyCFunction ) M_NLA_NewAction, METH_VARARGS,
67
{"CopyAction", ( PyCFunction ) M_NLA_CopyAction, METH_VARARGS,
68
M_NLA_CopyAction_doc},
69
{"GetActions", ( PyCFunction ) M_NLA_GetActions, METH_NOARGS,
70
M_NLA_GetActions_doc},
68
73
/*****************************************************************************/
69
/* Python BPy_Action methods declarations: */
74
/* Python BPy_Action methods declarations: */
70
75
/*****************************************************************************/
71
static PyObject *Action_getName (BPy_Action * self);
72
static PyObject *Action_setName (BPy_Action * self, PyObject * args);
73
static PyObject *Action_setActive (BPy_Action * self, PyObject * args);
74
static PyObject *Action_getChannelIpo(BPy_Action * self, PyObject * args);
75
static PyObject *Action_removeChannel(BPy_Action * self, PyObject * args);
76
static PyObject *Action_getAllChannelIpos(BPy_Action*self);
76
static PyObject *Action_getName( BPy_Action * self );
77
static PyObject *Action_setName( BPy_Action * self, PyObject * args );
78
static PyObject *Action_setActive( BPy_Action * self, PyObject * args );
79
static PyObject *Action_getChannelIpo( BPy_Action * self, PyObject * args );
80
static PyObject *Action_removeChannel( BPy_Action * self, PyObject * args );
81
static PyObject *Action_getAllChannelIpos( BPy_Action * self );
78
83
/*****************************************************************************/
79
84
/* Python BPy_Action methods table: */
80
85
/*****************************************************************************/
81
86
static PyMethodDef BPy_Action_methods[] = {
82
/* name, method, flags, doc */
83
{"getName", (PyCFunction) Action_getName, METH_NOARGS,
84
"() - return Action name"},
85
{"setName", (PyCFunction) Action_setName, METH_VARARGS,
86
"(str) - rename Action"},
87
{"setActive", (PyCFunction) Action_setActive, METH_VARARGS,
88
"(str) -set this action as the active action for an object"},
89
{"getChannelIpo", (PyCFunction) Action_getChannelIpo, METH_VARARGS,
90
"(str) -get the Ipo from a named action channel in this action"},
91
{"removeChannel", (PyCFunction) Action_removeChannel, METH_VARARGS,
92
"(str) -remove the channel from the action"},
93
{"getAllChannelIpos", (PyCFunction)Action_getAllChannelIpos, METH_NOARGS,
94
"() - Return a dict of (name:ipo)-keys containing each channel in the object's action"},
87
/* name, method, flags, doc */
88
{"getName", ( PyCFunction ) Action_getName, METH_NOARGS,
89
"() - return Action name"},
90
{"setName", ( PyCFunction ) Action_setName, METH_VARARGS,
91
"(str) - rename Action"},
92
{"setActive", ( PyCFunction ) Action_setActive, METH_VARARGS,
93
"(str) -set this action as the active action for an object"},
94
{"getChannelIpo", ( PyCFunction ) Action_getChannelIpo, METH_VARARGS,
95
"(str) -get the Ipo from a named action channel in this action"},
96
{"removeChannel", ( PyCFunction ) Action_removeChannel, METH_VARARGS,
97
"(str) -remove the channel from the action"},
98
{"getAllChannelIpos", ( PyCFunction ) Action_getAllChannelIpos,
100
"() - Return a dict of (name:ipo)-keys containing each channel in the object's action"},
101
{NULL, NULL, 0, NULL}
98
104
/*****************************************************************************/
99
/* Python TypeAction callback function prototypes: */
105
/* Python TypeAction callback function prototypes: */
100
106
/*****************************************************************************/
101
static void Action_dealloc (BPy_Action * bone);
102
static PyObject *Action_getAttr (BPy_Action * bone, char *name);
103
static int Action_setAttr (BPy_Action * bone, char *name, PyObject * v);
104
static PyObject *Action_repr (BPy_Action * bone);
107
static void Action_dealloc( BPy_Action * bone );
108
static PyObject *Action_getAttr( BPy_Action * bone, char *name );
109
static int Action_setAttr( BPy_Action * bone, char *name, PyObject * v );
110
static PyObject *Action_repr( BPy_Action * bone );
106
112
/*****************************************************************************/
107
113
/* Python TypeAction structure definition: */
108
114
/*****************************************************************************/
109
115
PyTypeObject Action_Type = {
110
PyObject_HEAD_INIT (NULL) 0, /* ob_size */
111
"Blender Action", /* tp_name */
112
sizeof (BPy_Action), /* tp_basicsize */
115
(destructor) Action_dealloc, /* tp_dealloc */
117
(getattrfunc) Action_getAttr, /* tp_getattr */
118
(setattrfunc) Action_setAttr, /* tp_setattr */
120
(reprfunc) Action_repr, /* tp_repr */
121
0, /* tp_as_number */
122
0, /* tp_as_sequence */
123
0, /* tp_as_mapping */
128
BPy_Action_methods, /* tp_methods */
116
PyObject_HEAD_INIT( NULL )
118
"Blender Action", /* tp_name */
119
sizeof( BPy_Action ), /* tp_basicsize */
122
( destructor ) Action_dealloc, /* tp_dealloc */
124
( getattrfunc ) Action_getAttr, /* tp_getattr */
125
( setattrfunc ) Action_setAttr, /* tp_setattr */
127
( reprfunc ) Action_repr, /* tp_repr */
128
0, /* tp_as_number */
129
0, /* tp_as_sequence */
130
0, /* tp_as_mapping */
135
BPy_Action_methods, /* tp_methods */
132
//-------------------------------------------------------------------------------------------------------------------------------
134
M_NLA_NewAction (PyObject * self, PyObject * args)
139
//-------------------------------------------------------------------------
140
static PyObject *M_NLA_NewAction( PyObject * self, PyObject * args )
136
char *name_str = "DefaultAction";
137
BPy_Action *py_action = NULL; /* for Action Data object wrapper in Python */
138
bAction *bl_action = NULL; /* for actual Action Data we create in Blender */
140
if (!PyArg_ParseTuple (args, "|s", &name_str)){
141
EXPP_ReturnPyObjError (PyExc_AttributeError,
142
"expected string or nothing");
146
//Create new action globally
147
bl_action = alloc_libblock(&G.main->action, ID_AC, name_str);
148
bl_action->id.flag |= LIB_FAKEUSER;
151
// now create the wrapper obj in Python
153
py_action = (BPy_Action *) PyObject_NEW (BPy_Action, &Action_Type);
155
EXPP_ReturnPyObjError (PyExc_RuntimeError,
156
"couldn't create Action Data in Blender");
160
if (py_action == NULL){
161
EXPP_ReturnPyObjError (PyExc_MemoryError,
162
"couldn't create Action Data object");
166
py_action->action = bl_action; // link Python action wrapper with Blender Action
168
Py_INCREF(py_action);
169
return (PyObject *) py_action;
142
char *name_str = "DefaultAction";
143
BPy_Action *py_action = NULL; /* for Action Data object wrapper in Python */
144
bAction *bl_action = NULL; /* for actual Action Data we create in Blender */
146
if( !PyArg_ParseTuple( args, "|s", &name_str ) ) {
147
EXPP_ReturnPyObjError( PyExc_AttributeError,
148
"expected string or nothing" );
151
//Create new action globally
152
bl_action = alloc_libblock( &G.main->action, ID_AC, name_str );
153
bl_action->id.flag |= LIB_FAKEUSER;
156
// now create the wrapper obj in Python
159
( BPy_Action * ) PyObject_NEW( BPy_Action,
162
EXPP_ReturnPyObjError( PyExc_RuntimeError,
163
"couldn't create Action Data in Blender" );
167
if( py_action == NULL ) {
168
EXPP_ReturnPyObjError( PyExc_MemoryError,
169
"couldn't create Action Data object" );
173
py_action->action = bl_action; // link Python action wrapper with Blender Action
175
Py_INCREF( py_action );
176
return ( PyObject * ) py_action;
173
M_NLA_CopyAction(PyObject* self, PyObject * args)
179
static PyObject *M_NLA_CopyAction( PyObject * self, PyObject * args )
175
181
BPy_Action *py_action = NULL;
176
182
bAction *copyAction = NULL;
178
if (!PyArg_ParseTuple (args, "O!", &Action_Type, &py_action)){
179
EXPP_ReturnPyObjError (PyExc_AttributeError,
180
"expected python action type");
184
if( !PyArg_ParseTuple( args, "O!", &Action_Type, &py_action ) ) {
185
EXPP_ReturnPyObjError( PyExc_AttributeError,
186
"expected python action type" );
183
copyAction = copy_action(py_action->action);
184
return Action_CreatePyObject (copyAction);
189
copyAction = copy_action( py_action->action );
190
return Action_CreatePyObject( copyAction );
188
M_NLA_GetActions(PyObject* self)
193
static PyObject *M_NLA_GetActions( PyObject * self )
190
PyObject *dict=PyDict_New ();
195
PyObject *dict = PyDict_New( );
191
196
bAction *action = NULL;
193
for(action = G.main->action.first; action; action = action->id.next){
194
PyObject * py_action = Action_CreatePyObject (action);
198
for( action = G.main->action.first; action; action = action->id.next ) {
199
PyObject *py_action = Action_CreatePyObject( action );
196
201
// Insert dict entry using the bone name as key
197
if (PyDict_SetItemString (dict, action->id.name + 2, py_action) !=0) {
198
Py_DECREF (py_action);
201
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
202
"NLA_GetActions: couldn't set dict item");
202
if( PyDict_SetItemString
203
( dict, action->id.name + 2, py_action ) != 0 ) {
204
Py_DECREF( py_action );
207
return EXPP_ReturnPyObjError
208
( PyExc_RuntimeError,
209
"NLA_GetActions: couldn't set dict item" );
204
Py_DECREF (py_action);
211
Py_DECREF( py_action );
207
return (PythonReturnErrorObject (PyExc_RuntimeError,
208
"NLA_GetActions: could not create Action object"));
214
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
215
"NLA_GetActions: could not create Action object" ) );
214
221
/*****************************************************************************/
215
222
/* Function: NLA_Init */
216
223
/*****************************************************************************/
222
Action_Type.ob_type = &PyType_Type;
224
submodule = Py_InitModule3 ("Blender.Armature.NLA",
225
M_NLA_methods, M_NLA_doc);
230
//-------------------------------------------------------------------------------------------------------------------------------
232
Action_getName (BPy_Action * self)
234
PyObject *attr = NULL;
237
(EXPP_ReturnPyObjError (PyExc_RuntimeError,
238
"couldn't get attribute from a NULL action"));
240
attr = PyString_FromString (self->action->id.name+2);
245
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
246
"couldn't get Action.name attribute"));
248
//-------------------------------------------------------------------------------------------------------------------------------
250
Action_setName (BPy_Action * self, PyObject * args)
255
(EXPP_ReturnPyObjError (PyExc_RuntimeError,
256
"couldn't get attribute from a NULL action"));
258
if (!PyArg_ParseTuple (args, "s", &name))
259
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
260
"expected string argument"));
263
strcpy(self->action->id.name+2, name);
270
Action_setActive(BPy_Action * self, PyObject * args)
275
(EXPP_ReturnPyObjError (PyExc_RuntimeError,
276
"couldn't get attribute from a NULL action"));
278
if (!PyArg_ParseTuple (args, "O!", &Object_Type, &object))
279
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
280
"expected python object argument"));
282
if(object->object->type != OB_ARMATURE) {
283
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
284
"object not of type armature"));
287
//set the active action to object
288
object->object->action = self->action;
295
Action_getChannelIpo(BPy_Action * self, PyObject * args)
224
PyObject *NLA_Init( void )
228
Action_Type.ob_type = &PyType_Type;
230
submodule = Py_InitModule3( "Blender.Armature.NLA",
231
M_NLA_methods, M_NLA_doc );
233
return ( submodule );
236
//----------------------------------------------------------------------
237
static PyObject *Action_getName( BPy_Action * self )
239
PyObject *attr = NULL;
242
( EXPP_ReturnPyObjError( PyExc_RuntimeError,
243
"couldn't get attribute from a NULL action" ) );
245
attr = PyString_FromString( self->action->id.name + 2 );
250
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
251
"couldn't get Action.name attribute" ) );
254
//----------------------------------------------------------------------
255
static PyObject *Action_setName( BPy_Action * self, PyObject * args )
260
( EXPP_ReturnPyObjError( PyExc_RuntimeError,
261
"couldn't get attribute from a NULL action" ) );
263
if( !PyArg_ParseTuple( args, "s", &name ) )
264
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
265
"expected string argument" ) );
268
strcpy( self->action->id.name + 2, name );
270
Py_INCREF( Py_None );
274
static PyObject *Action_setActive( BPy_Action * self, PyObject * args )
279
( EXPP_ReturnPyObjError( PyExc_RuntimeError,
280
"couldn't get attribute from a NULL action" ) );
282
if( !PyArg_ParseTuple( args, "O!", &Object_Type, &object ) )
283
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
284
"expected python object argument" ) );
286
if( object->object->type != OB_ARMATURE ) {
287
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
288
"object not of type armature" ) );
290
//set the active action to object
291
object->object->action = self->action;
293
Py_INCREF( Py_None );
297
static PyObject *Action_getChannelIpo( BPy_Action * self, PyObject * args )
298
300
bActionChannel *chan;
300
if(!PyArg_ParseTuple(args, "s", &chanName)){
301
EXPP_ReturnPyObjError(PyExc_AttributeError, "string expected");
305
chan = get_named_actionchannel(self->action,chanName);
307
EXPP_ReturnPyObjError(PyExc_AttributeError, "no channel with that name...");
302
if( !PyArg_ParseTuple( args, "s", &chanName ) ) {
303
EXPP_ReturnPyObjError( PyExc_AttributeError,
308
chan = get_named_actionchannel( self->action, chanName );
310
EXPP_ReturnPyObjError( PyExc_AttributeError,
311
"no channel with that name..." );
312
return Ipo_CreatePyObject (chan->ipo);
315
return Ipo_CreatePyObject( chan->ipo );
316
Action_removeChannel(BPy_Action * self, PyObject * args)
318
static PyObject *Action_removeChannel( BPy_Action * self, PyObject * args )
319
321
bActionChannel *chan;
321
if(!PyArg_ParseTuple(args, "s", &chanName)){
322
EXPP_ReturnPyObjError(PyExc_AttributeError, "string expected");
326
chan = get_named_actionchannel(self->action,chanName);
328
EXPP_ReturnPyObjError(PyExc_AttributeError, "no channel with that name...");
323
if( !PyArg_ParseTuple( args, "s", &chanName ) ) {
324
EXPP_ReturnPyObjError( PyExc_AttributeError,
329
chan = get_named_actionchannel( self->action, chanName );
331
EXPP_ReturnPyObjError( PyExc_AttributeError,
332
"no channel with that name..." );
334
337
chan->ipo->id.us--;
337
BLI_freelinkN (&self->action->chanbase, chan);
340
BLI_freelinkN( &self->action->chanbase, chan );
342
Py_INCREF( Py_None );
343
static PyObject *Action_getAllChannelIpos (BPy_Action *self)
346
static PyObject *Action_getAllChannelIpos( BPy_Action * self )
345
PyObject *dict=PyDict_New ();
348
PyObject *dict = PyDict_New( );
346
349
bActionChannel *chan = NULL;
348
for(chan = self->action->chanbase.first; chan; chan = chan->next){
349
PyObject * ipo_attr = Ipo_CreatePyObject (chan->ipo);
351
for( chan = self->action->chanbase.first; chan; chan = chan->next ) {
352
PyObject *ipo_attr = Ipo_CreatePyObject( chan->ipo );
351
354
// Insert dict entry using the bone name as key
352
if (PyDict_SetItemString (dict, chan->name, ipo_attr) !=0) {
353
Py_DECREF ( ipo_attr );
356
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
357
"Action_getAllChannelIpos: couldn't set dict item");
355
if( PyDict_SetItemString( dict, chan->name, ipo_attr )
357
Py_DECREF( ipo_attr );
360
return EXPP_ReturnPyObjError
361
( PyExc_RuntimeError,
362
"Action_getAllChannelIpos: couldn't set dict item" );
359
Py_DECREF (ipo_attr);
364
Py_DECREF( ipo_attr );
362
return (PythonReturnErrorObject (PyExc_RuntimeError,
363
"Action_getAllChannelIpos: could not create Ipo object"));
367
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
368
"Action_getAllChannelIpos: could not create Ipo object" ) );
369
//-------------------------------------------------------------------------------------------------------------------------------
371
Action_dealloc (BPy_Action * self)
375
//-------------------------------------------------------------------------------------------------------------------------------
377
Action_getAttr (BPy_Action * self, char *name)
379
PyObject *attr = Py_None;
381
if (strcmp (name, "name") == 0)
382
attr = Action_getName (self);
383
else if (strcmp (name, "__members__") == 0) {
384
attr = Py_BuildValue ("[s]",
389
return (EXPP_ReturnPyObjError (PyExc_MemoryError,
390
"couldn't create PyObject"));
393
return attr; /* member attribute found, return it */
395
/* not an attribute, search the methods table */
396
return Py_FindMethod (BPy_Action_methods, (PyObject *) self, name);
399
//-------------------------------------------------------------------------------------------------------------------------------
401
Action_setAttr (BPy_Action * self, char *name, PyObject * value)
404
PyObject *error = NULL;
406
valtuple = Py_BuildValue ("(O)", value); /* the set* functions expect a tuple */
409
return EXPP_ReturnIntError (PyExc_MemoryError,
410
"ActionSetAttr: couldn't create tuple");
412
if (strcmp (name, "name") == 0)
413
error = Action_setName (self, valtuple);
416
Py_DECREF (valtuple);
418
/* ... member with the given name was found */
419
return (EXPP_ReturnIntError (PyExc_KeyError, "attribute not found"));
422
Py_DECREF (valtuple);
424
if (error != Py_None)
427
Py_DECREF (Py_None); /* was incref'ed by the called Action_set* function */
428
return 0; /* normal exit */
430
//-------------------------------------------------------------------------------------------------------------------------------
432
Action_repr (BPy_Action * self)
435
return PyString_FromFormat ("[Action \"%s\"]", self->action->id.name + 2);
437
return PyString_FromString ("NULL");
439
//-------------------------------------------------------------------------------------------------------------------------------
441
Action_CreatePyObject (struct bAction * act)
443
BPy_Action *blen_action;
445
blen_action = (BPy_Action *) PyObject_NEW (BPy_Action, &Action_Type);
447
if (blen_action == NULL)
451
blen_action->action = act;
452
return ((PyObject *) blen_action);
454
//-------------------------------------------------------------------------------------------------------------------------------
456
Action_CheckPyObject (PyObject * py_obj)
458
return (py_obj->ob_type == &Action_Type);
460
//-------------------------------------------------------------------------------------------------------------------------------
462
Action_FromPyObject (PyObject * py_obj)
464
BPy_Action *blen_obj;
466
blen_obj = (BPy_Action *) py_obj;
467
return (blen_obj->action);
374
//----------------------------------------------------------------------
375
static void Action_dealloc( BPy_Action * self )
377
PyObject_DEL( self );
380
//----------------------------------------------------------------------
381
static PyObject *Action_getAttr( BPy_Action * self, char *name )
383
PyObject *attr = Py_None;
385
if( strcmp( name, "name" ) == 0 )
386
attr = Action_getName( self );
387
else if( strcmp( name, "__members__" ) == 0 ) {
388
attr = Py_BuildValue( "[s]", "name" );
392
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
393
"couldn't create PyObject" ) );
395
if( attr != Py_None )
396
return attr; /* member attribute found, return it */
398
/* not an attribute, search the methods table */
399
return Py_FindMethod( BPy_Action_methods, ( PyObject * ) self, name );
402
//----------------------------------------------------------------------
403
static int Action_setAttr( BPy_Action * self, char *name, PyObject * value )
406
PyObject *error = NULL;
408
valtuple = Py_BuildValue( "(O)", value ); /* the set* functions expect a tuple */
411
return EXPP_ReturnIntError( PyExc_MemoryError,
412
"ActionSetAttr: couldn't create tuple" );
414
if( strcmp( name, "name" ) == 0 )
415
error = Action_setName( self, valtuple );
417
Py_DECREF( valtuple );
419
/* ... member with the given name was found */
420
return ( EXPP_ReturnIntError
421
( PyExc_KeyError, "attribute not found" ) );
424
Py_DECREF( valtuple );
426
if( error != Py_None )
429
Py_DECREF( Py_None ); /* was incref'ed by the called Action_set* function */
430
return 0; /* normal exit */
433
//----------------------------------------------------------------------
434
static PyObject *Action_repr( BPy_Action * self )
437
return PyString_FromFormat( "[Action \"%s\"]",
438
self->action->id.name + 2 );
440
return PyString_FromString( "NULL" );
443
//----------------------------------------------------------------------
444
PyObject *Action_CreatePyObject( struct bAction * act )
446
BPy_Action *blen_action;
449
( BPy_Action * ) PyObject_NEW( BPy_Action, &Action_Type );
451
if( blen_action == NULL ) {
454
blen_action->action = act;
455
return ( ( PyObject * ) blen_action );
458
//-------------------------------------------------------------------------------------------------------------------------------
459
int Action_CheckPyObject( PyObject * py_obj )
461
return ( py_obj->ob_type == &Action_Type );
464
//----------------------------------------------------------------------
465
struct bAction *Action_FromPyObject( PyObject * py_obj )
467
BPy_Action *blen_obj;
469
blen_obj = ( BPy_Action * ) py_obj;
470
return ( blen_obj->action );