py api
[blender.git] / source / blender / python / api2_2x / BPyModule.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): Campbell Barton
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31 */
32
33
34 #include "MEM_guardedalloc.h"   /* for MEM_callocN */
35 #include "DNA_space_types.h"    /* SPACE_VIEW3D, SPACE_SEQ */
36 #include "DNA_scene_types.h"
37 #include "DNA_object_types.h" /* LibBlockSeq_new */
38 #include "DNA_texture_types.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_ipo_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_world_types.h"
43 #include "DNA_vfont_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_sound_types.h"
46 #include "DNA_text_types.h"
47 #include "DNA_action_types.h"
48 #include "DNA_meta_types.h"
49
50 /* Use the add_* from BKE_* */
51 #include "BKE_global.h"
52 #include "BKE_utildefines.h" /* clamp */
53 #include "BKE_armature.h"
54 #include "BKE_ipo.h"
55 #include "BKE_image.h"
56 #include "BKE_main.h"
57 #include "BKE_library.h"
58 #include "BKE_lattice.h"
59 #include "BKE_object.h"
60 #include "BKE_scene.h"
61 #include "BKE_sca.h" /*free_text_controllers*/
62 #include "BKE_font.h"
63 #include "BKE_mball.h"
64 #include "BKE_mesh.h"
65 #include "BKE_curve.h"
66 #include "BKE_material.h"
67 #include "BKE_group.h"
68 #include "BKE_text.h"
69 #include "BKE_texture.h"
70 #include "BKE_world.h"
71
72 #include "BLI_blenlib.h" /* BLI_countlist */
73 #include "BIF_drawscene.h"      /* for set_scene */
74 #include "BIF_screen.h"         /* curarea */
75 #include "BIF_drawimage.h" /* what image */
76 #include "BIF_drawtext.h" /* unlink_text */
77 #include "BIF_editsound.h" /* sound_new_sound */
78 #include "BIF_editaction.h" /* add_empty_action */
79
80 /* python types */
81 #include "../BPY_extern.h" /* clearing scriptlinks */
82
83 #include "gen_utils.h"
84 #include "gen_library.h" /* generic ID functions */
85
86 #include "Object.h"
87 #include "Camera.h"
88 #include "Armature.h"
89 #include "Lamp.h"
90 /*#include " CurNurb.h" do we need this ? */
91 #include "Curve.h"
92 #include "NMesh.h"
93 #include "Mesh.h"
94 #include "Lattice.h"
95 #include "Metaball.h"
96 #include "Text.h"
97 #include "Text3d.h"
98 #include "Font.h"
99 #include "Group.h"
100 #include "World.h"
101 #include "Texture.h"
102 #include "Ipo.h"
103 #include "Text.h"
104 #include "Sound.h"
105 #include "NLA.h"
106 #include "BPyModule.h"
107 #include "Scene.h"
108 #include "Library.h"
109
110 #include "Config.h" /* config pydata */
111
112 /* used only for texts.active */
113 #include "BIF_screen.h"
114 #include "DNA_space_types.h"
115 #include "DNA_screen_types.h"
116
117 extern VFont *get_builtin_font(void);
118
119 static PyObject *LibBlockSeq_CreatePyObject( Link *iter, int type )
120 {
121         BPy_LibBlockSeq *seq = PyObject_NEW( BPy_LibBlockSeq, &LibBlockSeq_Type);
122         seq->iter = iter;
123         seq->type = type;
124         return (PyObject *)seq;
125 }
126
127
128 static int LibBlockSeq_len( BPy_LibBlockSeq * self )
129 {
130         ListBase *lb = wich_libbase(G.main, self->type);
131         return BLI_countlist( lb );
132 }
133
134 static PyObject * LibBlockSeq_subscript(BPy_LibBlockSeq * self, PyObject *key)
135 {
136         char *name;
137         char *lib= NULL;
138         char use_lib = 0;
139         ID *id;
140         
141         id = (ID *)wich_libbase(G.main, self->type)->first;
142         
143         if ( PyString_Check(key) ) {
144                 name = PyString_AsString ( key );
145         } else if (PyTuple_Check(key) && (PyTuple_Size(key) == 2) ) {
146                 PyObject *pydata;
147                 use_lib = 1;
148                 
149                 /* Get the first arg */
150                 pydata = PyTuple_GET_ITEM(key, 0);
151                 if (!PyString_Check(pydata)) {
152                         return EXPP_ReturnPyObjError( PyExc_TypeError,
153                                 "the data name must be a string" );
154                 }
155                 
156                 name = PyString_AsString ( pydata );
157                 
158                 /* Get the second arg */
159                 pydata = PyTuple_GET_ITEM(key, 1);
160                 if (pydata == Py_None) {
161                         lib = NULL; /* data must be local */
162                 } else if (PyString_Check(pydata)) {
163                         lib = PyString_AsString ( pydata );
164                         if (!strcmp( "", lib)) {
165                                 lib = NULL; /* and empty string also means data must be local */
166                         }
167                 } else {
168                         return EXPP_ReturnPyObjError( PyExc_TypeError,
169                                 "the lib name must be a string or None" );
170                 }
171         } else {
172                 return EXPP_ReturnPyObjError( PyExc_TypeError,
173                                 "expected the a name string or a tuple (lib, name)" );
174         }
175         
176         for (; id; id = id->next) {
177                 if(!strcmp( name, id->name+2 )) {
178                         if (
179                                 (!use_lib) || /* any data, local or external lib data */
180                                 (use_lib && !lib && !id->lib) || /* only local */
181                                 (lib && use_lib && id->lib && (!strcmp( id->lib->name, lib))) /* only external lib */
182                         )
183                         {
184                                 return GetPyObjectFromID(id);
185                         }
186                 }
187         }
188         return ( EXPP_ReturnPyObjError
189                                  ( PyExc_KeyError, "Requested data does not exist") );
190 }
191
192 static PyMappingMethods LibBlockSeq_as_mapping = {
193         ( inquiry ) LibBlockSeq_len,    /* mp_length */
194         ( binaryfunc ) LibBlockSeq_subscript,   /* mp_subscript */
195         ( objobjargproc ) 0,    /* mp_ass_subscript */
196 };
197
198
199 /************************************************************************
200  *
201  * Python LibBlockSeq_Type iterator (iterates over GroupObjects)
202  *
203  ************************************************************************/
204
205 /*
206  * Initialize the interator index
207  */
208
209 static PyObject *LibBlockSeq_getIter( BPy_LibBlockSeq * self )
210 {
211         /* we need to get the first base, but for selected context we may need to advance
212         to the first selected or first conext base */
213         
214         ListBase *lb;
215         Link *link;
216         lb = wich_libbase(G.main, self->type);
217         
218         link = lb->first;
219         
220         /* create a new iterator if were alredy using this one */
221         if (self->iter==NULL) {
222                 self->iter = link;
223                 return EXPP_incr_ret ( (PyObject *) self );
224         } else {
225                 return LibBlockSeq_CreatePyObject(link, self->type);
226         }
227 }
228
229 /*
230  * Return next LibBlockSeq iter.
231  */
232
233 static PyObject *LibBlockSeq_nextIter( BPy_LibBlockSeq * self )
234 {
235         PyObject *object;
236         Link *link;
237         if( !(self->iter) ) {
238                 self->iter= NULL;
239                 return EXPP_ReturnPyObjError( PyExc_StopIteration,
240                                 "iterator at end" );
241         }
242         
243         object = GetPyObjectFromID((ID *)self->iter);
244         
245         link= self->iter->next;
246         self->iter= link;
247         return object;
248 }
249
250 PyObject *LibBlockSeq_getActive(BPy_LibBlockSeq *self)
251 {
252         switch (self->type) {
253         case ID_SCE:
254                 if ( !G.scene ) {
255                         Py_RETURN_NONE;
256                 } else {
257                         return Scene_CreatePyObject( ( Scene * ) G.scene );
258                 }
259                 
260                 break;
261         case ID_IM:
262                 if (!G.sima || !G.sima->image) {
263                         Py_RETURN_NONE;
264                 } else {
265                         what_image( G.sima );   /* make sure image data exists */
266                         return Image_CreatePyObject( G.sima->image );
267                 }
268                 break;
269         case ID_TXT: {
270                         SpaceText *st= curarea->spacedata.first;
271                         
272                         if (st->spacetype!=SPACE_TEXT || st->text==NULL) {
273                                 Py_RETURN_NONE;
274                         } else {
275                                 return Text_CreatePyObject( st->text );
276                         }
277                 }
278         }
279         
280         return EXPP_ReturnPyObjError( PyExc_TypeError,
281                         "Only Scene and Image types have the active attribute" );
282 }
283
284 static int LibBlockSeq_setActive(BPy_LibBlockSeq *self, PyObject *value)
285 {
286         switch (self->type) {
287         case ID_SCE:
288                 if (!BPy_Scene_Check(value)) {
289                         return EXPP_ReturnIntError(PyExc_TypeError,
290                                         "Must be a scene" );
291                 } else {
292                         BPy_Scene *bpydata;
293                         Scene *data;
294                         
295                         bpydata = (BPy_Scene *)value;
296                         data= bpydata->scene;
297                         
298                         if (!data)
299                                 return EXPP_ReturnIntError(PyExc_RuntimeError,
300                                         "This Scene has been removed" );
301                         
302                         if (data != G.scene) {
303                                 set_scene( data );
304                                 scene_update_for_newframe(data, data->lay);
305                         }
306                 }
307                 return 0;
308                 
309         case ID_IM:
310                 if (!BPy_Image_Check(value)) {
311                         return EXPP_ReturnIntError(PyExc_TypeError,
312                                         "Must be a scene" );
313                 } else {
314                         BPy_Image *bpydata;
315                         Image *data;
316                         
317                         if (!G.sima) 
318                                 return 0;
319                         
320                         bpydata = (BPy_Image *)value;
321                         data= bpydata->image;
322                         
323                         if (!data)
324                                 return EXPP_ReturnIntError(PyExc_RuntimeError,
325                                         "This Scene has been removed" );
326                         
327                         if (data != G.sima->image)
328                                 G.sima->image= data;
329                 }
330                 return 0;
331         
332         case ID_TXT:
333                 if (!BPy_Text_Check(value)) {
334                         return EXPP_ReturnIntError(PyExc_TypeError,
335                                         "Must be a text" );
336                 } else {
337                         SpaceText *st= curarea->spacedata.first;        
338                         Text *data = ((BPy_Text *)value)->text;
339                         
340                         if( !data )
341                                 return EXPP_ReturnIntError( PyExc_RuntimeError,
342                                                       "This object isn't linked to a Blender Text Object" );
343                         if(st->spacetype!=SPACE_TEXT)
344                                 return 0;
345                         st->text = data;
346                 }
347                 return 0;
348         }
349         
350         return EXPP_ReturnIntError( PyExc_TypeError,
351                         "Only Scene and Image types have the active attribute" );
352 }
353
354 static int LibBlockSeq_setTag(BPy_LibBlockSeq *self, PyObject *value)
355 {
356         int param = PyObject_IsTrue( value );
357         ID *id;
358         
359         if( param == -1 )
360                 return EXPP_ReturnIntError( PyExc_TypeError,
361                                 "expected int argument in range [0,1]" );
362         
363         id = (ID *)wich_libbase(G.main, self->type)->first;
364         
365         if (param) {
366                 for (; id; id = id->next) {
367                         id->flag |= LIB_DOIT;
368                 }
369         } else {
370                 for (; id; id = id->next) {
371                         id->flag &= ~LIB_DOIT;
372                 }
373         }
374         
375         return 0;       
376 }
377
378
379 /* New Data, internal functions */
380 Mesh *add_mesh__internal(char *name)
381 {
382         Mesh *mesh = add_mesh(name); /* doesn't return NULL now, but might someday */
383         
384         /* Bound box set to null needed because a new mesh is initialized
385         with a bounding box of -1 -1 -1 -1 -1 -1
386         if its not set to null the bounding box is not re-calculated
387         when ob.getBoundBox() is called.*/
388         MEM_freeN(mesh->bb);
389         mesh->bb= NULL;
390         return mesh;
391 }
392
393 /* used for new and load */
394 PyObject *LibBlockSeq_new(BPy_LibBlockSeq *self, PyObject * args, PyObject *kwd)
395 {
396         ID *id = NULL;
397         char *name=NULL, *filename=NULL, *data_type=NULL;
398         int img_width=256, img_height=256;
399         short data_code = 0;
400         int user_count = 0;
401         
402         /* Load from file */
403         if ( (  self->type==ID_IM || self->type==ID_VF          ||
404                         self->type==ID_SO || self->type==ID_TXT)        &&
405                                 ( PyTuple_Size( args ) < 3              ))
406         {
407                 static char *kwlist[] = {"name", "filename", NULL};
408                 
409                 if(PyArg_ParseTupleAndKeywords(args, kwd, "|ss", kwlist, &name, &filename) && filename ) {
410                         PyObject *ret;
411                         
412                         if (strlen(filename) > FILE_MAXDIR + FILE_MAXFILE - 1)
413                                         return ( EXPP_ReturnPyObjError( PyExc_IOError,
414                                                                 "filename too long" ) );
415                         
416                         if (self->type == ID_IM) {
417                                 Image *img = BKE_add_image_file( filename );
418                                 if (!img)
419                                         return ( EXPP_ReturnPyObjError( PyExc_IOError,
420                                                                 "couldn't load image" ) );
421                                 ret = Image_CreatePyObject( img );
422                                 
423                         } else if (self->type == ID_VF) {
424                                 VFont *vf = load_vfont (filename);
425                                 if (!vf)
426                                         return EXPP_ReturnPyObjError( PyExc_IOError,
427                                                                 "couldn't load font" );
428                                 ret = Font_CreatePyObject(vf);
429                                 
430                         } else if (self->type == ID_SO) {
431                                 bSound  *snd = sound_new_sound( filename );
432                                 if (!snd)
433                                         return EXPP_ReturnPyObjError( PyExc_IOError,
434                                                                 "couldn't load sound" );
435                                 ret = Sound_CreatePyObject(snd);
436                                 
437                         } else if (self->type == ID_TXT) {
438                                 Text *txt = NULL;
439                                 txt = add_text( filename );
440                                 if( !txt )
441                                         return EXPP_ReturnPyObjError( PyExc_IOError,
442                                                       "couldn't load text" );
443                                 ret = Text_CreatePyObject(txt);
444                         }
445                         
446                         if (!ret)
447                                         return EXPP_ReturnPyObjError( PyExc_IOError,
448                                                       "couldn't create pyobject on load, unknown error" );
449                         if (name) {
450                                 ID *id = ((BPy_GenericLib *)ret)->id;
451                                 rename_id( id, name );
452                         }
453                         return ret;
454                 }
455         }
456         
457         /* New Data */
458         if (self->type == ID_IM) {
459                 /* Image, accepts width and height*/
460                 if( !PyArg_ParseTuple( args, "|sii", &name, &img_width, &img_height ) )
461                         return EXPP_ReturnPyObjError( PyExc_TypeError,
462                                 "one string and two ints expected as arguments" );
463                 CLAMP(img_width,  4, 5000);
464                 CLAMP(img_height, 4, 5000);
465         
466         } else if (self->type == ID_CU) {
467                 /* Curve, needs name and type strings */
468                 if( !PyArg_ParseTuple( args, "ss", &name, &data_type ) )
469                         return EXPP_ReturnPyObjError( PyExc_TypeError,
470                                 "two strings expected as arguments" );
471                 
472                 if(      !strcmp( data_type, "Curve" ) )                data_code = OB_CURVE;
473                 else if( !strcmp( data_type, "Text3d" ) )                       data_code = OB_FONT;/*
474                 else if( !strcmp( data_type, "Surf" ) )                 data_code = OB_SURF;*/
475                 else return EXPP_ReturnPyObjError( PyExc_TypeError,
476                                 "Second argument for Curve type incorrect\t\nmust be a string in (Curve or Text - Surf is not supported yet)" );
477         
478         } else if (self->type == ID_IP) {
479                 /* IPO, needs name and type strings */
480                 if( !PyArg_ParseTuple( args, "ss", &name, &data_type ) )
481                         return EXPP_ReturnPyObjError( PyExc_TypeError,
482                                 "two strings expected as arguments" );
483                 
484                 if(      !strcmp( data_type, "Object" ) )               data_code = ID_OB;
485                 else if( !strcmp( data_type, "Camera" ) )               data_code = ID_CA;
486                 else if( !strcmp( data_type, "World" ) )                data_code = ID_WO;
487                 else if( !strcmp( data_type, "Material" ) )             data_code = ID_MA;
488                 else if( !strcmp( data_type, "Texture" ) )              data_code = ID_TE;
489                 else if( !strcmp( data_type, "Lamp" ) )                 data_code = ID_LA;
490                 else if( !strcmp( data_type, "Action" ) )               data_code = ID_PO;
491                 else if( !strcmp( data_type, "Constraint" ) )   data_code = ID_CO;
492                 else if( !strcmp( data_type, "Sequence" ) )             data_code = ID_SEQ;
493                 else if( !strcmp( data_type, "Curve" ) )                data_code = ID_CU;
494                 else if( !strcmp( data_type, "Key" ) )                  data_code = ID_KE;
495                 else return EXPP_ReturnPyObjError( PyExc_TypeError,
496                                 "Second argument for IPO type incorrect\t\nmust be a string in (Object, Camera, World, Material, Texture, Lamp, Action, Sequence, Curve, Key)" );
497                 
498         } else {
499                 /* Other types only need the name */
500                 if( !PyArg_ParseTuple( args, "|s", &name ) )
501                         return EXPP_ReturnPyObjError( PyExc_TypeError,
502                                 "new(name) - name must be a string argument" );
503         }
504         
505         switch (self->type) {
506         case ID_SCE:
507                 id = (ID *)add_scene( name );
508                 user_count = 1;
509                 break;
510         case ID_OB:
511                 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
512                         "Add objects through the scenes objects iterator" );
513         case ID_ME:
514                 id = (ID *)add_mesh__internal( name );
515                 break;
516         case ID_CU:
517                 id = (ID *)add_curve( name, data_code );
518                 if (data_code==OB_FONT) {
519                         Text3d *text3d = (Text3d *)id;
520                         text3d->vfont= get_builtin_font();
521                         text3d->vfont->id.us++;
522                         text3d->str= MEM_mallocN(sizeof(wchar_t), "str");
523                         text3d->str[0] = '\0';
524                         text3d->totbox= text3d->actbox= 1;
525                         text3d->tb= MEM_callocN(MAXTEXTBOX*sizeof(TextBox), "textbox");
526                         text3d->tb[0].w = text3d->tb[0].h = 0.0;
527                 
528                 } /*else { CURVE - Dont need to do anything } */
529                 break;
530         case ID_MB:
531                 id = (ID *)add_mball( name );
532                 break;
533         case ID_MA:
534                 id = (ID *)add_material( name );
535                 break;
536         case ID_TE:
537                 id = (ID *)add_texture( name );
538                 break;
539         case ID_IM: 
540         {
541                 id = (ID *)BKE_add_image_size(img_width, img_height, name, 0);
542                 if( !id )
543                         return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
544                                 "couldn't create PyObject Image_Type" ) );
545                 /* new images have zero user count */
546                 break;
547         }
548         case ID_LT:
549                 id = (ID *)add_lattice( name );
550                 break;
551         case ID_LA:
552                 id = (ID *)add_lamp( name );
553                 break;
554         case ID_CA:
555                 id = (ID *)add_camera( name );
556                 break;
557         case ID_IP:
558                 id = (ID *)add_ipo( name, data_code );
559                 break;
560         case ID_WO:
561                 id = (ID *)add_world( name );
562                 break;
563         case ID_VF:
564                 return EXPP_ReturnPyObjError( PyExc_TypeError,
565                         "Cannot create new fonts, use the load() function to load from a file" );
566         case ID_TXT:
567                 id = (ID *)add_empty_text( name );
568                 user_count = 1;
569                 break;
570         case ID_SO:
571                 return EXPP_ReturnPyObjError( PyExc_TypeError,
572                         "Cannot create new sounds, use the load() function to load from a file" );
573         case ID_GR:     
574                 id = (ID *)add_group( name );
575                 user_count = 1;
576                 break;
577         case ID_AR:
578                 id = (ID *)add_armature( name );
579                 break;
580         case ID_AC:
581                 id = (ID *)add_empty_action( name );
582                 user_count = 1;
583                 break;
584         }
585
586         if (!id)
587                 Py_RETURN_NONE;
588
589         /* set some types user count to 1, otherwise zero */
590         id->us = user_count;
591         
592         return GetPyObjectFromID(id);
593 }
594
595
596 PyObject *LibBlockSeq_unlink(BPy_LibBlockSeq *self, PyObject * args)
597 {
598         PyObject *pyobj;
599         
600         switch (self->type) {
601         case ID_SCE:
602                 if( !PyArg_ParseTuple( args, "O!", &Scene_Type, &pyobj ) ) {
603                         return EXPP_ReturnPyObjError( PyExc_TypeError,
604                                         "expected Scene object" );
605                 } else {
606                         BPy_Scene *bpydata;
607                         Scene *data;
608                         
609                         bpydata = (BPy_Scene *)pyobj;
610                         data = bpydata->scene;
611                         
612                         if (!data)
613                                 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
614                                                 "This Scene has been removed" );
615                         
616                         /* Run the removal code */
617                         free_libblock( &G.main->scene, data );
618                         bpydata->scene= NULL;
619                         
620                         Py_RETURN_NONE;
621                 }
622         case ID_GR:
623                 if( !PyArg_ParseTuple( args, "O!", &Group_Type, &pyobj ) ) {
624                         return EXPP_ReturnPyObjError( PyExc_TypeError,
625                                         "expected Group object" );
626                 } else {
627                         BPy_Group *bpydata;
628                         Group *data;
629                         
630                         bpydata = (BPy_Group *)pyobj;
631                         data = bpydata->group;
632                         
633                         if (!data)
634                                 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
635                                                 "This Group has been removed alredy" );
636                         
637                         /* Run the removal code */
638                         free_group(data);
639                         unlink_group(data);
640                         data->id.us= 0;
641                         free_libblock( &G.main->group, data );
642                         bpydata->group= NULL;
643                         
644                         Py_RETURN_NONE;
645                 }
646
647         case ID_TXT:
648                 if( !PyArg_ParseTuple( args, "O!", &Text_Type, &pyobj ) ) {
649                         return EXPP_ReturnPyObjError( PyExc_TypeError,
650                                         "expected Text object" );
651                 } else {
652                         BPy_Text *bpydata;
653                         Text *data;
654                         bpydata = (BPy_Text *)pyobj;
655                         data = bpydata->text;
656                         
657                         if (!data)
658                                 return EXPP_ReturnPyObjError( PyExc_RuntimeError,
659                                                 "This Group has been removed alredy" );
660                         
661                         /* Run the removal code */
662                         BPY_clear_bad_scriptlinks( data );
663                         free_text_controllers( data );
664                         unlink_text( data );
665                         free_libblock( &G.main->text, data );
666                         bpydata->text = NULL;
667                         
668                         Py_RETURN_NONE;
669                 }
670         }
671         return EXPP_ReturnPyObjError( PyExc_TypeError,
672                                       "Only types Scene, Group and Text can unlink" );  
673 }
674
675 static int LibBlockSeq_compare( BPy_LibBlockSeq * a, BPy_LibBlockSeq * b )
676 {
677         return ( a->type == b->type) ? 0 : -1;  
678 }
679
680 /*
681  * repr function
682  * callback functions building meaninful string to representations
683  */
684 static PyObject *LibBlockSeq_repr( BPy_LibBlockSeq * self )
685 {
686         return PyString_FromFormat( "[LibBlockSeq Iterator]");
687 }
688
689 static PyGetSetDef LibBlockSeq_getseters[] = {
690         {"active",
691          (getter)LibBlockSeq_getActive, (setter)LibBlockSeq_setActive,
692          "active object",
693          NULL},
694         {"tag",
695          (getter)NULL, (setter)LibBlockSeq_setTag,
696          "tag all data in True or False (write only)",
697          NULL},
698         {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
699 };
700
701 static struct PyMethodDef BPy_LibBlockSeq_methods[] = {
702         {"new", (PyCFunction)LibBlockSeq_new, METH_VARARGS | METH_KEYWORDS,
703                 "(name) - Create a new object in this scene from the obdata given and return a new object"},
704         {"unlink", (PyCFunction)LibBlockSeq_unlink, METH_VARARGS,
705                 "unlinks the object from the scene"},
706         {NULL, NULL, 0, NULL}
707 };
708
709 /*****************************************************************************/
710 /* Python LibBlockSeq_Type structure definition:                               */
711 /*****************************************************************************/
712 PyTypeObject LibBlockSeq_Type = {
713         PyObject_HEAD_INIT( NULL )  /* required py macro */
714         0,                          /* ob_size */
715         /*  For printing, in format "<module>.<name>" */
716         "Blender LibBlockSeq",           /* char *tp_name; */
717         sizeof( BPy_LibBlockSeq ),       /* int tp_basicsize; */
718         0,                          /* tp_itemsize;  For allocation */
719
720         /* Methods to implement standard operations */
721
722         NULL,                                           /* destructor tp_dealloc; */
723         NULL,                       /* printfunc tp_print; */
724         NULL,                       /* getattrfunc tp_getattr; */
725         NULL,                       /* setattrfunc tp_setattr; */
726         ( cmpfunc ) LibBlockSeq_compare, /* cmpfunc tp_compare; */
727         ( reprfunc ) LibBlockSeq_repr,   /* reprfunc tp_repr; */
728
729         /* Method suites for standard classes */
730
731         NULL,                       /* PyNumberMethods *tp_as_number; */
732         NULL,       /* PySequenceMethods *tp_as_sequence; */
733         &LibBlockSeq_as_mapping,                       /* PyMappingMethods *tp_as_mapping; */
734
735         /* More standard operations (here for binary compatibility) */
736
737         NULL,                       /* hashfunc tp_hash; */
738         NULL,                       /* ternaryfunc tp_call; */
739         NULL,                       /* reprfunc tp_str; */
740         NULL,                       /* getattrofunc tp_getattro; */
741         NULL,                       /* setattrofunc tp_setattro; */
742
743         /* Functions to access object as input/output buffer */
744         NULL,                       /* PyBufferProcs *tp_as_buffer; */
745
746   /*** Flags to define presence of optional/expanded features ***/
747         Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
748
749         NULL,                       /*  char *tp_doc;  Documentation string */
750   /*** Assigned meaning in release 2.0 ***/
751         /* call function for all accessible objects */
752         NULL,                       /* traverseproc tp_traverse; */
753
754         /* delete references to contained objects */
755         NULL,                       /* inquiry tp_clear; */
756
757   /***  Assigned meaning in release 2.1 ***/
758   /*** rich comparisons ***/
759         NULL,                       /* richcmpfunc tp_richcompare; */
760
761   /***  weak reference enabler ***/
762         0,                          /* long tp_weaklistoffset; */
763
764   /*** Added in release 2.2 ***/
765         /*   Iterators */
766         ( getiterfunc) LibBlockSeq_getIter, /* getiterfunc tp_iter; */
767         ( iternextfunc ) LibBlockSeq_nextIter, /* iternextfunc tp_iternext; */
768
769   /*** Attribute descriptor and subclassing stuff ***/
770         BPy_LibBlockSeq_methods,       /* struct PyMethodDef *tp_methods; */
771         NULL,                       /* struct PyMemberDef *tp_members; */
772         LibBlockSeq_getseters,       /* struct PyGetSetDef *tp_getset; */
773         NULL,                       /* struct _typeobject *tp_base; */
774         NULL,                       /* PyObject *tp_dict; */
775         NULL,                       /* descrgetfunc tp_descr_get; */
776         NULL,                       /* descrsetfunc tp_descr_set; */
777         0,                          /* long tp_dictoffset; */
778         NULL,                       /* initproc tp_init; */
779         NULL,                       /* allocfunc tp_alloc; */
780         NULL,                       /* newfunc tp_new; */
781         /*  Low-level free-memory routine */
782         NULL,                       /* freefunc tp_free;  */
783         /* For PyObject_IS_GC */
784         NULL,                       /* inquiry tp_is_gc;  */
785         NULL,                       /* PyObject *tp_bases; */
786         /* method resolution order */
787         NULL,                       /* PyObject *tp_mro;  */
788         NULL,                       /* PyObject *tp_cache; */
789         NULL,                       /* PyObject *tp_subclasses; */
790         NULL,                       /* PyObject *tp_weaklist; */
791         NULL
792 };
793
794
795 /*-----------------------------BPy module Init())-----------------------------*/
796
797 static char M_BPy_Init_doc[] = "The bpy module";
798
799 void M_BPy_Init( void )
800 {
801         PyObject *module;
802         PyObject *dict;
803         
804         
805         PyType_Ready( &LibBlockSeq_Type );
806         PyType_Ready( &Config_Type );
807         
808         /*submodule = Py_InitModule3( "Blender.Main", NULL, M_Main_doc );*/
809         module = Py_InitModule3( "bpy", NULL, M_BPy_Init_doc );
810         dict = PyModule_GetDict( module );
811
812         PyDict_SetItemString( dict, "libraries", Library_Init(  ) );
813         
814         /* Python Data Types */
815         PyModule_AddObject( module, "scenes", LibBlockSeq_CreatePyObject(NULL, ID_SCE) );
816         PyModule_AddObject( module, "objects", LibBlockSeq_CreatePyObject(NULL, ID_OB) );
817         PyModule_AddObject( module, "meshes", LibBlockSeq_CreatePyObject(NULL, ID_ME) );
818         PyModule_AddObject( module, "curves", LibBlockSeq_CreatePyObject(NULL, ID_CU) );
819         PyModule_AddObject( module, "metaballs", LibBlockSeq_CreatePyObject(NULL, ID_MB) );
820         PyModule_AddObject( module, "materials", LibBlockSeq_CreatePyObject(NULL, ID_MA) );
821         PyModule_AddObject( module, "textures", LibBlockSeq_CreatePyObject(NULL, ID_TE) );
822         PyModule_AddObject( module, "images", LibBlockSeq_CreatePyObject(NULL, ID_IM) );
823         PyModule_AddObject( module, "lattices", LibBlockSeq_CreatePyObject(NULL, ID_LT) );
824         PyModule_AddObject( module, "lamps", LibBlockSeq_CreatePyObject(NULL, ID_LA) );
825         PyModule_AddObject( module, "cameras", LibBlockSeq_CreatePyObject(NULL, ID_CA) );
826         PyModule_AddObject( module, "ipos", LibBlockSeq_CreatePyObject(NULL, ID_IP) );
827         PyModule_AddObject( module, "worlds", LibBlockSeq_CreatePyObject(NULL, ID_WO) );
828         PyModule_AddObject( module, "fonts", LibBlockSeq_CreatePyObject(NULL, ID_VF) );
829         PyModule_AddObject( module, "texts", LibBlockSeq_CreatePyObject(NULL, ID_TXT) );
830         PyModule_AddObject( module, "sounds", LibBlockSeq_CreatePyObject(NULL, ID_SO) );
831         PyModule_AddObject( module, "groups", LibBlockSeq_CreatePyObject(NULL, ID_GR) );
832         PyModule_AddObject( module, "armatures", LibBlockSeq_CreatePyObject(NULL, ID_AR) );
833         PyModule_AddObject( module, "actions", LibBlockSeq_CreatePyObject(NULL, ID_AC) );
834         
835         /* Other Types */
836         PyModule_AddObject( module, "config", Config_CreatePyObject() );
837 }