copy transform

793 views
Skip to first unread message

Sebastian Schoellhammer

unread,
Apr 14, 2008, 5:56:15 AM4/14/08
to python_in...@googlegroups.com
Hello there,

I want to copy the transformation from one object onto another while keeping its shape in the exact same position.

I couldn't figure out a better way than first applying the transform matrix and then transforming all the meshes vertexes back by the inverse matrix.

Heres my script:

import maya.cmds as mc
from cgkit.cgtypes import mat4


def copyTransform(source, toCopy):
    #get the matrix
    m = mc.xform(source, worldSpace = True, matrix=True, query=True)
    mInv = mat4(m).inverse()
    mc.select(toCopy)
    #reset transform
    mc.makeIdentity(apply=True, t=1, r=1, s=1, n=0)
    mel.eval("performResetTransformations(0)")
    #apply source transform
    mc.xform(toCopy, worldSpace = True, matrix=m)

    #transform all verts inversely
    numVerts = mc.polyEvaluate(toCopy,vertex=True)
   
    for i in range(0,numVerts):
        n = toCopy+".vtx["+str(i)+"]"
        mc.xform(n, matrix=mInv.toList(1))
    mel.eval("DeleteHistory")

sel = mc.ls(sl=True)
last = sel.pop()
for s in sel:
    copyTransform(last, s)

It's seems a bit silly to me and im sure there must be a better way... Anyone?

Thanks so much,

seb

--
--
Sebastian Schoellhammer
3DArtist・Technical Director
www.subdivme.com/sebscorner

Sylvain Berger

unread,
Apr 14, 2008, 12:24:55 PM4/14/08
to python_in...@googlegroups.com
using constraints then deleting the constraints might be a quick and dirty way to do what you want

2008/4/14 Sebastian Schoellhammer <sschoel...@gmail.com>:



--
They say, "Evil prevails when good men fail to act." What they ought to say is, "Evil prevails."
Nicolas Cage as Yuri Orlov in Lord of War.

Olivier Renouard

unread,
Apr 16, 2008, 5:44:12 PM4/16/08
to python_in...@googlegroups.com
Hi,

I guess you don't want to use pivots. You could do this which amounts to the same, but could be faster by using
mc.makeIdentity (apply=True, translate=True, rotate=True, scale=True) :

Just try this, the idea is, apply inverse source transform, freeze transform, apply source transform

def copyTransform(source, toCopy):
    #get the matrix
    m = mc.xform(source, worldSpace = True, matrix=True, query=True)
    mInv = mat4(m).inverse()
    mi = []
    for i in xrange(4) :
        for j in xrange(4) :
            mi.append(mInv[j][i])
    # apply source inverse transform
    mc.xform(toCopy, worldSpace = True, relative = True, matrix=mi)
    #reset transform
    mc.makeIdentity(toCopy, apply=True, t=1, r=1, s=1, n=0)
    #apply source transform
    mc.xform(toCopy, worldSpace = True, relative = True, matrix=m)

I see you use cgkit for the matrix math. I'd like to know who on list would be for the use of cgkit, or Numpy or Maya's api for doing matrix , vector operations and such related things ? What's your thoughts ?

Olivier

Chadrik

unread,
Apr 17, 2008, 1:25:59 AM4/17/08
to python_inside_maya
i have not used cgkit or numpy, but a cursory glance shows that they
both need to be compiled per platform. the fact that MVector and
MMatrix offer a pre-compiled solution makes them quite attractive just
from the distribution standpoint. also, the consistency of the
interface with the rest of maya is a plus. furthermore numpy seems a
bit of overkill for maya.

however, like i said, i don't have experience with them, so i'd like
to hear what others have to say, particularly cgkit versus maya api.

-chad




On Apr 16, 2:44 pm, Olivier Renouard <o.renou...@attitude-studio.com>
wrote:
> >www.subdivme.com/sebscorner<http://www.subdivme.com/sebscorner>

Ofer Koren

unread,
Apr 17, 2008, 2:01:51 AM4/17/08
to python_in...@googlegroups.com
just happened to use numpy recently but it was for a more complicated matrix calculation, using matrices bigger than the 4x4 that maya allows (it does allow only 4x4 right?)
Otherwise I use maya's or pymel's.

2008/4/16 Chadrik <cha...@gmail.com>:



--


- Ofer
www.mrbroken.com
Reply all
Reply to author
Forward
0 new messages