[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15680] trunk/blender/source/blender/ python/api2_2x: Python API

Ken Hughes khughes at pacific.edu
Mon Jul 21 22:42:11 CEST 2008


Revision: 15680
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15680
Author:   khughes
Date:     2008-07-21 22:42:11 +0200 (Mon, 21 Jul 2008)

Log Message:
-----------
Python API
----------

Particle patch from C?\195?\169dric Paille: bugfixes and child-particles export improvements for .getLoc(), part.getRot() part.getSize(), part.getAge() methods.

Also fix a bug with part.randemission getter (was using PART_BOIDS_2D instead of PART_TRAND), plus typos and duplications in API documentation.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/Particle.c
    trunk/blender/source/blender/python/api2_2x/doc/Particle.py

Modified: trunk/blender/source/blender/python/api2_2x/Particle.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Particle.c	2008-07-21 19:27:59 UTC (rev 15679)
+++ trunk/blender/source/blender/python/api2_2x/Particle.c	2008-07-21 20:42:11 UTC (rev 15680)
@@ -40,6 +40,7 @@
 #include "BKE_material.h"
 #include "BKE_utildefines.h"
 #include "BKE_pointcache.h"
+#include "BKE_DerivedMesh.h"
 #include "BIF_editparticle.h"
 #include "BIF_space.h"
 #include "blendef.h"
@@ -799,22 +800,27 @@
 	Py_RETURN_NONE;
 }
 
