pymel globalscale

49 views
Skip to first unread message

kevco...@gmail.com

unread,
Aug 19, 2014, 12:23:24 AM8/19/14
to python_in...@googlegroups.com
Hey all,

I'm having some trouble accessing the global scale of a transform matrix. Not quite sure why this isn't working...

in the pymel docs, for a transform node, there's two methods; getMatrix, and getTransformation. They both say they return a transformation Matrix - ok great.

When I use getMatrix(worldSpace=1) - it returns the transformation matrix I wanna work with.

But using the getScale method on this gives me errors...

When I use getTransformation().getScale() - I don't get errors, but I also don't get the matrix I wanna work with. what gives? or better yet, how do I access globalScale?

####
import pymel.core as pm

objectMat=pm.selected()[0].getMatrix(worldSpace=1)
print objectMat.getScale("world")

objectTfm=pm.selected()[0].getTransformation()
print objectTfm.getScale("world")
####


Thanks guys!

Geordie Martinez

unread,
Aug 19, 2014, 4:55:23 PM8/19/14
to python_inside_maya
I'm a little confused what you're looking for and what you are calling a matrix.
if you want to get the translation of an object you can get that from a globalMatrix as well as getTranslation.

but if you want to get the scale:
the docs say this for getMatrix:

  Note that, when querying the scale, that this calculation is cumulative and is only valid if there are all uniform scales and no rotation. In a
  hierarchy with non-uniform scale and rotation, this value may not correspond entirely with the perceived global scale.

One way you could do it is to go up the hierarchy and multiply the scale by each parent's scale. 
I'm sure there is a linear algebraic way of doing this, eterminant multiplied by the inverseMatrix or some such. I dunno.
where is Chad Vernon when you need him? 

but this kinda works even if there are rotations in the parents and children. it won't "look" the same because of possible shearing but it will get you what I think it is you're looking for. 
import pymel.core as pm
def returnAncestors(node):
    """ returns a list of ancstors"""  
    hierarchy = []
    while not node.getParent() == None:
        node = node.getParent()
        hierarchy.append(node)
       
    return hierarchy
          
def retGlobalScale(node):
    """ returns a cumulative scale from transform hierarchy"""
    curScale = node.scale.get()
    for ancestor in returnAncestors(node):
        scale = ancestor.getScale()
        curScale = curScale * scale
    
    return curScale

#EXAMPLE USE OF A TRANSFORM IN A HIERARCHY OF DIFFERENT SCALES
scl = retGlobalScale(pm.selected()[0])

kevco...@gmail.com

unread,
Aug 19, 2014, 10:59:26 PM8/19/14
to python_in...@googlegroups.com, kevco...@gmail.com
Hi Geordie, Thanks for the help. I think I'm confused now. What is the difference in returns between using getMatrix() and getTransformation()? They both say they return a transformation matrix, is it correct?

Anyhow, I basically just want to return the scale of an object/locator as if you unparented it, and got that value in scale. Which I believe your functions does for me. Thank you. I'm a little surprised maya doesn't give you access to 'global scale', but does for all other components of a transform.

I think this will work great for my need though. Cheers!

Marcus Ottosson

unread,
Aug 20, 2014, 12:45:27 AM8/20/14
to python_in...@googlegroups.com, kevco...@gmail.com

There is also the decomposeMatrix node, and the worldMatrix slot of transforms.

my_transform, my_shape = cmds.polyCube()
decompose = cmds.createNode('decomposeMatrix')
cmds.connectAttr(my_transform + ".worldMatrix", decompose + ".inputMatrix")
cmds.getAttr(decompose + ".outputScale")

This will give you the worldspace equivalent of all transform channels in real-time.

cmds.getAttr(decompose + ".outputTranslate")
cmds.getAttr(decompose + ".outputRotate")
cmds.getAttr(decompose + ".outputShear")
...



--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9788952a-59f0-4ed7-9311-408ebc12cf4f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Geordie Martinez

unread,
Aug 20, 2014, 2:17:25 AM8/20/14
to python_inside_maya
Ah yes. I completely forgot about that utility node. it works perfectly for this.



Paul Molodowitch

unread,
Aug 20, 2014, 2:37:15 AM8/20/14
to python_inside_maya

Anyhow, I basically just want to return the scale of an object/locator as if you unparented it, and got that value in scale.  Which I believe your functions does for me. Thank you.  I'm a little surprised maya doesn't give you access to 'global scale', but does for all other components of a transform.

The problem is that, in the general case, there's no such thing as a simple "global scale". As soon as you start rotating and applying non-uniform scales, things get messy.


To take an easy example, form a hierarchy where you take a cube, scale in x by 3, rotate 30 degrees, scale in x by 3 again, and rotate 30 degrees again.  You now have a transformation that cannot be represented by a single rotation and a single scale. In order to decompose that, you would either need to introduce a shear factor, or allow repeated scales or rotations.

- Paul


Reply all
Reply to author
Forward
0 new messages