Blender 2.41 Python Particles enhancement

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
AkhIL
Posts: 0
Joined: Tue Aug 30, 2005 12:51 pm
Location: Kemerovo, Siberia
Contact:

Blender 2.41 Python Particles enhancement

Post by AkhIL »

Two new BPy cals for Effect module
getParticlesAge(time) - gives list of particle ages
getParticlesID(time) - particles id

Code: Select all

*** Effect.c.bak	2006-05-19 20:08:22.000000000 +0800
--- Effect.c	2006-05-19 20:50:40.000000000 +0800
***************
*** 159,168 ****
--- 159,170 ----
  static PyObject *Effect_getVertGroup( BPy_Effect * self );
  static int Effect_setVertGroup( BPy_Effect * self, PyObject * a );
  static PyObject *Effect_getSpeedVertGroup( BPy_Effect * self );
  static int Effect_setSpeedVertGroup( BPy_Effect * self, PyObject * a );
  static PyObject *Effect_getParticlesLoc( BPy_Effect * self, PyObject * a  );
+ static PyObject *Effect_getParticlesAge( BPy_Effect * self, PyObject * a  );
+ static PyObject *Effect_getParticlesID( BPy_Effect * self, PyObject * a  );
  
  static PyObject *Effect_oldsetType( void );
  static PyObject *Effect_oldsetFlag( BPy_Effect * self, PyObject * args );
  static PyObject *Effect_oldsetSta( BPy_Effect * self, PyObject * a );
  static PyObject *Effect_oldsetEnd( BPy_Effect * self, PyObject * a );
***************
*** 302,311 ****
--- 304,317 ----
  	 METH_NOARGS, "()-Return particle life time"},
  	{"setDefvec", ( PyCFunction ) Effect_oldsetDefvec, METH_VARARGS,
  	 "()- Sets particle life time "},
  	{"getParticlesLoc", ( PyCFunction ) Effect_getParticlesLoc, METH_VARARGS,
  	 "()- Sets particle life time "},
+ 	{"getParticlesAge", ( PyCFunction ) Effect_getParticlesAge, METH_VARARGS,
+ 	 "()- Sets particle life time "},
+ 	{"getParticlesID", ( PyCFunction ) Effect_getParticlesID, METH_VARARGS,
+ 	 "()- Sets particle life time "},
  	{NULL, NULL, 0, NULL}
  };
  
  /*****************************************************************************/
  /* Python BPy_Effect attributes get/set structure:                           */
***************
*** 1431,1440 ****
--- 1437,1558 ----
  	}
  
  	return list;	
  }
  
