-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_createNullOffset.py
309 lines (267 loc) · 10.9 KB
/
mr_createNullOffset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
# ------------------------------------------------------------------------------ #
# SCRIPT: mr_createNullOffset.py
# VERSION: 0005
#
# CREATORS: Maria Robertson
# ---------------------------------------
#
# ---------------------------------------
# DESCRIPTION:
# ---------------------------------------
# Place each selected object into its own offset group, under their original parent.
# The Offset group will match the selected object's translation and rotation, to zero out their values.
#
# EXAMPLE USES:
# ---------------------------------------
# Can be helpful when modifying rigs.
# Can't be used with referenced rigs.
#
# INSTRUCTIONS:
# ---------------------------------------
# It's best to run this on the original bind pose of the rig.
#
# ---------------------------------------
# RUN COMMAND:
# ---------------------------------------
import importlib
import mr_createNullOffset
importlib.reload(mr_createNullOffset)
mr_createNullOffset.mr_createNullOffset_static()
OR
mr_createNullOffset.mr_createNullOffset_keyed()
# ---------------------------------------
# REQUIREMENTS:
# ---------------------------------------
# mr_bakeToWorldspace.py
# ---------------------------------------
# RESEARCH THAT HELPED:
# ---------------------------------------
# For listing relatives: https://forums.cgsociety.org/t/list-all-parents-of-an-object-mel/1806687/2
#
# The endsWith command: https://help.autodesk.com/cloudhelp/2016/CHS/Maya-LT-Tech-Docs/Commands/endsWith.html
# Learnt it because of this post: https://forums.autodesk.com/t5/maya-programming/selecting-all-polygon-objects-in-the-scene-with-a-certain-prefix/td-p/4240087
#
# ---------------------------------------
# WISHLIST:
# ---------------------------------------
# - Combine the two functions, to avoid repeated logic.
#
# ---------------------------------------
# CHANGELOG:
# ---------------------------------------
# 2023-07-19 - 0005:
# - Adding comments to script to make rereading easier.
# - Made bakeResults bake on both translate and rotate attributes, for mr_createNullOffset_keyed()
# Decided to have it, while experimenting with Aim Space scripts.
#
# 2023-06-29 - 0004:
# - For mr_createNullOffset_keyed, added function to set all transform keys on original selection to default values, keeping the original key timings.
# - Added mr_find_drivers_of_selected and baking for mr_createNullOffset_static to remove temp worldspace locators.
# - Made bakeResults only bake on translate attributes.
#
# 2023-06-29 - 0003:
# - Converted to Python
# - If a selected object has keyframes, bake the null group to match the translation
#
# 2022-12-17 - 0002:
# - Added checks for if selected object is already in an offset group.
#
# 2022-12-16 - 0001:
# - First pass of MEL version of script.
# ------------------------------------------------------------------------------ #
"""
import maya.cmds as cmds
import importlib
import mr_bakeToWorldspace
importlib.reload(mr_bakeToWorldspace)
import mr_find_constraint_targets_and_drivers
importlib.reload(mr_find_constraint_targets_and_drivers)
def mr_createNullOffset_keyed():
sel = cmds.ls(selection=True)
start_time = cmds.playbackOptions(q=True, min=True)
end_time = cmds.playbackOptions(q=True, max=True)
valid_objects = []
keyed_objects = []
nulls_for_keyed_objects = []
static_objects = []
# Organise selected objects into variables.
for item in sel:
parent = cmds.listRelatives(item, parent=True)
# If item is an offset group, or is already in an offset group, give an error.
if parent and parent[0].endswith("_offset_grp"):
print("NOTE: The selected item is already in an offset_grp.")
continue
elif item.endswith("_offset_grp"):
print("NOTE: The selected item is an offset group.")
continue
else:
valid_objects.append(item)
# Unlock attributes for clean parenting.
attrs = [".tx", ".ty", ".tz", ".rx", ".ry", ".rz"]
for attr in attrs:
cmds.setAttr(item + attr, lock=False)
# Check if item has keyframes.
has_keyframes = cmds.keyframe(item, query=True, keyframeCount=True)
if has_keyframes:
keyed_objects.append(item)
else:
static_objects.append(item)
# If keyed, bake them to spare locators, to hold their worldspace position while placing the item in a parent.
if keyed_objects:
cmds.select(keyed_objects)
mr_bakeToWorldspace.mr_bakeToWorldspace("both")
# Create null group.
# For each item,
for item in valid_objects:
# Create a null group.
parent = cmds.listRelatives(item, parent=True)
null = cmds.group(empty=True, name=item + "_offset_grp")
# Match the position and orientation.
cmds.parentConstraint(item, null)
# If item is keyed, add it to a variable to be baked later.
if item in keyed_objects:
nulls_for_keyed_objects.append(null)
# Otherwise, just delete the constraint.
elif item in static_objects:
cmds.delete(null + "_parentConstraint1")
# If item has a parent, place the null under it too.
if parent:
cmds.parent(null, parent[0])
# Parent item under the null.
cmds.parent(item, null)
# For every item with keys, bake their constrained nulls.
if nulls_for_keyed_objects:
cmds.refresh(suspend=True)
cmds.bakeResults(
nulls_for_keyed_objects,
simulation=True,
attribute=['translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ' ], # Only key translate attributes
time=(start_time, end_time),
sampleBy=1,
disableImplicitControl=True,
preserveOutsideKeys=True,
sparseAnimCurveBake=False,
removeBakedAttributeFromLayer=False,
removeBakedAnimFromLayer=False,
bakeOnOverrideLayer=False,
minimizeRotation=True,
controlPoints=False
)
cmds.filterCurve()
# Delete constraints on the nulls after baking.
cmds.delete(nulls_for_keyed_objects, constraints=True)
cmds.refresh(suspend=False)
# Key the original selection, before deleting their drivers
cmds.setKeyframe(sel, attribute='translateX')
cmds.setKeyframe(sel, attribute='translateY')
cmds.setKeyframe(sel, attribute='translateZ')
cmds.setKeyframe(sel, attribute='rotateX')
cmds.setKeyframe(sel, attribute='rotateY')
cmds.setKeyframe(sel, attribute='rotateZ')
# Delete their drivers.
cmds.select(sel)
mr_find_constraint_targets_and_drivers.mr_find_drivers_of_selected()
cmds.delete()
# Remove keys on original selection (to not double the motion)
cmds.select(sel)
set_transform_values_to_default()
"""
# Finish script by selecting the last item
last_item = sel[-1]
cmds.select(last_item, replace=True)
"""
cmds.select(sel)
def mr_createNullOffset_static():
sel = cmds.ls(selection=True)
start_time = cmds.playbackOptions(q=True, min=True)
end_time = cmds.playbackOptions(q=True, max=True)
keyed_objects = []
for item in sel:
parent = cmds.listRelatives(item, parent=True)
if parent and parent[0].endswith("_offset_grp"):
print("NOTE: The selected item is already in an offset_grp.")
continue
elif item.endswith("_offset_grp"):
print("NOTE: The selected item is an offset group.")
continue
else:
# Unlock attributes for clean parenting
attrs = [".tx", ".ty", ".tz", ".rx", ".ry", ".rz"]
for attr in attrs:
cmds.setAttr(item + attr, lock=False)
# Check if the item has keyframes
has_keyframes = cmds.keyframe(item, query=True, keyframeCount=True)
if has_keyframes:
keyed_objects.append(item)
if keyed_objects:
cmds.select(keyed_objects)
mr_bakeToWorldspace.mr_bakeToWorldspace("both")
for item in sel:
if parent and parent[0].endswith("_offset_grp"):
continue
elif item.endswith("_offset_grp"):
continue
else:
# Create group
null = cmds.group(empty=True, name=item + "_offset_grp")
# Match position and orientation
cmds.parentConstraint(item, null)
cmds.delete(null + "_parentConstraint1")
# Parent nulls
if parent:
cmds.parent(null, parent[0])
cmds.parent(item, null)
# Lock attributes of offset group
attrs += [".sx", ".sy", ".sz", ".v"]
for attr in attrs:
cmds.setAttr(null + attr, lock=True)
if keyed_objects:
cmds.refresh(suspend=True)
cmds.bakeResults(
keyed_objects,
simulation=True,
time=(start_time, end_time),
attribute=['translateX', 'translateY', 'translateZ'], # Only key translate attributes
sampleBy=1,
disableImplicitControl=True,
preserveOutsideKeys=True,
sparseAnimCurveBake=False,
removeBakedAttributeFromLayer=False,
removeBakedAnimFromLayer=False,
bakeOnOverrideLayer=False,
minimizeRotation=True,
controlPoints=False
)
cmds.filterCurve()
cmds.refresh(suspend=False)
# Delete drivers
cmds.select(sel)
mr_find_constraint_targets_and_drivers.mr_find_drivers_of_selected()
cmds.delete()
"""
# Finish script by selecting the last item
last_item = sel[-1]
cmds.select(last_item, replace=True)
"""
cmds.select(sel)
def set_transform_values_to_default():
selected_objects = cmds.ls(selection=True)
if selected_objects:
for obj in selected_objects:
anim_curves = cmds.keyframe(obj, query=True, name=True)
if anim_curves:
for curve in anim_curves:
keyframes = cmds.keyframe(curve, query=True, timeChange=True)
"""
for frame in keyframes:
cmds.setKeyframe(curve, time=(frame, frame), value=0)
"""
for frame in keyframes:
if 'translate' in curve or 'rotate' in curve:
cmds.setKeyframe(curve, time=(frame, frame), value=0)
cmds.keyTangent(curve, edit=True, itt="flat", ott="flat")
elif 'scale' in curve:
cmds.setKeyframe(curve, time=(frame, frame), value=1)
cmds.keyTangent(curve, edit=True, itt="flat", ott="flat")
cmds.select(selected_objects)