0

Hi, how can I get object's actual rotation after freeze?

For instance :

# create a cube
CudeTransformNode = cmds.polyCube()[ 0 ]


# rotate X 20 degree.
cmds.setAttr( f"{CudeTransformNode}.rotateX" , 20 )
# * now its like 
#   - freezed rotation X : 0
#   - rotation translation X : 20
#   - actual rotation X : 20

# freeze translation.
cmds.makeIdentity( CudeTransformNode , a = 1 , r = 1 )
# * then its like 
#   - freezed rotation X : 20
#   - rotation translation X : 0
#   - actual rotation X : 20


# for test, rotate X 30 more degree.
cmds.setAttr( f"{CudeTransformNode}.rotateX" , 30 )
# * now its like 
#   - freezed rotation X : 20
#   - rotation translation X : 30
#   - actual rotation X : 50

# From here
# how to get actual rotation
Foo() == 50
# or how to get freezed rotation
Boo() == 20

** Above example, my question is how can we get the real rotation??( how to get 50 or 20 )**

Because every method I found is only telling you how to get current rotation( * rotation translation )

For reference :

All these are telling you to use Matrix to get rotation, but the Matrix returned from native commands are always reflecting the translated values only. Therefore, in above example, the calculated output will always be 30( current rotation ).

For instance :

import maya.api.OpenMaya as om

Matrix = cmds.xform( CudeTransformNode, q = 1 , m = 1 ) 
_M = om.MMatrix( Matrix )
_TM = om.MTransformationMatrix( _M )
_rotation_radiants = _TM.rotation()

_rotation_radiants[ 0 ] <-- # equals to 30 degree

# But I want to get 20 or 50...

Maybe the question is more correct to say, how to get overall rotation matrix?

Thank you for your advices!!

Micah
  • 4,254
  • 8
  • 30
  • 38
  • Did you try something like the function `xform()`? It has a `worldSpace` parameter to get world space transforms. – haggi krey Jul 13 '23 at 09:05
  • @haggikrey yes, tried, xform will only return current rotation translation regardless which space. – Micah Jul 13 '23 at 09:14
  • Strange. If I try this: `cmds.xform("pCube1", q=True, ws=True, ro=True)` with a hierarchy of three nodes, each rotated by -10 degree, I get the correct -30 for the cube transform node. – haggi krey Jul 13 '23 at 09:52
  • @haggikrey, if you freeze rotation, will you still get -30? – Micah Jul 13 '23 at 10:16
  • Freezing the rotation on the cube transform? Of course not because freezing means the transfomation is baked into the point positions. Then the result is -20 what is completely correct. – haggi krey Jul 13 '23 at 10:32
  • @haggikrey then my question will be how can I get the baked rotation? – Micah Jul 13 '23 at 10:52
  • Not sure I understand, what exactly do you mean with baked roation? If you mean freezing, then the transformations are baked into the point positions and the parent transforms are set to 0. This means that the transformation information is lost. – haggi krey Jul 13 '23 at 10:57
  • @haggikrey they dont have solid name to call this, so I used it as freezed rotation, rotation translation, actual rotation in my example. My question is how can I get rotation after freeze. In the xform way will be 0, but its rotated right, what is the rotation? – Micah Jul 13 '23 at 11:37
  • I thought I made it clear, if you feeeze anything, the transformation is baked into point positions, and transformation information is lost. – haggi krey Jul 13 '23 at 11:48
  • @haggikrey, umm, ok, I get your point now. Maybe, that is true.... But even the positional x y z, scaling x y z, I already found the way to get the numbers before and after freeze.....I think maybe rotation is highly possible too....yet the rotation calculations are just too difficult to understand... FYI, for positions, you can use pivot to get the actual positions and freezed positions. The dimensions, you can use bounding box with scaling to get actually dimensions and freezed dimensions. – Micah Jul 13 '23 at 11:59

1 Answers1

1

As haggy krey has pointed out in the comments, there is no direct way to retrieve the rotation after freezing the transform, since you are applying the transformation to the components and zero out the transform node. The rotation information is simply lost.

As you mention yourself, you can determine the scale after freezing, by using the bounding box, but this only works as long as you use primitives, where their bounding box would be equivalent to applying some scale.

What exactly are you trying to accomplish?

If you are trying to restore/determine some original orientation, then you can potentially use vector math to calculate a rotation - either from two vertices, the normal vector of a face or similar. But it would require that those components align with the original rotation.

import maya.cmds as cmds

angle_vector = [0.5, 0.7, 1.0]
up_vector = [0.0, 1.0, 0.0]

euler = cmds.angleBetween(euler=True, vector1=angle_vector, vector2=up_vector)

print(euler)

Another approach could be to create some helper node (e.g., an empty group), where you don't freeze rotations. Then you could simply add or subtract the rotation of the cube and the helper node.

Finally, you could also store the original rotation in some custom attributes on the object, and add or subtract those values from the cube's current rotation.

import maya.cmds as cmds

cube = cmds.polyCube()[0]
cmds.setAttr(f"{cube}.rx", 30)

cmds.addAttr(cube, ln="originalRotationX", sn="orx", at="double", dv=0)
cmds.setAttr(f"{cube}.originalRotationX", e=True, keyable=True)  # show in Channel Box

rotationX = cmds.getAttr(f"{cube}.rx")
cmds.setAttr(f"{cube}.originalRotationX", rotationX)
Morten
  • 487
  • 2
  • 13
  • Hi Morten, thank you for your advice, let me take some time to test your way now! – Micah Jul 27 '23 at 06:44
  • Hi Morten, thank you for your advice, my goal is find a way to calculate rotation. For instance, if the given object is already freeze but the task is to turn x up 50 degrees, if we cant get the current rotation numbers, the information already lost, isnt it impossible to calculate it to 50?? – Micah Jul 27 '23 at 06:56
  • Short answer: No. not unless you store this information somewhere, like a custom attribute as I suggested in my answer. Or any of the other methods. – Morten Jul 28 '23 at 07:07