+ 
+ /*****************************************************************************/
+ /*							hack by AkhIL	     */
+ /* Method:              getParticlesAge                                      */
+ /* Python equivalent:   effect.getParticlesAge                               */
+ /* Description:         Get the current Age of each  particle                */
+ /*                      and return a list of floats                          */
+ /* Data:                float currentframe                                   */
+ /* Return:              One python list of floats                            */
+ /*****************************************************************************/
+ static PyObject *Effect_getParticlesAge( BPy_Effect * self, PyObject * args  )
+ {
+ 	Object *ob;
+ 	PartEff *paf;
+ 	Particle *pa;
+ 	int a;
+ 	PyObject *list;
+ 	float p_time, c_time, age, cfra;
+ 
+ 	if( !PyArg_ParseTuple( args, "f", &cfra) )
+ 		return EXPP_ReturnPyObjError( PyExc_TypeError,
+ 					"expected float argument" );
+ 
+ 	ob = self->object;
+ 	paf = (PartEff *)self->effect;
+ 
+ 	pa = paf->keys;
+ 	if( !pa )
+ 		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+ 				"Particles location: no keys" );
+ 	
+ 	if( ob->ipoflag & OB_OFFS_PARTICLE )
+ 		p_time= ob->sf;
+ 	else
+ 		p_time= 0.0;
+ 	
+ 	c_time= bsystem_time( ob, 0, cfra, p_time );
+ 	list = PyList_New( 0 );
+ 	if( !list )
+ 		return EXPP_ReturnPyObjError( PyExc_MemoryError, "PyList() failed" );
+ 	
+ 	for( a=0; a<paf->totpart; a++, pa += paf->totkey ) {
+ 		if( c_time > pa->time && c_time < pa->time+pa->lifetime ) {
+ 			if( pa->lifetime == 0)
+ 			    age = 0;
+ 			else
+ 			    age = (pa->time - c_time) / (pa->lifetime);
+ 
+ 			if( PyList_Append( list, Py_BuildValue("f", age)) < 0 ) {
+ 				return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ 						  "Couldn't append item to PyList" );
+ 			}
+ 		}
+ 	}
+ 
+ 	return list;	
+ }
+ 
+ 
+ /*****************************************************************************/
+ /*							hack by AkhIL	     */
+ /* Method:              getParticlesID                                       */
+ /* Python equivalent:   effect.getParticlesID                                */
+ /* Description:         Get the current ID  of each  particle                */
+ /*                      and return a list of integers                        */
+ /* Data:                float currentframe                                   */
+ /* Return:              One python list of integers			       */
+ /*****************************************************************************/
+ static PyObject *Effect_getParticlesID( BPy_Effect * self, PyObject * args  )
+ {
+ 	Object *ob;
+ 	PartEff *paf;
+ 	Particle *pa;
+ 	int a;
+ 	PyObject *list;
+ 	float p_time, c_time, cfra;
+ 
+ 	if( !PyArg_ParseTuple( args, "f", &cfra) )
+ 		return EXPP_ReturnPyObjError( PyExc_TypeError,
+ 					"expected float argument" );
+ 
+ 	ob = self->object;
+ 	paf = (PartEff *)self->effect;
+ 
+ 	pa = paf->keys;
+ 	if( !pa )
+ 		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+ 				"Particles location: no keys" );
+ 	
+ 	if( ob->ipoflag & OB_OFFS_PARTICLE )
+ 		p_time= ob->sf;
+ 	else
+ 		p_time= 0.0;
+ 	
+ 	c_time= bsystem_time( ob, 0, cfra, p_time );
+ 	list = PyList_New( 0 );
+ 	if( !list )
+ 		return EXPP_ReturnPyObjError( PyExc_MemoryError, "PyList() failed" );
+ 	
+ 	for( a=0; a<paf->totpart; a++, pa += paf->totkey ) {
+ 		if( c_time > pa->time && c_time < pa->time+pa->lifetime ) {
+ 			if( PyList_Append( list, Py_BuildValue("i", a)) < 0 ) {
+ 				return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+ 						  "Couldn't append item to PyList" );
+ 			}
+ 		}
+ 	}
+ 
+ 	return list;	
+ }
+ 
+ 
  /*****************************************************************************/
  /* Function:    Effect_dealloc                                               */
  /* Description: This is a callback function for the BPy_Effect type. It is   */
  /*              the destructor function.                                     */
  /*****************************************************************************/

LetterRip
Posts: 0
Joined: Thu Mar 25, 2004 7:03 am

Post by LetterRip »

Hi,

please use the patch tracker, that will increase the odds of them getting accepted.

LetterRip

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

Before this makes its way into the patch tracker, a couple comments:

{"getParticlesLoc", ..., "()- Sets particle life time "},
{"getParticlesAge", ... , "()- Sets particle life time "},
{"getParticlesID", ..., "()- Sets particle life time "},
Consistancy is often good. However, having the same doc string in three different methods is not.
PartEff *paf;
Particle *pa;
float cfra;
Two and three character variable names are both annoying and hard to search for. Code is read much more than it is written. Make that an easy task. If 'cfra' stands for current frame, a couple extra characters will make more obvious.

Note: points taken off for any response mentioning i,j,k or x,y,z without realizing there is some context there that makes them useful.

jms
Posts: 33
Joined: Sat Oct 19, 2002 3:04 pm

Post by jms »

stiv wrote:

Code: Select all

PartEff *paf;
Particle *pa;
float cfra;


Two and three character variable names are both annoying and hard to search for. Code is read much more than it is written. Make that an easy task. If 'cfra' stands for current frame, a couple extra characters will make more obvious.

Note: points taken off for any response mentioning i,j,k or x,y,z without realizing there is some context there that makes them useful.
Did you notice that these names are used in a bunch of official blender files :
  • convertrender.c
    effect.c
    anim.c
    buttons_object.c
    group.c
    library.c
    readfile.c
Zoo-3D.Blender, Ze French-Speaking Community SKB My french book about Blender.

jms
Posts: 33
Joined: Sat Oct 19, 2002 3:04 pm

Post by jms »

stiv wrote:Before this makes its way into the patch tracker, a couple comments:

{"getParticlesLoc", ..., "()- Sets particle life time "},
This one has been improved a few weeks ago .
Zoo-3D.Blender, Ze French-Speaking Community SKB My french book about Blender.

Post Reply