Python API
authorKen Hughes <khughes@pacific.edu>
Wed, 8 Oct 2008 16:37:33 +0000 (16:37 +0000)
committerKen Hughes <khughes@pacific.edu>
Wed, 8 Oct 2008 16:37:33 +0000 (16:37 +0000)
----------
Add optional string argument to Object.newParticleSystem() method, so that
objects can link to existing particle systems (if the name of the particle
system is known).  Also cleans up some code in Object.c which accesses the
particle sys listbase.

source/blender/python/api2_2x/Object.c
source/blender/python/api2_2x/Particle.c
source/blender/python/api2_2x/doc/Object.py

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();
index 893ee077d67e789407628e1e4ac3cdd1c612fa0b..bbdc3942fc7e64a04bd1145673d3d283f9f39470 100644 (file)
@@ -138,7 +138,7 @@ static PyObject *Part_GetAge( BPy_PartSys * self, PyObject * args );
 /*****************************************************************************/
 /* Python Effect_Type callback function prototypes:                           */
 /*****************************************************************************/
-static PyObject *ParticleSys_repr( void );
+static PyObject *ParticleSys_repr( BPy_PartSys * self );
 
 /*****************************************************************************/
 /* The following string definitions are used for documentation strings.      */
@@ -415,13 +415,14 @@ PyTypeObject ParticleSys_Type = {
 
 /*****************************************************************************/
 /* Function:    PARTICLESYS_repr                                             */
-/* Description: This is a callback function for the BPy_Effect type. It      */
-/*              builds a meaninful string to represent effcte objects.       */
+/* Description: This is a callback function for the BPy_PartSys type. It     */
+/*              builds a meaningful string to represent effect objects.      */
 /*****************************************************************************/
 
-static PyObject *ParticleSys_repr( void )
+static PyObject *ParticleSys_repr( BPy_PartSys * self )
 {
-       return PyString_FromString( "ParticleSys" );
+       return PyString_FromFormat( "ParticleSys \"%s\"",
+                       self->psys->part->id.name+2 );
 }
 
 /*****************************************************************************/
index d56a547911ede3fb69ec60bce355ab60213572ff..c5ce7a4d2bfd7961021e3a520c871f9e029aa6c7 100644 (file)
@@ -656,9 +656,13 @@ class Object:
                Return a list of particle systems linked to this object (see Blender.Particle).
                """
                
-       def newParticleSystem():
+       def newParticleSystem(name = None):
                """
-               Link a new particle system (see Blender.Particle).
+               Link a particle system (see Blender.Particle).  If no name is
+               given, a new particle system is created.  If a name is given and a 
+               particle system  with that name exists, it is linked to the object.
+               @type name: string
+               @param name: The name of the requested Particle system (optional).
                """
                
        def addVertexGroupsFromArmature(object):