Maya Python: Apply Transformation Matrix
Question: I have been looking for thi answer but i don't seem to figure it out anywhere,
so i hope i could get my answer here...

I'm in Maya Python API and i want to apply a transformation Matrix to a mesh.

This is how i made the mesh:

    
    
        mesh = om.MFnMesh()
        ShapeMesh = cmds.group(em=True)
        parentOwner = get_mobject( ShapeMesh )
        meshMObj = mesh.create(NumVerts, len(FaceCount), VertArray, FaceCount, FaceArray ,parentOwner)
        cmds.sets( ShapeMesh, e=True,forceElement='initialShadingGroup')
        defaultUVSetName = ''
        defaultUVSetName = mesh.currentUVSetName(-1)
        mesh.setUVs ( UArray, VArray, defaultUVSetName )
        mesh.assignUVs ( FaceCount,  FaceArray, defaultUVSetName )
    

This is how i create the TFM:

    
    
    m = struct.unpack("<16f",f.read(64))
    mm = om.MMatrix()
    om.MScriptUtil.createMatrixFromList(m,mm)
    mt = om.MTransformationMatrix(mm)
    

Basically i read 16 floats and convert them into a Transformation Matrix,
however i don't know how to apply the mt matrix to my mesh...

I managed to get the Position,Rotation and Scale from this though, maybe it
helps, this way:

    
    
        translate = mt.translation(om.MSpace.kWorld)
        rotate = mt.rotation().asEulerRotation()
        scaleUtil = om.MScriptUtil()
        scaleUtil.createFromList([0,0,0],3)
        scaleVec = scaleUtil.asDoublePtr()
        mt.getScale(scaleVec,om.MSpace.kWorld)
        scale = [om.MScriptUtil.getDoubleArrayItem(scaleVec,i) for i in range(0,3)]
    

Now my last step comes in applying this Matrix to the mesh, but i can't find a
good way to do it, does someone know how to do this on maya?

Thanks in advance: Seyren.


Answer: Not sure what you mean by applying the matrix to your mesh, but if you want to
update the position of each point by transforming them with that matrix, then
here you go for a given MFnMesh `mesh` and a given MMatrix `matrix`:

    
    
    import banana.maya
    banana.maya.patch()
    from maya import OpenMaya
    
    mesh = OpenMaya.MFnMesh.bnn_get('pCubeShape1')
    matrix = OpenMaya.MMatrix()
    
    points = OpenMaya.MPointArray()
    mesh.getPoints(points)
    for i in range(points.length()):
        points.set(points[i] * matrix, i)
    
    mesh.setPoints(points)
    

If you don't want to directly update the points of the mesh, then you need to
apply the matrix to the transformation node by retrieving its parent transform
and using the `MFnTransform::set()` method.

Note that I've used in my code snippet a set of extensions that I've wrote and
that might be helpful if you're using the Maya Python API. The code is
available on [GitHub](https://github.com/christophercrouzet/banana.maya) and
it also comes with a [documentation](http://bananamaya.readthedocs.org/) to
give you an idea.

