===Python API===

Bugfix #3660:  NMesh.getVertexInfluences() was broken following the changes
to the armature system.  Tron Thomas (kudos) came up with a fix that seems
to perform identically to the old method.  I'm also adding it to the Mesh
module for compatibility.
This commit is contained in:
Ken Hughes
2006-01-19 15:48:56 +00:00
parent 14e1000e38
commit 0fcfd5bd40
3 changed files with 92 additions and 57 deletions

View File

@@ -82,8 +82,6 @@
#include "constant.h"
#include "gen_utils.h"
#define MESH_TOOLS /* add access to mesh tools */
/* EXPP Mesh defines */
#define MESH_SMOOTHRESH 30
@@ -5509,7 +5507,53 @@ static PyObject *Mesh_getVertGroupNames( BPy_Mesh * self )
return list;
}
#ifdef MESH_TOOLS
static PyObject *Mesh_getVertexInfluences( BPy_Mesh * self, PyObject * args )
{
int index;
PyObject *influence_list = NULL;
Object *object = self->object;
Mesh *me = self->mesh;
/* Get a reference to the mesh object wrapped in here. */
if( !object )
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"This mesh must be linked to an object" );
/* Parse the parameters: only on integer (vertex index) */
if( !PyArg_ParseTuple( args, "i", &index ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected int argument (index of the vertex)" );
/* check for valid index */
if( index < 0 || index >= me->totvert )
return EXPP_ReturnPyObjError( PyExc_IndexError,
"vertex index out of range" );
influence_list = PyList_New( 0 );
/* Proceed only if we have vertex deformation information */
if( me->dvert ) {
int i;
MDeformWeight *sweight = NULL;
/* Number of bones influencing the vertex */
int totinfluences = me->dvert[index].totweight;
/* Get the reference of the first weight structure */
sweight = me->dvert[index].dw;
/* Build the list only with weights and names of the influent bones */
for( i = 0; i < totinfluences; i++, sweight++ ) {
bDeformGroup *defgroup = BLI_findlink( &object->defbase,
sweight->def_nr );
if( defgroup )
PyList_Append( influence_list, Py_BuildValue( "[sf]",
defgroup->name, sweight->weight ) );
}
}
return influence_list;
}
static PyObject *Mesh_Tools( BPy_Mesh * self, int type, void **args )
{
@@ -5711,8 +5755,6 @@ static PyObject *Mesh_fill( BPy_Mesh * self )
return Mesh_Tools( self, MESH_TOOL_FILL, NULL );
}
#endif
static struct PyMethodDef BPy_Mesh_methods[] = {
{"calcNormals", (PyCFunction)Mesh_calcNormals, METH_NOARGS,
"all recalculate vertex normals"},
@@ -5740,10 +5782,10 @@ static struct PyMethodDef BPy_Mesh_methods[] = {
"Rename an existing vertex group"},
{"getVertGroupNames", (PyCFunction)Mesh_getVertGroupNames, METH_NOARGS,
"Get names of vertex groups"},
{"getVertexInfluences", (PyCFunction)Mesh_getVertexInfluences, METH_VARARGS,
"Get list of the influences of bones for a given mesh vertex"},
#ifdef MESH_TOOLS
/* Mesh tools */
{"smooth", (PyCFunction)Mesh_smooth, METH_NOARGS,
"Flattens angle of selected faces (experimental)"},
{"flipNormals", (PyCFunction)Mesh_flipNormals, METH_NOARGS,
@@ -5762,7 +5804,6 @@ static struct PyMethodDef BPy_Mesh_methods[] = {
"Removes duplicates from selected vertices (experimental)"},
{"recalcNormals", (PyCFunction)Mesh_recalcNormals, METH_VARARGS,
"Recalculates inside or outside normals (experimental)"},
#endif
{NULL, NULL, 0, NULL}
};

View File

@@ -1466,71 +1466,55 @@ static PyObject *NMesh_update( PyObject *self, PyObject *a, PyObject *kwd )
* influences that this vertex receives.
* @author Jordi Rovira i Bonet
*/
static PyObject *NMesh_getVertexInfluences( PyObject * self, PyObject * args )
{
int index;
PyObject *influence_list = NULL;
/* Get a reference to the mesh object wrapped in here. */
Object *object = ( ( BPy_NMesh * ) self )->object;
Mesh *me = ( ( BPy_NMesh * ) self )->mesh;
/* Get a reference to the mesh object wrapped in here. */
if( !me )
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"unlinked nmesh: call its .update() method first" );
"unlinked nmesh: call its .update() method first" );
if( !object )
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"This mesh must be linked to an object" );
/* Parse the parameters: only on integer (vertex index) */
if( !PyArg_ParseTuple( args, "i", &index ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected int argument (index of the vertex)" );
"expected int argument (index of the vertex)" );
/* Proceed only if we have vertex deformation information and index is valid */
/* check for valid index */
if( index < 0 || index >= me->totvert )
return EXPP_ReturnPyObjError( PyExc_IndexError,
"vertex index out of range" );
influence_list = PyList_New( 0 );
/* Proceed only if we have vertex deformation information */
if( me->dvert ) {
if( ( index >= 0 ) && ( index < me->totvert ) ) {
int i;
MDeformWeight *sweight = NULL;
int i;
MDeformWeight *sweight = NULL;
/* Number of bones influencing the vertex */
int totinfluences = me->dvert[index].totweight;
/* Number of bones influencing the vertex */
int totinfluences = me->dvert[index].totweight;
/* Get the reference of the first weight structure */
sweight = me->dvert[index].dw;
/* Build the list only with weights and names of the influent bones */
/*influence_list = PyList_New(totinfluences); */
influence_list = PyList_New( 0 );
/* Get the reference of the first weight structure */
sweight = me->dvert[index].dw;
for( i = 0; i < totinfluences; i++ ) {
/*Add the weight and the name of the bone, which is used to identify it */
/* Disabled this code, it couldn't be correct!
* sweight->data was being set to a posechannel not a bone
* for one thing, and it is not always set for another.
* The only thing safe here is to return the defgroup number. -zr
*/
// if( sweight->data )
/* valid bone: return its name */
/* PyList_SetItem(influence_list, i,
Py_BuildValue("[sf]", sweight->data->name, sweight->weight));
else // NULL bone: return Py_None instead
PyList_SetItem(influence_list, i,
Py_BuildValue("[Of]", Py_None, sweight->weight)); */
// PyList_Append( influence_list,
// Py_BuildValue( "[sf]",
// sweight->
// data->
// name,
// sweight->
// weight ) );
/* Next weight */
sweight++;
}
} else //influence_list = PyList_New(0);
return EXPP_ReturnPyObjError( PyExc_IndexError,
"vertex index out of range" );
} else
influence_list = PyList_New( 0 );
/* Build the list only with weights and names of the influent bones */
for( i = 0; i < totinfluences; i++, sweight++ ) {
bDeformGroup *defgroup = BLI_findlink( &object->defbase,
sweight->def_nr );
if( defgroup )
PyList_Append( influence_list, Py_BuildValue( "[sf]",
defgroup->name, sweight->weight ) );
}
}
return influence_list;
}

View File

@@ -846,6 +846,16 @@ class Mesh:
associated with the mesh's object
"""
def getVertexInfluences(index):
"""
Get the bone influences for a specific vertex.
@type index: int
@param index: The index of a vertex.
@rtype: list of lists
@return: List of pairs [name, weight], where name is the bone name (string)
and weight is a float value.
"""
def smooth():
"""
Flattens angle of selected faces. Experimental mesh tool.