Discussion:
[Maya-Python] Maya API: rotate around certain point without affecting rotate pivot
igo rov
2018-08-26 07:31:18 UTC
Permalink
I've been searching for hours, but didn't find anything: I want to rotate
my object around some point on the surface (vertex). But instead of setting
the rotate pivot to this point, I want to calculate the transformation of
my object, as if it would have a parent-child relationship, so basically if
the object would be parented to its own vertex. But I don`t want to work
with empty transforms, just calculating the transfom (I guess with matrix).
Can anyone give me a hint where to start?
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b8b466d0-18c6-4d5d-a945-a0daab167275%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pedro Bellini
2018-08-27 03:22:13 UTC
Permalink
Hi, see if this works

Basically you need to compute the transformation matrix that represents the
rotation from a specific point ("vertex" in your case). That can be
generally done by matrix composition.... m1*m2-1*transform*m2. Where m1 is
the matrix of the object you want to move, m2-1 is the inverse matrix of
the object you want to use as reference (parent space), transform is your
rotation matrix, then you move it back to world by multiplying back m2.

A quick example in maya can look like this:

from maya.api import OpenMaya
from maya import cmds

cube = cmds.polyCube()[0]
vtx = cube+'.vtx[2]'
vtx_pos = cmds.xform(vtx, q=1,ws=1,t=1)
# vertex does not have orientation, so will keep world
# you can use vertex normals or custom...
# ill just extract the position
vtx_world_mmat = OpenMaya.MMatrix()
for i in xrange(3):
vtx_world_mmat.setElement(3,i, vtx_pos[i])

grp = cmds.createNode('transform')
cmds.xform(grp, t=(5,0,0))
obj = cmds.polyPrism()[0]
cmds.parent(obj,grp,r=1)
obj_world_mat = cmds.xform(obj, q=1,ws=1,m=1)

# compose the rotation matrix however you want, ill just make from euler
rotation
rot_eu = OpenMaya.MEulerRotation()
rot_eu.y=3.1415*.5 # 90 deg (ish)
rot_mmat = rot_eu.asMatrix()

obj_world_mmat = OpenMaya.MMatrix(obj_world_mat)

# transform
*resulting_mmat = obj_world_mmat * vtx_world_mmat.inverse() * rot_mmat *
vtx_world_mmat*
# rotate
cmds.xform(obj, ws=1, m=list(resulting_mmat))

If I understood you correctly this should be it.

Cheers.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/12ffcb0c-37f1-4f13-9e27-153ccbe4ee1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
igo rov
2018-08-27 19:25:53 UTC
Permalink
Hi thanks,
your code has some helpful information for me...
this is what i got so far, though the locator is no vertex... and you have
to execute first part, then rotate reference locator and then execute
second part. Since api 1.0 has no" MMatrix.setElement" I find the very
useful MTransformationMatrix. But as always when using API I find myself
going long and indirect ways to reach my goal...

import maya.OpenMaya as om

# execute this to initialize the selections and get the positions

selCube = om.MSelectionList()
selCube.add("pCube1")
mDagCube = om.MDagPath()
selCube.getDagPath(0, mDagCube)
fnTransCube = om.MFnTransform(mDagCube)
posCube = fnTransCube.translation(4)

selLoc = om.MSelectionList()
selLoc.add("locator1")
mDagLoc = om.MDagPath()
selLoc.getDagPath(0, mDagLoc)
fnTransLoc = om.MFnTransform(mDagLoc)
posLoc = fnTransLoc.translation(4)

# execute this to get the hypothetical child Matrix
vec = posCube - posLoc

mChildTransMatr = om.MTransformationMatrix()
mChildTransMatr.setTranslation(vec, 4)
childMatr = mChildTransMatr.asMatrix()

# now you can move and rotate your parent object and again get his matrix
parentMatr = mDagLoc.inclusiveMatrix()


# this gets the child's world matrix
childWorldMatr = childMatr * parentMatr

# last step is to apply the matrix to the hypothetical child
chMatr = om.MTransformationMatrix(childWorldMatr)
quatRotation = om.MQuaternion()
rot = chMatr.rotation()
trans = chMatr.translation(4)
fnTransCube.setTranslation(trans, 4)
fnTransCube.setRotation(rot,4)
Post by Pedro Bellini
Hi, see if this works
Basically you need to compute the transformation matrix that represents
the rotation from a specific point ("vertex" in your case). That can be
generally done by matrix composition.... m1*m2-1*transform*m2. Where m1 is
the matrix of the object you want to move, m2-1 is the inverse matrix of
the object you want to use as reference (parent space), transform is your
rotation matrix, then you move it back to world by multiplying back m2.
from maya.api import OpenMaya
from maya import cmds
cube = cmds.polyCube()[0]
vtx = cube+'.vtx[2]'
vtx_pos = cmds.xform(vtx, q=1,ws=1,t=1)
# vertex does not have orientation, so will keep world
# you can use vertex normals or custom...
# ill just extract the position
vtx_world_mmat = OpenMaya.MMatrix()
vtx_world_mmat.setElement(3,i, vtx_pos[i])
grp = cmds.createNode('transform')
cmds.xform(grp, t=(5,0,0))
obj = cmds.polyPrism()[0]
cmds.parent(obj,grp,r=1)
obj_world_mat = cmds.xform(obj, q=1,ws=1,m=1)
# compose the rotation matrix however you want, ill just make from euler
rotation
rot_eu = OpenMaya.MEulerRotation()
rot_eu.y=3.1415*.5 # 90 deg (ish)
rot_mmat = rot_eu.asMatrix()
obj_world_mmat = OpenMaya.MMatrix(obj_world_mat)
# transform
*resulting_mmat = obj_world_mmat * vtx_world_mmat.inverse() * rot_mmat *
vtx_world_mmat*
# rotate
cmds.xform(obj, ws=1, m=list(resulting_mmat))
If I understood you correctly this should be it.
Cheers.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/07851383-12c6-4119-b941-ee486a1e94c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...