Skip to content

Commit 875208d

Browse files
committed
apply_pivot_offset_to_translation added. Store and apply world space updated #501
1 parent 2ab8070 commit 875208d

1 file changed

Lines changed: 57 additions & 12 deletions

File tree

release/scripts/mgear/core/transform.py

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,29 @@ def rotate_180(axis="Y", rotation_order="XYZ", objects=None):
10831083

10841084

10851085
# Wolrd Transform Functions: Transform data IO with pivot offset
1086+
def apply_pivot_offset_to_translation(obj):
1087+
"""Add the local rotate pivot to the object's current translation.
1088+
1089+
Args:
1090+
obj (str): Name of the object.
1091+
1092+
Raises:
1093+
RuntimeError: If the object does not exist.
1094+
"""
1095+
if not cmds.objExists(obj):
1096+
raise RuntimeError("Object '{}' does not exist.".format(obj))
1097+
1098+
# Check if any component of .translate is locked or connected
1099+
for axis in ["X", "Y", "Z"]:
1100+
attr = "{}.translate{}".format(obj, axis)
1101+
if cmds.getAttr(attr, lock=True):
1102+
return
1103+
1104+
translation = cmds.getAttr(obj + ".translate")[0]
1105+
rotate_pivot = cmds.getAttr(obj + ".rotatePivot")[0]
1106+
1107+
new_translation = [t - rp for t, rp in zip(translation, rotate_pivot)]
1108+
cmds.setAttr(obj + ".translate", *new_translation, type="double3")
10861109

10871110
def get_world_transform_data(obj):
10881111
"""Get world transform matrix and pivot of a given object.
@@ -1099,9 +1122,14 @@ def get_world_transform_data(obj):
10991122
if not cmds.objExists(obj):
11001123
raise RuntimeError("Object '{}' does not exist.".format(obj))
11011124

1102-
matrix = cmds.xform(obj, q=True, ws=True, m=True)
1103-
pivot = cmds.xform(obj, q=True, ws=True, rotatePivot=True)
1104-
return {'matrix': matrix, 'rotatePivot': pivot}
1125+
transform_data = {
1126+
'matrix': cmds.xform(obj, q=True, ws=True, m=True),
1127+
"translation": cmds.xform(obj, q=True, ws=True, t=True),
1128+
"rotation": cmds.xform(obj, q=True, ws=True, rotation=True),
1129+
"scale": cmds.xform(obj, q=True, ws=True, s=True),
1130+
"rotatePivot": cmds.xform(obj, q=True, ws=True, rotatePivot=True),
1131+
}
1132+
return transform_data
11051133

11061134

11071135
def set_world_transform_data(obj, transform_data):
@@ -1117,14 +1145,31 @@ def set_world_transform_data(obj, transform_data):
11171145
if not cmds.objExists(obj):
11181146
raise RuntimeError("Object '{}' does not exist.".format(obj))
11191147

1120-
matrix = transform_data.get('matrix')
1121-
pivot = transform_data.get('rotatePivot')
1122-
1123-
if matrix:
1124-
cmds.xform(obj, ws=True, m=matrix)
1125-
if pivot:
1126-
# restore both rotate- and scale-pivot in world space
1127-
cmds.xform(obj, ws=True, rotatePivot=pivot)
1128-
cmds.xform(obj, ws=True, scalePivot=pivot)
1148+
# matrix = transform_data.get('matrix')
1149+
# pivot = transform_data.get('rotatePivot')
1150+
1151+
if "matrix" in transform_data:
1152+
cmds.xform(obj, ws=True, m=transform_data["matrix"])
1153+
# reset rotatePivotTranslate to neutral value
1154+
cmds.setAttr(obj + ".rotatePivotTranslate", *[0,0,0], type="double3")
1155+
# re-apply the final position in world space
1156+
if "translation" in transform_data:
1157+
cmds.xform(
1158+
obj,
1159+
ws=True,
1160+
t=vector.subtract_3Dvectors_list(
1161+
transform_data["translation"], transform_data["rotatePivot"]
1162+
),
1163+
)
1164+
v1 = vector.subtract_3Dvectors_list(
1165+
transform_data["rotatePivot"], transform_data["translation"]
1166+
)
1167+
v2 = vector.add_3Dvectors_list(transform_data["translation"], v1)
1168+
cmds.xform(obj, ws=True, t=v2)
1169+
apply_pivot_offset_to_translation(obj)
1170+
# if pivot:
1171+
# # restore both rotate- and scale-pivot in world space
1172+
# cmds.xform(obj, ws=True, rotatePivot=pivot)
1173+
# cmds.xform(obj, ws=True, scalePivot=pivot)
11291174

11301175

0 commit comments

Comments
 (0)