-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_bakeToWorldspace.py
216 lines (184 loc) · 8.25 KB
/
mr_bakeToWorldspace.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
"""
# ------------------------------------------------------------------------------ #
# SCRIPT: mr_bakeToWorldspace.py
# VERSION: 0007
#
# CREATORS: Maria Robertson
# CREDIT: Richard Lico (for workflow)
# ---------------------------------------
# Last tested for Autodesk Maya 2023.3
# ---------------------------------------
# DESCRIPTION:
# ---------------------------------------
# Bake selected objects to worldspace translation and rotation.
# Also bake follow locators.
#
# INSTRUCTIONS:
# ---------------------------------------
# Select objects you would like to bake to worldspace, and run the function in one of the three modes:
# - "both" : Convert to both worldspace translation and rotation.
# - "translate" : Convert to just worldspace translation.
# - "rotate" : Convert to just worldspace rotation (locator will follow objects translation).
#
#
# EXAMPLE USES:
# On a shelf button, single-click to create a worldspace locator constraint. Double-click to just create a follow locator.
#
# ---------------------------------------
# RUN COMMAND:
# ---------------------------------------
import importlib
import mr_bakeToWorldspace
importlib.reload(mr_bakeToWorldspace)
# USE ONE OF THE FOLLOWING COMMANDS:
# Use these for the default settings.
mr_bakeToWorldspace.main("both")
mr_bakeToWorldspace.main("translate")
mr_bakeToWorldspace.main("rotate")
# Slower version for baking dynamic objects:
mr_bakeToWorldspace.main(mode="both", constrain=True, simulate_bake=True)
mr_bakeToWorldspace.main(mode="both", constrain=True, simulate_bake=True)
mr_bakeToWorldspace.main(mode="both", constrain=True, simulate_bake=True)
# To create locators without constraining anything to them:
# Faster version:
mr_bakeToWorldspace.main(mode="both", constrain=False, simulate_bake=False)
mr_bakeToWorldspace.main(mode="both", constrain=False, simulate_bake=False)
mr_bakeToWorldspace.main(mode="both", constrain=False, simulate_bake=False)
# Slower version for baking dynamic objects:
mr_bakeToWorldspace.main(mode="both", constrain=False, simulate_bake=True)
mr_bakeToWorldspace.main(mode="both", constrain=False, simulate_bake=True)
mr_bakeToWorldspace.main(mode="both", constrain=False, simulate_bake=True)
# ---------------------------------------
# REQUIREMENTS:
# ---------------------------------------
# The mr_utilities.py file, for support functions:
# https://github.com/maria137-art/MayaAnimScripts/blob/main/mr_utilities.py
#
# ---------------------------------------
# RESEARCH THAT HELPED:
# ---------------------------------------
# Richard Lico's "Space Switching for Animators" course on Animation Sherpa.
#
# WISH LIST:
# ---------------------------------------
# - Give warning if selected object has its relevant attributes constrained.
#
# ------------------------------------------------------------------------------ #
"""
import maya.cmds as cmds
import maya.mel as mel
import importlib
import mr_utilities
importlib.reload(mr_utilities)
def main(mode=None, constrain=True, simulate_bake=False):
# -------------------------------------------------------------------
# 01. DEFINE TIMESLIDER RANGE.
# -------------------------------------------------------------------
start_time = cmds.playbackOptions(query=True, min=True)
end_time = cmds.playbackOptions(query=True, max=True)
selection = cmds.ls(selection=True)
if not selection:
cmds.confirmDialog(title="Confirm", message="You need to select something first")
return
locators = []
constraints = []
# Store the lock state of the BaseAnimation animation layer.
if cmds.objExists("BaseAnimation"):
is_baseAnimation_locked = cmds.getAttr("BaseAnimation" + ".lock")
if is_baseAnimation_locked:
cmds.animLayer("BaseAnimation", edit=True, lock=False)
# -------------------------------------------------------------------
# 01. CREATE A LOCATOR PER OBJECT.
# -------------------------------------------------------------------
for item in selection:
locator_name = item + "_temp_worldspace_locator"
locators.append(locator_name)
locator = cmds.spaceLocator(name=locator_name)[0]
cmds.setAttr(locator + ".localScale", 18, 18, 18)
point_constraint = cmds.pointConstraint(item, locator)
orient_constraint = cmds.orientConstraint(item, locator)
constraints.append(point_constraint)
constraints.append(orient_constraint)
# -------------------------------------------------------------------
# 01. SELECT OBJECTS THAT ONLY HAVE KEYFRAMES.
# -------------------------------------------------------------------
cmds.refresh(suspend=True)
attributes = ["translateX", "translateY", "translateZ","rotateX", "rotateY", "rotateZ"]
cmds.bakeResults(
locators,
attribute = attributes,
simulation=simulate_bake,
time=(start_time, end_time),
sampleBy=1,
disableImplicitControl=True,
preserveOutsideKeys=True,
minimizeRotation=True
)
# Delete static channels.
cmds.delete(locators, sc=True)
# Filter curves and delete constraints.
for constraint in constraints:
cmds.filterCurve(constraint)
cmds.delete(constraint)
cmds.refresh(suspend=False)
if constrain == True:
# -------------------------------------------------------------------
# 01. REVERSE CONSTRAINTS
# -------------------------------------------------------------------
for i, item in enumerate(selection):
locator = locators[i]
if mode == "both":
mr_utilities.constrain_unlocked_translates(locator, item)
mr_utilities.constrain_unlocked_rotates(locator, item)
if mode == "translate":
mr_utilities.constrain_unlocked_translates(locator, item)
# End with the Translate manipulator on.
mel.eval("buildTranslateMM ;")
mel.eval("destroySTRSMarkingMenu MoveTool ;")
if mode == "rotate":
mr_utilities.constrain_unlocked_rotates(locator, item)
cmds.pointConstraint(item, locator)
# End with Rotate manipulator active.
mel.eval("buildRotateMM ;")
mel.eval("destroySTRSMarkingMenu RotateTool ;")
# Lock and hide attributes on the locator if corresponding ones on the target are locked.
mr_utilities.set_corresponding_attribute_states(locator, item, mode, keyable=False, lock=True)
# End with the locators selected.
cmds.select(locators)
# Restore original lock state of BaseAnimation.
if cmds.objExists("BaseAnimation"):
if is_baseAnimation_locked:
cmds.animLayer("BaseAnimation", edit=True, lock=True)
"""
##################################################################################################################################################
# ---------------------------------------
# CHANGELOG:
# ---------------------------------------
# 2024-01-20- 0007:
# - Checking if the BaseAnimation animation layer is locked before running tool, to avoid potential bugs.
# - Moving changelog to the bottom.
# - Moving support functions to mr_utilities.py.
# - Renaming lock_and_hide_corresponding_attributes() inside it, to set_corresponding_attribute_states().
#
# 2023-12-28 - 0006:
# - Adding option to just bake a worldspace locator, without reversing constraints.
# - Adding option to bake with or without simulation, depending on needing to bake fast vs baking physics.
#
# 2023-12-17 - 0005:
# - End script with relevant manipulators active.
#
# 2023-12-06 - 0004:
# - Fixed lock_and_hide_corresponding_attributes to lock and hide attributes on multiple objects.
#
# 2023-12-06 - 0003:
# - Adding functions to hide and ignore locked and unkeyable attributes on targests, avoiding errors.
#
# 2023-07-10 - 0002:
# - Fixing issue with locking constrained attributes.
#
# 2023-06-30 - 0001:
# - First pass of workflow in Python, to bake all objects at once.
# - Provide 3 options.
# ---------------------------------------
##################################################################################################################################################
"""