Matrix inverse of several transformations ?

63 views
Skip to first unread message

justin hidair

unread,
Apr 4, 2018, 11:39:30 AM4/4/18
to Python Programming for Autodesk Maya

I'm relatively new in the matrix game , I'm trying to apply some rotation , position and scale in one matrix on a object,
and it did work , I just had to multiply the matrices together to obtain one transformation matrix, it does its job but the problem is that I need at
some point to revert the changes, like all of them , and when I'm trying to inverse the "master matrix" I open a black hole and the object is no longer how it was before

:

```python
# matrix to position to center
pos_mat = om.MMatrix( [
        (1,0,0,0),
        (0,1,0,0),
        (0,0,1,0),
        (-o.x,-o.y,-o.z,1) ] )   

# matrix to flatten on y
scale_mat = om.MMatrix( [
        (1,0,0,0),
        (0,0,0,0),
        (0,0,1,0),
        (0,0,0,1)] )   

fnmat = ( pos_mat * rot_mat ) * scale_mat
# fnmat = pos_mat

for i in xrange( fnm.numVertices ):
    fnm.setPoint( i , fnm.getPoint( i) * fnmat )
        # fnm.getPoint( i) * fnmat.inverse() to reverse does not work / is odd looking
```

so the question is how to reverse several transformations with one 'reverse matrix' if it's possible
Message has been deleted

justin hidair

unread,
Apr 4, 2018, 5:06:33 PM4/4/18
to Python Programming for Autodesk Maya

nvm fixed it, this is confusing tho but when you are done with your transformations and you find yourself at the origin you just need to add the previous position to your transformed points , the code speaks for itself,
basically if the plane stay the same, it means it works , and it worked ...


import maya.api.OpenMaya as om
import math


sel
= om.MSelectionList()
sel
.add('pPlaneShape1')

mesh
= sel.getDependNode(0)

fnm
= om.MFnMesh( mesh )

normal
= fnm.getPolygonNormal( 0 )

o
= om.MItMeshPolygon(mesh).center()


def get_orient_mat( a ):
    b
= om.MVector.kYaxisVector
    i
= om.MMatrix.kIdentity
    v
= a ^ b
    ang
= a.angle(b)
   
if ang < 1e-6:
        res
= i
       
return res
   
if math.fabs(ang-math.pi) < 1e-6:
        res
= om.MMatrix( [
           
(math.cos(math.pi) , math.sin(math.pi), 0, 0),
           
(-math.sin(math.pi), math.cos(math.pi), 0, 0),
           
(0, 0, 1, 0),
           
(0,0,0,1) ] )    
       
return res
    c
= math.cos( ang)
    skew
= om.MMatrix( [
           
(0, v.z, -v.y, 0),
           
(-v.z, 0, v.x, 0),
           
(v.y, -v.x, 0, 0),
           
(0,0,0,1) ] )    
    res
= i + skew +  (skew*skew)*( 1 / (1+c) )
   
return res

# matrix to orient in 2D
rmat
= get_orient_mat( normal)


# matrix to position to center

tmat
= om.MMatrix( [
       
(1,0,0,0),

       
(0,1,0,0),
       
(0,0,1,0),
       
(-o.x,-o.y,-o.z,1) ] )    

# matrix to flatten on y

smat
= om.MMatrix( [
       
(1,0,0,0),
       
(0,0,0,0),
       
(0,0,1,0),
       
(0,0,0,1)] )

mat
= tmat * rmat * smat    

for i in xrange( fnm.numVertices ):

   
    cur
= fnm.getPoint( i)
   
# cur*=tmat
   
# cur*=rmat
   
# cur*=smat
    cur
*=mat
    fnm
.setPoint( i ,  cur )

#reverse

for i in xrange( fnm.numVertices ):

   
    cur
= fnm.getPoint( i)
    duh
= om.MPoint( om.MVector( cur * rmat.inverse() ) + om.MVector(  o)  )
    fnm
.setPoint( i ,  duh )


Michael Boon

unread,
Apr 5, 2018, 1:31:29 AM4/5/18
to Python Programming for Autodesk Maya
Glad you got it.

One thing I noticed from your first post there, is "matrix to flatten on Y." You can't actually inverse a matrix that flattens things to a plane. Mathematically, it requires dividing by 0 as your determinate will be 0. Physically think about the fact that an infinite number of shapes will result in the same plane once they've been flattened by that matrix, and you have no way of knowing which one of them you want to get back.

justin hidair

unread,
Apr 5, 2018, 1:36:53 AM4/5/18
to python_in...@googlegroups.com

Yes I noticed that , flattening is not reversible , fortunately in my case it doesn’t need to be so I just reverse the translation and the rotation. If I were to do it tho, I would just register the points prior flattening

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/DQR6qJLVCKg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/f61fe086-4a32-4f80-8c5d-a7bbd1d2c82f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Reply all
Reply to author
Forward
0 new messages