[Bf-python] nmesh,Transform update

Campbell Barton cbarton at metavr.com
Sat Mar 19 13:25:50 CET 2005


Hi, this should fix the normal transformation.

___________

static PyObject *NMesh_transform( PyObject * self, PyObject * args )
{
    BPy_NMesh *nmesh = ( BPy_NMesh * ) self;
    BPy_NMVert *mv;
    PyObject *ob1 = NULL;
    MatrixObject *mat;
    MatrixObject *imat;
    float vx, vy, vz;
    int i;
    int loc_only = 0;
   
    if( !PyArg_ParseTuple( args, "O!|i", &matrix_Type, &ob1, &loc_only ) )
        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 locations 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];
    }
    if ( ! loc_only ) {
        /* We can safely transpose and inverse the matrix for use on 
normals */
        Mat4Transp( (float ( * )[4])*mat->matrix );
        Mat4Invert( (float ( * )[4])*imat->matrix, (float ( * 
)[4])*mat->matrix );
       
       
        /* Loop through all the verts and transform normals by the 
supplied matrix */
        for( i = 0; i < PySequence_Length(nmesh->verts); i++ ) {
            mv = ( BPy_NMVert * ) PySequence_GetItem( nmesh->verts, i );
            vx = mv->no[0];
            vy = mv->no[1];
            vz = mv->no[2];
            mv->no[0] = vx*imat->matrix[0][0] + vy*imat->matrix[1][0] + 
vz*imat->matrix[2][0];
            mv->no[1] = vx*imat->matrix[0][1] + vy*imat->matrix[1][1] + 
vz*imat->matrix[2][1];
            mv->no[2] = vx*imat->matrix[0][2] + vy*imat->matrix[1][2] + 
vz*imat->matrix[2][2];
            Normalise(mv->no);
        }
    }
   
    Py_DECREF( mv );
    Py_INCREF( Py_None );
    return Py_None;
}



More information about the Bf-python mailing list