Python API
[blender.git] / source / blender / python / api2_2x / Object.c
index 4495dc6c65555f3fcd27fe837b634b85fc8f8595..a6ee9b7d540ee9c5554ad6d50e0fa560a76b0c27 100644 (file)
@@ -342,9 +342,8 @@ static int setupSB(Object* ob); /*Make sure Softbody Pointer is initialized */
 static int setupPI(Object* ob);
 
 static PyObject *Object_getParticleSys( BPy_Object * self );
-/* fixme Object_newParticleSys( self, default-partsys-name ) */
 static PyObject *Object_addVertexGroupsFromArmature( BPy_Object * self, PyObject * args);
-static PyObject *Object_newParticleSys( BPy_Object * self );
+static PyObject *Object_newParticleSys( BPy_Object * self, PyObject * args );
 static PyObject *Object_buildParts( BPy_Object * self );
 static PyObject *Object_clearIpo( BPy_Object * self );
 static PyObject *Object_clrParent( BPy_Object * self, PyObject * args );
@@ -478,7 +477,7 @@ static PyMethodDef BPy_Object_methods[] = {
        /* name, method, flags, doc */
        {"getParticleSystems", ( PyCFunction ) Object_getParticleSys, METH_NOARGS,
         "Return a list of particle systems"},
-       {"newParticleSystem", ( PyCFunction ) Object_newParticleSys, METH_NOARGS,
+       {"newParticleSystem", ( PyCFunction ) Object_newParticleSys, METH_VARARGS,
         "Create and link a new particle system"},
        {"addVertexGroupsFromArmature" , ( PyCFunction ) Object_addVertexGroupsFromArmature, METH_VARARGS,
         "Add vertex groups from armature using the bone heat method"},
@@ -1044,42 +1043,48 @@ static PyObject *M_Object_Duplicate( PyObject * self_unused,
 /*****************************************************************************/
 
 PyObject *Object_getParticleSys( BPy_Object * self ){
-       ParticleSystem *blparticlesys = 0;
+       PyObject *list;
+       ParticleSystem *psys= NULL;
        Object *ob = self->object;
-       PyObject *partsyslist,*current;
-
-       blparticlesys = ob->particlesystem.first;
-
-       partsyslist = PyList_New( 0 );
-
-       if (!blparticlesys)
-               return partsyslist;
+       int i= 0;
 
-/* fixme:  for(;;) */
-       current = ParticleSys_CreatePyObject( blparticlesys, ob );
-       PyList_Append(partsyslist,current);
-       Py_DECREF(current);
+       list = PyList_New( BLI_countlist( &ob->particlesystem ) );
+       if( !list )
+               return EXPP_ReturnPyObjError( PyExc_MemoryError,
+                               "PyList_New() failed" );
 
-       while((blparticlesys = blparticlesys->next)){
-               current = ParticleSys_CreatePyObject( blparticlesys, ob );
-               PyList_Append(partsyslist,current);
-               Py_DECREF(current);
-       }
+       for( psys=ob->particlesystem.first; psys; psys=psys->next )
+               PyList_SET_ITEM( list, i++, ParticleSys_CreatePyObject( psys, ob ) );
 
-       return partsyslist;
+       return list;
 }
 
-PyObject *Object_newParticleSys( BPy_Object * self ){
+PyObject *Object_newParticleSys( BPy_Object * self, PyObject * args ) {
        ParticleSystem *psys = 0;
        ParticleSystem *rpsys = 0;
        ModifierData *md;
        ParticleSystemModifierData *psmd;
        Object *ob = self->object;
-/*     char *name = NULL;  optional name param */
+       char *name = NULL;
        ID *id;
-       int nr;
+       int nr; 
+
+       if( !PyArg_ParseTuple( args, "|s", &name ) )
+               return EXPP_ReturnPyObjError( PyExc_TypeError,
+                               "expected a string or nothing" );
 
-       id = (ID *)psys_new_settings("PSys", G.main);
+       if( name ) {
+               for( id= G.main->particle.first; id; id= id->next ) {
+                       if( !strcmp( name, id->name + 2 ) )
+                               break;
+               }
+               if( !id )
+                       return EXPP_ReturnPyObjError( PyExc_AttributeError,
+                                       "specified particle system not found" );
+               else
+                       id->us++;
+       } else
+               id = (ID *)psys_new_settings("PSys", G.main);
 
        psys = MEM_callocN(sizeof(ParticleSystem), "particle_system");
        psys->pointcache = BKE_ptcache_add();