d57c7a60d9b608a729895df3084c9eba81b7fa3d
[blender.git] / source / blender / python / api2_2x / Key.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): Pontus Lidman, Johnny Matthews, Ken Hughes
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include "DNA_scene_types.h"
34
35 #include <BLI_blenlib.h>
36 #include <BKE_global.h>
37 #include <BKE_main.h>
38 #include <BKE_curve.h>
39
40 #include "Ipocurve.h"
41 #include "Key.h"
42 #include "NMesh.h" /* we create NMesh.NMVert objects */
43 #include "Ipo.h"
44 #include "BezTriple.h"
45
46 #include "BSE_editipo.h"
47 #include "mydevice.h"
48 #include "BKE_depsgraph.h"
49 #include "blendef.h"
50 #include "constant.h"
51 #include "gen_utils.h"
52
53 #define KEY_TYPE_MESH    0
54 #define KEY_TYPE_CURVE   1
55 #define KEY_TYPE_LATTICE 2
56
57 /* macro from blenkernel/intern/key.c:98 */
58 #define GS(a)   (*((short *)(a)))
59
60 static void Key_dealloc( PyObject * self );
61 static void KeyBlock_dealloc( PyObject * self );
62 static PyObject *Key_repr( BPy_Key * self );
63
64 static PyObject *Key_getBlocks( PyObject * self );
65 static PyObject *Key_getType( PyObject * self );
66 static PyObject *Key_getIpo( PyObject * self );
67 static int Key_setIpo( PyObject * self, PyObject * args );
68 static PyObject *Key_getValue( PyObject * self );
69
70 static struct PyMethodDef Key_methods[] = {
71         { "getBlocks", (PyCFunction) Key_getBlocks, METH_NOARGS, "Get key blocks" },
72         { "getIpo", (PyCFunction) Key_getIpo, METH_NOARGS, "Get key Ipo" },
73         { 0, 0, 0, 0 }
74 };
75
76 static PyGetSetDef BPy_Key_getsetters[] = {
77                 {"type",(getter)Key_getType, (setter)NULL,
78                  "Key Type",NULL},
79                 {"value",(getter)Key_getValue, (setter)NULL,
80                  "Key value",NULL},
81                 {"ipo",(getter)Key_getIpo, (setter)Key_setIpo,
82                  "ipo linked to key",NULL},
83                 {"blocks",(getter)Key_getBlocks, (setter)NULL,
84                  "blocks linked to the key",NULL},
85                 {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
86         };
87
88
89
90 static PyObject *KeyBlock_getData( PyObject * self );
91
92 static PyObject *KeyBlock_getName( BPy_KeyBlock * self );
93 static PyObject *KeyBlock_getPos( BPy_KeyBlock * self );
94 static PyObject *KeyBlock_getSlidermin( BPy_KeyBlock * self );
95 static PyObject *KeyBlock_getSlidermax( BPy_KeyBlock * self );
96 static PyObject *KeyBlock_getVgroup( BPy_KeyBlock * self );
97
98 static int KeyBlock_setName( BPy_KeyBlock *, PyObject * args  );
99 static int KeyBlock_setVgroup( BPy_KeyBlock *, PyObject * args  );
100 static int KeyBlock_setSlidermin( BPy_KeyBlock *, PyObject * args  );
101 static int KeyBlock_setSlidermax( BPy_KeyBlock *, PyObject * args  );
102
103 static struct PyMethodDef KeyBlock_methods[] = {
104         { "getData", (PyCFunction) KeyBlock_getData, METH_NOARGS,
105                 "Get keyblock data" },
106         { 0, 0, 0, 0 }
107 };
108
109 static PyGetSetDef BPy_KeyBlock_getsetters[] = {
110                 {"name",(getter)KeyBlock_getName, (setter)KeyBlock_setName,
111                  "Keyblock Name",NULL},
112                 {"pos",(getter)KeyBlock_getPos, (setter)NULL,
113                  "Keyblock Pos",NULL},
114                 {"slidermin",(getter)KeyBlock_getSlidermin, (setter)KeyBlock_setSlidermin,
115                  "Keyblock Slider Minimum",NULL},
116                 {"slidermax",(getter)KeyBlock_getSlidermax, (setter)KeyBlock_setSlidermax,
117                  "Keyblock Slider Maximum",NULL},
118                 {"vgroup",(getter)KeyBlock_getVgroup, (setter)KeyBlock_setVgroup,
119                  "Keyblock VGroup",NULL},
120                 {"data",(getter)KeyBlock_getData, (setter)NULL,
121                  "Keyblock VGroup",NULL},
122                 {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
123 };
124
125 PyTypeObject Key_Type = {
126         PyObject_HEAD_INIT( NULL ) 0,   /*ob_size */
127         "Blender Key",                                  /*tp_name */
128         sizeof( BPy_Key ),                              /*tp_basicsize */
129         0,                                                              /*tp_itemsize */
130         /* methods */
131         ( destructor ) Key_dealloc,             /*tp_dealloc */
132         ( printfunc ) 0,                                /*tp_print */
133         ( getattrfunc ) 0,      /*tp_getattr */
134         ( setattrfunc ) 0,                              /*tp_setattr */
135         0,                                                              /*tp_compare*/
136         ( reprfunc ) Key_repr,                  /* tp_repr */
137         /* Method suites for standard classes */
138
139         NULL,                       /* PyNumberMethods *tp_as_number; */
140         NULL,                       /* PySequenceMethods *tp_as_sequence; */
141         NULL,                       /* PyMappingMethods *tp_as_mapping; */
142
143         /* More standard operations (here for binary compatibility) */
144
145         NULL,                       /* hashfunc tp_hash; */
146         NULL,                       /* ternaryfunc tp_call; */
147         NULL,                       /* reprfunc tp_str; */
148         NULL,                       /* getattrofunc tp_getattro; */
149         NULL,                       /* setattrofunc tp_setattro; */
150
151         /* Functions to access object as input/output buffer */
152         NULL,                       /* PyBufferProcs *tp_as_buffer; */
153
154   /*** Flags to define presence of optional/expanded features ***/
155         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
156
157         NULL,                       /*  char *tp_doc;  Documentation string */
158   /*** Assigned meaning in release 2.0 ***/
159         /* call function for all accessible objects */
160         NULL,                       /* traverseproc tp_traverse; */
161
162         /* delete references to contained objects */
163         NULL,                       /* inquiry tp_clear; */
164
165   /***  Assigned meaning in release 2.1 ***/
166   /*** rich comparisons ***/
167         NULL,                       /* richcmpfunc tp_richcompare; */
168
169   /***  weak reference enabler ***/
170         0,                          /* long tp_weaklistoffset; */
171
172   /*** Added in release 2.2 ***/
173         /*   Iterators */
174         NULL,                       /* getiterfunc tp_iter; */
175         NULL,                       /* iternextfunc tp_iternext; */
176
177   /*** Attribute descriptor and subclassing stuff ***/
178         Key_methods,                    /* struct PyMethodDef *tp_methods; */
179         NULL,                       /* struct PyMemberDef *tp_members; */
180         BPy_Key_getsetters,             /* struct PyGetSetDef *tp_getset; */
181         NULL,                       /* struct _typeobject *tp_base; */
182         NULL,                           /* PyObject *tp_dict; */
183         NULL,                       /* descrgetfunc tp_descr_get; */
184         NULL,                       /* descrsetfunc tp_descr_set; */
185         0,                          /* long tp_dictoffset; */
186         NULL,                       /* initproc tp_init; */
187         NULL,                       /* allocfunc tp_alloc; */
188         NULL,                       /* newfunc tp_new; */
189         /*  Low-level free-memory routine */
190         NULL,                       /* freefunc tp_free;  */
191         /* For PyObject_IS_GC */
192         NULL,                       /* inquiry tp_is_gc;  */
193         NULL,                       /* PyObject *tp_bases; */
194         /* method resolution order */
195         NULL,                       /* PyObject *tp_mro;  */
196         NULL,                       /* PyObject *tp_cache; */
197         NULL,                       /* PyObject *tp_subclasses; */
198         NULL,                       /* PyObject *tp_weaklist; */
199         NULL
200 };
201
202 PyTypeObject KeyBlock_Type = {
203         PyObject_HEAD_INIT( NULL ) 0,   /*ob_size */
204         "Blender KeyBlock",     /*tp_name */
205         sizeof( BPy_KeyBlock ), /*tp_basicsize */
206         0,                      /*tp_itemsize */
207         /* methods */
208         ( destructor ) KeyBlock_dealloc,        /*tp_dealloc */
209         NULL,                           /*tp_print */
210         NULL,                           /*tp_getattr */
211         NULL,                           /*tp_setattr */
212         NULL,                           /*tp_compare*/
213         NULL,                           /* tp_repr */
214         /* Method suites for standard classes */
215
216         NULL,                       /* PyNumberMethods *tp_as_number; */
217         NULL,                       /* PySequenceMethods *tp_as_sequence; */
218         NULL,                       /* PyMappingMethods *tp_as_mapping; */
219
220         /* More standard operations (here for binary compatibility) */
221
222         NULL,                       /* hashfunc tp_hash; */
223         NULL,                       /* ternaryfunc tp_call; */
224         NULL,                       /* reprfunc tp_str; */
225         NULL,                       /* getattrofunc tp_getattro; */
226         NULL,                       /* setattrofunc tp_setattro; */
227
228         /* Functions to access object as input/output buffer */
229         NULL,                       /* PyBufferProcs *tp_as_buffer; */
230
231   /*** Flags to define presence of optional/expanded features ***/
232         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
233
234         NULL,                       /*  char *tp_doc;  Documentation string */
235   /*** Assigned meaning in release 2.0 ***/
236         /* call function for all accessible objects */
237         NULL,                       /* traverseproc tp_traverse; */
238
239         /* delete references to contained objects */
240         NULL,                       /* inquiry tp_clear; */
241
242   /***  Assigned meaning in release 2.1 ***/
243   /*** rich comparisons ***/
244         NULL,                       /* richcmpfunc tp_richcompare; */
245
246   /***  weak reference enabler ***/
247         0,                          /* long tp_weaklistoffset; */
248
249   /*** Added in release 2.2 ***/
250         /*   Iterators */
251         NULL,                       /* getiterfunc tp_iter; */
252         NULL,                       /* iternextfunc tp_iternext; */
253
254   /*** Attribute descriptor and subclassing stuff ***/
255         KeyBlock_methods,                       /* struct PyMethodDef *tp_methods; */
256         NULL,                       /* struct PyMemberDef *tp_members; */
257         BPy_KeyBlock_getsetters,    /* struct PyGetSetDef *tp_getset; */
258         NULL,                       /* struct _typeobject *tp_base; */
259         NULL,                       /* PyObject *tp_dict; */
260         NULL,                       /* descrgetfunc tp_descr_get; */
261         NULL,                       /* descrsetfunc tp_descr_set; */
262         0,                          /* long tp_dictoffset; */
263         NULL,                       /* initproc tp_init; */
264         NULL,                       /* allocfunc tp_alloc; */
265         NULL,                       /* newfunc tp_new; */
266         /*  Low-level free-memory routine */
267         NULL,                       /* freefunc tp_free;  */
268         /* For PyObject_IS_GC */
269         NULL,                       /* inquiry tp_is_gc;  */
270         NULL,                       /* PyObject *tp_bases; */
271         /* method resolution order */
272         NULL,                       /* PyObject *tp_mro;  */
273         NULL,                       /* PyObject *tp_cache; */
274         NULL,                       /* PyObject *tp_subclasses; */
275         NULL,                       /* PyObject *tp_weaklist; */
276         NULL
277 };
278
279 static void Key_dealloc( PyObject * self )
280 {
281         PyObject_DEL( self );
282 }
283
284 static PyObject *new_Key(Key * oldkey)
285 {
286         BPy_Key *k = PyObject_NEW( BPy_Key, &Key_Type );
287
288         if( !oldkey ) {
289                 k->key = 0;
290         } else {
291                 k->key = oldkey;
292         }
293         return ( PyObject * ) k;
294 }
295
296 PyObject *Key_CreatePyObject( Key * k )
297 {
298         BPy_Key *key = ( BPy_Key * ) new_Key( k );
299
300         return ( PyObject * ) key;
301 }
302
303 static PyObject *Key_repr( BPy_Key * self )
304 {
305         return PyString_FromFormat( "[Key \"%s\"]", self->key->id.name + 2 );
306 }
307
308 static PyObject *Key_getIpo( PyObject * self )
309 {
310         BPy_Key *k = ( BPy_Key * ) self;
311         BPy_Ipo *new_ipo;
312
313         if (k->key->ipo) {
314                 new_ipo = ( BPy_Ipo * ) PyObject_NEW( BPy_Ipo, &Ipo_Type );
315                 new_ipo->ipo = k->key->ipo;
316                 return (PyObject *) new_ipo;
317         } else {
318                 return EXPP_incr_ret( Py_None );
319         }
320 }
321
322 static int Key_setIpo( PyObject * self, PyObject * value )
323 {
324         Ipo *ipo = NULL;
325         Ipo *oldipo = (( BPy_Key * )self)->key->ipo;
326         ID *id;
327
328         /* if parameter is not None, check for valid Ipo */
329
330         if ( value != Py_None ) {
331                 if ( !Ipo_CheckPyObject( value ) )
332                         return EXPP_ReturnIntError( PyExc_RuntimeError,
333                                                 "expected an Ipo object" );
334
335                 ipo = Ipo_FromPyObject( value );
336
337                 if( !ipo )
338                         return EXPP_ReturnIntError( PyExc_RuntimeError,
339                                                 "null ipo!" );
340
341                 if( ipo->blocktype != ID_KE )
342                         return EXPP_ReturnIntError( PyExc_TypeError,
343                                                 "Ipo is not a key data Ipo" );
344         }
345
346         /* if already linked to Ipo, delete link */
347
348         if ( oldipo ) {
349                 id = &oldipo->id;
350                 if( id->us > 0 )
351                         id->us--;
352         }
353
354         /* assign new Ipo and increment user count, or set to NULL if deleting */
355
356         (( BPy_Key * )self)->key->ipo = ipo;
357         if ( ipo ) {
358                 id = &ipo->id;
359                 id->us++;
360         }
361
362         return 0;
363 }
364
365 static PyObject *Key_getType( PyObject * self )
366 {
367         BPy_Key *k = ( BPy_Key * ) self;
368         int idcode;
369         int type = -1;
370
371         idcode = GS( k->key->from->name );
372
373         switch( idcode ) {
374         case ID_ME:
375                 type = KEY_TYPE_MESH;
376                 break;
377         case ID_CU:
378                 type = KEY_TYPE_CURVE;
379                 break;
380         case ID_LT:
381                 type = KEY_TYPE_LATTICE;
382                 break;
383         }
384
385         return PyInt_FromLong( type );
386 }
387
388 static PyObject *Key_getBlocks( PyObject * self )
389 {
390         BPy_Key *k = ( BPy_Key * ) self;
391         Key *key = k->key;
392         KeyBlock *kb;
393         PyObject *keyblock_object;
394         PyObject *l = PyList_New( 0 );
395
396         for (kb = key->block.first; kb; kb = kb->next) {
397                 
398                 keyblock_object =  KeyBlock_CreatePyObject( kb, key );
399                 PyList_Append( l, keyblock_object );
400         }
401
402         return l;
403 }
404
405 static PyObject *Key_getValue( PyObject * self )
406 {
407         BPy_Key *k = ( BPy_Key * ) self;
408
409         return PyFloat_FromDouble( k->key->curval );
410 }
411
412 /* ------------ Key Block Functions -------------- */
413
414 static void KeyBlock_dealloc( PyObject * self )
415 {
416         PyObject_DEL( self );
417 }
418
419 static PyObject *new_KeyBlock( KeyBlock * oldkeyBlock, Key *parentKey)
420 {
421         BPy_KeyBlock *kb = PyObject_NEW( BPy_KeyBlock, &KeyBlock_Type );
422
423         kb->key = parentKey;
424
425         if( !oldkeyBlock ) {
426                 kb->keyblock = 0;
427         } else {
428                 kb->keyblock = oldkeyBlock;
429         }
430         return ( PyObject * ) kb;
431 }
432
433 PyObject *KeyBlock_CreatePyObject( KeyBlock * kb, Key *parentKey )
434 {
435         BPy_KeyBlock *keyBlock = ( BPy_KeyBlock * ) new_KeyBlock( kb, parentKey );
436
437         return ( PyObject * ) keyBlock;
438 }
439
440 static PyObject *KeyBlock_getName( BPy_KeyBlock * self ) {
441         BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self;
442         PyObject *name = Py_BuildValue( "s", kb->keyblock->name);
443         return name;
444 }
445
446 static PyObject *KeyBlock_getPos( BPy_KeyBlock * self ){
447         BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self;
448         return PyFloat_FromDouble( kb->keyblock->pos );                 
449 }
450
451 static PyObject *KeyBlock_getSlidermin( BPy_KeyBlock * self ){
452         BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self;
453         return PyFloat_FromDouble( kb->keyblock->slidermin );   
454 }
455
456 static PyObject *KeyBlock_getSlidermax( BPy_KeyBlock * self ){
457         BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self;
458         return PyFloat_FromDouble( kb->keyblock->slidermax );
459 }
460
461 static PyObject *KeyBlock_getVgroup( BPy_KeyBlock * self ){
462         BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self;
463         PyObject *name = Py_BuildValue( "s", kb->keyblock->vgroup);
464         return name;    
465 }
466
467 static int KeyBlock_setName( BPy_KeyBlock * self, PyObject * args ){
468         char* text = NULL;
469     BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self; 
470     
471         text = PyString_AsString ( args );
472         if( !text )
473                 return EXPP_ReturnIntError( PyExc_TypeError,
474                                               "expected string argument" );                                                     
475         strncpy( kb->keyblock->name, text , 32);
476
477         return 0;       
478 }
479
480 static int KeyBlock_setVgroup( BPy_KeyBlock * self, PyObject * args  ){
481         char* text = NULL;
482     BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self; 
483     
484         text = PyString_AsString ( args );
485         if( !text )
486                 return EXPP_ReturnIntError( PyExc_TypeError,
487                                               "expected string argument" );                                                     
488         strncpy( kb->keyblock->vgroup, text , 32);
489
490         return 0;       
491 }
492 static int KeyBlock_setSlidermin( BPy_KeyBlock * self, PyObject * args  ){
493         return EXPP_setFloatClamped ( args, &self->keyblock->slidermin,
494                                                                 -10.0f,
495                                                                 10.0f );        
496 }
497 static int KeyBlock_setSlidermax( BPy_KeyBlock * self, PyObject * args  ){
498         return EXPP_setFloatClamped ( args, &self->keyblock->slidermax,
499                                                                 -10.0f,
500                                                                 10.0f );
501 }
502
503 static Curve *find_curve( Key *key )
504 {
505         Curve *cu;
506
507         if( !key )
508                 return NULL;
509
510         for( cu = G.main->curve.first; cu; cu = cu->id.next ) {
511                 if( cu->key == key )
512                         break;
513         }
514         return cu;
515 }
516
517 static PyObject *KeyBlock_getData( PyObject * self )
518 {
519         /* If this is a mesh key, data is an array of MVert coords.
520            If lattice, data is an array of BPoint coords
521            If curve, data is an array of BezTriple or BPoint */
522
523         char *datap;
524         int datasize;
525         int idcode;
526         int i;
527         Curve *cu;
528         Nurb* nu;
529         PyObject *l;
530         BPy_KeyBlock *kb = ( BPy_KeyBlock * ) self;
531         Key *key = kb->key;
532
533         if( !kb->keyblock->data )
534                 Py_RETURN_NONE;
535
536         l = PyList_New( kb->keyblock->totelem );
537         if( !l )
538                 return EXPP_ReturnPyObjError( PyExc_MemoryError,
539                                 "PyList_New() failed" );                                                        
540
541         idcode = GS( key->from->name );
542
543         switch(idcode) {
544         case ID_ME:
545
546                 for (i=0, datap = kb->keyblock->data; i<kb->keyblock->totelem; i++) {
547
548                         BPy_NMVert *mv = PyObject_NEW( BPy_NMVert, &NMVert_Type );
549                         MVert *vert = (MVert *) datap;
550
551                         mv->co[0]=vert->co[0];
552                         mv->co[1]=vert->co[1];
553                         mv->co[2]=vert->co[2];
554                         mv->no[0] = 0.0;
555                         mv->no[1] = 0.0;
556                         mv->no[2] = 0.0;
557
558                         mv->uvco[0] = mv->uvco[1] = mv->uvco[2] = 0.0;
559                         mv->index = i;
560                         mv->flag = 0;
561
562                         PyList_SetItem(l, i, ( PyObject * ) mv);
563
564                         datap += kb->key->elemsize;
565                 }
566                 break;
567
568         case ID_CU:
569                 cu = find_curve ( key );
570                 if( !cu )
571                         return EXPP_ReturnPyObjError( PyExc_RuntimeError,
572                                               "key is no linked to any curve!" );                                                       
573                 datasize = count_curveverts(&cu->nurb);
574                 nu = cu->nurb.first;
575                 if( nu->bezt ) {
576                         datasize /= 3;
577                         Py_DECREF (l);  
578                         l = PyList_New( datasize );
579                         for( i = 0, datap = kb->keyblock->data; i < datasize;
580                                         i++, datap += sizeof(float)*12 ) {
581                                 /* 
582                                  * since the key only stores the control point and not the
583                                  * other BezTriple attributes, build a Py_NEW BezTriple
584                                  */
585                                 PyObject *pybt = newBezTriple( (float *)datap );
586                                 PyList_SetItem( l, i, pybt );
587                         }
588                 } else {
589                         for( i = 0, datap = kb->keyblock->data; i < datasize;
590                                         i++, datap += kb->key->elemsize ) {
591                                 PyObject *pybt;
592                                 float *fp = (float *)datap;
593                                 pybt = Py_BuildValue( "[f,f,f]", fp[0],fp[1],fp[2]);
594                                 if( !pybt ) {
595                                         Py_DECREF( l );
596                                         return EXPP_ReturnPyObjError( PyExc_MemoryError,
597                                               "Py_BuildValue() failed" );                                                       
598                                 }
599                                 PyList_SetItem( l, i, pybt );
600                         }
601                 }
602                 break;
603
604         case ID_LT:
605
606                 for( i = 0, datap = kb->keyblock->data; i < kb->keyblock->totelem;
607                                 i++, datap += kb->key->elemsize ) {
608                         /* Lacking a python class for BPoint, use a list of three floats */
609                         PyObject *pybt;
610                         float *fp = (float *)datap;
611                         pybt = Py_BuildValue( "[f,f,f]", fp[0],fp[1],fp[2]);
612                         if( !pybt ) {
613                                 Py_DECREF( l );
614                                 return EXPP_ReturnPyObjError( PyExc_MemoryError,
615                                           "Py_BuildValue() failed" );                                                   
616                         }
617                         PyList_SetItem( l, i, pybt );
618                 }
619                 break;
620         }
621         
622         return l;
623 }
624
625 static PyObject *M_Key_Get( PyObject * self, PyObject * args )
626 {
627         char *name = NULL;
628         Key *key_iter;
629         char error_msg[64];
630         int i;
631
632         if( !PyArg_ParseTuple( args, "|s", &name ) )
633                 return ( EXPP_ReturnPyObjError( PyExc_TypeError,
634                         "expected string argument (or nothing)" ) );
635
636         if ( name ) {
637                 for (key_iter = G.main->key.first; key_iter; key_iter=key_iter->id.next) {
638                         if  (strcmp ( key_iter->id.name + 2, name ) == 0 ) {
639                                 return Key_CreatePyObject( key_iter );
640                         }
641                 }
642
643                 PyOS_snprintf( error_msg, sizeof( error_msg ),
644                         "Key \"%s\" not found", name );
645                 return ( EXPP_ReturnPyObjError ( PyExc_NameError, error_msg ) );
646                 
647         }
648
649         else {
650
651                 PyObject *keylist;
652
653                 keylist = PyList_New( BLI_countlist( &( G.main->key ) ) );
654
655                 for ( i=0, key_iter = G.main->key.first; key_iter; key_iter=key_iter->id.next, i++ ) {
656                         PyList_SetItem(keylist, i, Key_CreatePyObject(key_iter));
657                 }
658                 return keylist;
659         }
660 }
661
662 struct PyMethodDef M_Key_methods[] = {
663         {"Get", M_Key_Get, METH_VARARGS, "Get a key or all key names"},
664         {NULL, NULL, 0, NULL}
665 };
666
667 PyObject *Key_Init( void )
668 {
669         PyObject *submodule, *KeyTypes;
670
671         if( PyType_Ready( &Key_Type ) < 0)
672                 return NULL;
673
674         Key_Type.ob_type = &PyType_Type;
675         PyType_Ready( &KeyBlock_Type );
676
677         submodule =
678                 Py_InitModule3( "Blender.Key", M_Key_methods, "Key module" );
679
680         KeyTypes = PyConstant_New( );
681
682         PyConstant_Insert(( BPy_constant * ) KeyTypes, "MESH", PyInt_FromLong(KEY_TYPE_MESH));
683         PyConstant_Insert(( BPy_constant * ) KeyTypes, "CURVE", PyInt_FromLong(KEY_TYPE_CURVE));
684         PyConstant_Insert(( BPy_constant * ) KeyTypes, "LATTICE", PyInt_FromLong(KEY_TYPE_LATTICE));
685
686         PyModule_AddObject( submodule, "Types", KeyTypes);
687
688         /*
689         PyModule_AddIntConstant( submodule, "TYPE_MESH",    KEY_TYPE_MESH );
690         PyModule_AddIntConstant( submodule, "TYPE_CURVE",   KEY_TYPE_CURVE );
691         PyModule_AddIntConstant( submodule, "TYPE_LATTICE", KEY_TYPE_LATTICE );
692         */
693         return submodule;
694 }
695