[Bf-python] Transforming a mesh C-, amazing speed increase of exporters.

Yann Vernier yann at algonet.se
Sun Feb 6 11:30:40 CET 2005


Looks good to me, assuming the assumption that verts is a sequence of
NMVerts holds. If it's known to be a list, PyList_GET_{SIZE,ITEM} can
give you a quicker way to traverse it; note that the latter returns
borrowed references so you wouldn't do the decref anymore. (Assuming
that it's a list doesn't seem that much worse than assuming it
uniformly contains NMVerts to me.)

On Sun, Feb 06, 2005 at 03:12:05AM +0800, Campbell J Barton wrote:
> +static PyObject *NMesh_transform( PyObject * self, PyObject * args )
> +{
> +	BPy_NMesh *nmesh = ( BPy_NMesh * ) self;
> +	BPy_NMVert *mv;
> +	PyObject *ob1 = NULL;
> +	MatrixObject *mat;
> +	float vx, vy, vz;
> +	int i;
> +	
> +	if( !PyArg_ParseTuple( args, "O!", &matrix_Type, &ob1 ) )
> +		return ( EXPP_ReturnPyObjError( PyExc_TypeError,
> +						"expected matrix\n" ) );
> +	
> +	mat = ( MatrixObject * ) ob1;
> +	
> +	if( mat->colSize != 4 || mat->rowSize != 4 )
> +		return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
> +						"matrix must be a 4x4 transformation matrix as returned by object.getMatrix()\n" ) );
> +	
> +	/* Loop through all the verts and transform them by the supplied matrix */
> +	for( i = 0; i < PySequence_Length(nmesh->verts); i++ ) {
> +		mv = ( BPy_NMVert * ) PySequence_GetItem( nmesh->verts, i );
> +		vx = mv->co[0];
> +		vy = mv->co[1];
> +		vz = mv->co[2];
> +		mv->co[0] = vx*mat->matrix[0][0] + vy*mat->matrix[1][0] + vz*mat->matrix[2][0] + mat->matrix[3][0];
> +		mv->co[1] = vx*mat->matrix[0][1] + vy*mat->matrix[1][1] + vz*mat->matrix[2][1] + mat->matrix[3][1];
> +		mv->co[2] = vx*mat->matrix[0][2] + vy*mat->matrix[1][2] + vz*mat->matrix[2][2] + mat->matrix[3][2];
> +	}
> +	
> +	Py_DECREF( mv );

This should be in the loop. You're doing a GetItem for each vertex.

> +	Py_INCREF( Py_None );
> +	return Py_None;
> +}
> +
>  static PyObject *NMesh_getMode( BPy_NMesh * self )
>  {
>  	PyObject *attr = PyInt_FromLong( self->mode );
> @@ -1513,6 +1551,7 @@
>  	MethodDef( setMode ),
>  	MethodDef( setMaxSmoothAngle ),
>  	MethodDef( setSubDivLevels ),
> +	MethodDef( transform ),
>  
>  /* METH_NOARGS: function(PyObject *self) */
>  #undef MethodDef



More information about the Bf-python mailing list