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