-static PyObject *Part_GetLoc( BPy_PartSys * self, PyObject * args ){
+static PyObject *Part_GetLoc( BPy_PartSys * self, PyObject * args )
+{
 	ParticleSystem *psys = 0L;
 	Object *ob = 0L;
 	PyObject *partlist,*seglist;
+	ParticleCacheKey **cache,*path;
 	PyObject* loc = 0L;
-	ParticleCacheKey **cache,*path;
 	ParticleKey state;
-	float cfra=bsystem_time(ob,(float)CFRA,0.0);
+	DerivedMesh* dm;
+	float cfra;
 	int i,j,k;
+	float vm[4][4],wm[4][4];
 	int	childexists = 0;
 	int all = 0;
 	int id = 0;
 
+	cfra = bsystem_time(ob,(float)CFRA,0.0);
+
 	if( !PyArg_ParseTuple( args, "|ii", &all,&id ) )
 		return EXPP_ReturnPyObjError( PyExc_TypeError,
-				"expected one optional integer as argument" );
+				"expected two optional integers as arguments" );
 
 	psys = self->psys;
 	ob = self->object;
@@ -822,89 +828,119 @@
 	if (!ob || !psys)
 		Py_RETURN_NONE;
 
-	if (psys->part->type == 2){
-		cache=psys->pathcache;
+	G.rendering = 1;
 
-		/* little hack to calculate hair steps in render mode */
-		psys->renderdata = (void*)(int)1;
+	/* Just to create a valid rendering context */
+	psys_render_set(ob,psys,vm,wm,0,0,0);
 
-		psys_cache_paths(ob, psys, cfra, 1);
+	dm = mesh_create_derived_render(ob,CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
+	dm->release(dm);
 
-		psys->renderdata = NULL;
+	if ( !psys_check_enabled(ob,psys) ){
+		G.rendering = 0;
+		psys_render_restore(ob,psys);
+		Particle_Recalc(self,1);
+		Py_RETURN_NONE;
+	}
 
-		partlist = PyList_New( 0 );
-		if( !partlist )
-			return EXPP_ReturnPyObjError( PyExc_MemoryError, "PyList() failed" );
+	partlist = PyList_New( 0 );
+	if( !partlist ){
+		PyErr_SetString( PyExc_MemoryError, "PyList_New() failed" );
+		goto error;
+	}
 
-		for(i = 0; i < psys->totpart; i++){
-			path=cache[i];
-			seglist = PyList_New( 0 );
-			k = path->steps+1;
-			for( j = 0; j < k ; j++){
-				loc = PyTuple_New(3);
+	if (psys->part->type == PART_HAIR){
+		cache = psys->pathcache;
 
-				PyTuple_SetItem(loc,0,PyFloat_FromDouble((double)path->co[0]));
-				PyTuple_SetItem(loc,1,PyFloat_FromDouble((double)path->co[1]));
-				PyTuple_SetItem(loc,2,PyFloat_FromDouble((double)path->co[2]));
+		if ( ((self->psys->part->draw & PART_DRAW_PARENT) && (self->psys->part->childtype != 0)) || (self->psys->part->childtype == 0) ){
 
-				if ( (PyList_Append(seglist,loc) < 0) ){
-					Py_DECREF(seglist);
-					Py_DECREF(partlist);
-					Py_XDECREF(loc);
-					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-							"Couldn't append item to PyList" );
+			for(i = 0; i < psys->totpart; i++){
+				seglist = PyList_New( 0 );
+				if (!seglist){
+					PyErr_SetString( PyExc_MemoryError,
+							"PyList_New() failed" );
+					goto error;
 				}
-				Py_DECREF(loc); /* PyList_Append increfs */
-				path++;
-			}
 
-			if ( PyList_Append(partlist,seglist) < 0 ){
-				Py_DECREF(seglist);
-				Py_DECREF(partlist);
-				return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-						"Couldn't append item to PyList" );		
+				path=cache[i];
+				k = path->steps+1;
+				for( j = 0; j < k ; j++, path++){
+					loc = Py_BuildValue("(fff)",(double)path->co[0],
+							(double)path->co[1], (double)path->co[2]);
+
+					if (!loc){
+						PyErr_SetString( PyExc_RuntimeError,
+								"Couldn't build tuple" );
+						goto error;
+					}
+
+					if ( (PyList_Append(seglist,loc) < 0) ){
+						PyErr_SetString( PyExc_RuntimeError,
+								"Couldn't append item to PyList" );
+						goto error;
+					}
+					Py_DECREF(loc); /* PyList_Append increfs */
+					loc = NULL;
+				}
+
+				if ( PyList_Append(partlist,seglist) < 0 ){
+					PyErr_SetString( PyExc_RuntimeError,
+							"Couldn't append item to PyList" );		
+					goto error;
+				}
+				Py_DECREF(seglist); /* PyList_Append increfs */
+				seglist = NULL;
 			}
-			Py_DECREF(seglist); /* PyList_Append increfs */
 		}
 
 		cache=psys->childcache;
 
 		for(i = 0; i < psys->totchild; i++){
+			seglist = PyList_New( 0 );
+			if (!seglist){
+				PyErr_SetString( PyExc_MemoryError,
+						"PyList_New() failed" );
+				goto error;
+			}
+
 			path=cache[i];
-			seglist = PyList_New( 0 );
 			k = path->steps+1;
-			for( j = 0; j < k ; j++){
-				loc = PyTuple_New(3);
+			for( j = 0; j < k ; j++, path++ ){
+				loc = Py_BuildValue("(fff)",(double)path->co[0],
+						(double)path->co[1], (double)path->co[2]);
 
-				PyTuple_SetItem(loc,0,PyFloat_FromDouble((double)path->co[0]));
-				PyTuple_SetItem(loc,1,PyFloat_FromDouble((double)path->co[1]));
-				PyTuple_SetItem(loc,2,PyFloat_FromDouble((double)path->co[2]));
+				if (!loc){
+					PyErr_SetString( PyExc_RuntimeError,
+							"Couldn't build tuple" );
+					goto error;
+				}
 
 				if ( PyList_Append(seglist,loc) < 0){
-					Py_DECREF(partlist);
-					Py_XDECREF(loc);
-					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+					PyErr_SetString( PyExc_RuntimeError,
 							"Couldn't append item to PyList" );
+					goto error;
 				}
 				Py_DECREF(loc);/* PyList_Append increfs */
-				path++;
+				loc = NULL;
 			}
 
 			if ( PyList_Append(partlist,seglist) < 0){
-				Py_DECREF(partlist);
-				Py_XDECREF(loc);
-				return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+				PyErr_SetString( PyExc_RuntimeError,
 						"Couldn't append item to PyList" );	
+				goto error;
 			}
 			Py_DECREF(seglist); /* PyList_Append increfs */
+			seglist = NULL;
 		}
-		
 	} else {
 		int init;
-		partlist = PyList_New( 0 );
-		if( !partlist )
-			return EXPP_ReturnPyObjError( PyExc_MemoryError, "PyList() failed" );
+		char *fmt = NULL;
 
+		if(id)
+			fmt = "(fffi)";
+		else
+			fmt = "(fff)";
+
 		if (psys->totchild > 0 && !(psys->part->draw & PART_DRAW_PARENT))
 			childexists = 1;
 
@@ -919,55 +955,67 @@
 				init = 1;
 
 			if (init){
-				if (!id)
-					loc = PyTuple_New(3);
-				else
-					loc = PyTuple_New(4);
-				PyTuple_SetItem(loc,0,PyFloat_FromDouble((double)state.co[0]));
-				PyTuple_SetItem(loc,1,PyFloat_FromDouble((double)state.co[1]));
-				PyTuple_SetItem(loc,2,PyFloat_FromDouble((double)state.co[2]));
-				if (id)
-					PyTuple_SetItem(loc,3,PyInt_FromLong(i));
+				loc = Py_BuildValue(fmt,(double)state.co[0],
+						(double)state.co[1], (double)state.co[2],i);
+				
+				if (!loc){
+					PyErr_SetString( PyExc_RuntimeError,
+							"Couldn't build tuple" );
+					goto error;
+				}
 
 				if ( PyList_Append(partlist,loc) < 0 ){
-					Py_DECREF(partlist);
-					Py_XDECREF(loc);
-					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-								"Couldn't append item to PyList" );
+					PyErr_SetString( PyExc_RuntimeError,
+							"Couldn't append item to PyList" );
+					goto error;
 				}
-				Py_DECREF(loc);/* PyList_Append increfs */
-			}
-			else {
-				if ( all ){
-					if ( PyList_Append(partlist,Py_None) < 0 ){
-						Py_DECREF(partlist);
-						return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-									"Couldn't append item to PyList" );
-					}
-					Py_DECREF(Py_None); /* PyList_Append increfs */
+				Py_DECREF(loc);
+				loc = NULL;
+			} else {
+				if ( all && PyList_Append(partlist,Py_None) < 0 ){
+					PyErr_SetString( PyExc_RuntimeError,
+							"Couldn't append item to PyList" );
+					goto error;
 				}
 			}
 		}
 	}
+
+	psys_render_restore(ob,psys);
+	G.rendering = 0;
+	Particle_Recalc(self,1);
 	return partlist;
+
+error:
+	Py_XDECREF(partlist);
+	Py_XDECREF(seglist);
+	Py_XDECREF(loc);
+	psys_render_restore(ob,psys);
+	G.rendering = 0;
+	Particle_Recalc(self,1);
+	return NULL;
 }
 
-static PyObject *Part_GetRot( BPy_PartSys * self, PyObject * args ){
+static PyObject *Part_GetRot( BPy_PartSys * self, PyObject * args )
+{
 	ParticleSystem *psys = 0L;
 	Object *ob = 0L;
 	PyObject *partlist = 0L;
 	PyObject* loc = 0L;
 	ParticleKey state;
+	DerivedMesh* dm;
+	float vm[4][4],wm[4][4];
 	int i;
 	int childexists = 0;
 	int all = 0;
 	int id = 0;
+    char *fmt = NULL;
 
 	float cfra=bsystem_time(ob,(float)CFRA,0.0);
 
 	if( !PyArg_ParseTuple( args, "|ii", &all, &id ) )
 		return EXPP_ReturnPyObjError( PyExc_TypeError,
-				"expected one optional integer as argument" );
+				"expected two optional integers as arguments" );
 
 	psys = self->psys;
 	ob = self->object;
@@ -975,77 +1023,145 @@
 	if (!ob || !psys)
 		Py_RETURN_NONE;
 
-	if (psys->part->type != 2){
+	G.rendering = 1;
+
+	/* Just to create a valid rendering context */
+	psys_render_set(ob,psys,vm,wm,0,0,0);
+
+	dm = mesh_create_derived_render(ob,CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
+	dm->release(dm);
+
+	if ( !psys_check_enabled(ob,psys) ){
+		G.rendering = 0;
+		psys_render_restore(ob,psys);
+		Particle_Recalc(self,1);
+		Py_RETURN_NONE;
+	}
+
+	if (psys->part->type != PART_HAIR){
 		partlist = PyList_New( 0 );
 
+		if( !partlist ){
+			PyErr_SetString( PyExc_MemoryError, "PyList_New() failed" );
+			goto error;
+		}
+
 		if (psys->totchild > 0 && !(psys->part->draw & PART_DRAW_PARENT))
 			childexists = 1;
 
+		if(id)
+			fmt = "(ffffi)";
+		else
+			fmt = "(ffff)";
+
 		for (i = 0; i < psys->totpart + psys->totchild; i++){
 			if (childexists && (i < psys->totpart))
 				continue;
 
 			state.time = cfra;
 			if(psys_get_particle_state(ob,psys,i,&state,0)==0){
-				if ( all ){
-					PyList_Append(partlist,Py_None);
-					Py_DECREF(Py_None); /* PyList_Append increfs */
-					continue;
-				} else {
-					continue;
+				if ( all && PyList_Append(partlist,Py_None) < 0){
+					PyErr_SetString( PyExc_RuntimeError,
+						"Couldn't append item to PyList" );
+					goto error;
 				}
+			} else {
+				loc = Py_BuildValue(fmt,(double)state.rot[0], (double)state.rot[1],
+						(double)state.rot[2], (double)state.rot[3], i);
+
+				if (!loc){
+					PyErr_SetString( PyExc_RuntimeError,
+							"Couldn't build tuple" );
+					goto error;
+				}
+				if (PyList_Append(partlist,loc) < 0){
+					PyErr_SetString ( PyExc_RuntimeError,
+							"Couldn't append item to PyList" );
+					goto error;
+				}
+				Py_DECREF(loc); /* PyList_Append increfs */
+				loc = NULL;
 			}
-			if (!id)
-				loc = PyTuple_New(4);
-			else
-				loc = PyTuple_New(5);

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list