Skip to content

Commit 92cc57c

Browse files
committed
Add ability to set/apply a retarget scale factor to animations in Maya.
1 parent 68c9646 commit 92cc57c

File tree

1 file changed

+99
-18
lines changed

1 file changed

+99
-18
lines changed

plugins/maya/castplugin.py

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
except NameError:
2323
xrange = range
2424

25-
# Used for various configuration
25+
# Used for various configuration (Persists to disk)
2626
sceneSettings = {
2727
"importAtTime": False,
2828
"importSkin": True,
@@ -44,6 +44,11 @@
4444
"setupArnoldHair": True,
4545
}
4646

47+
# Used for runtime configuration (Does not persist to disk)
48+
runtimeSettings = {
49+
"retargetScale": 1.0,
50+
}
51+
4752
# Shared version number
4853
version = "1.93"
4954

@@ -266,6 +271,67 @@ def utilityEditNotetracks():
266271
cmds.showWindow(window)
267272

268273

274+
def utilityRetargetScale():
275+
result = cmds.promptDialog(title="Cast - Retarget Scale",
276+
message="Enter in a retarget scale factor:\t\t ",
277+
text=str(runtimeSettings["retargetScale"]),
278+
button=["Set", "Apply", "Cancel"],
279+
defaultButton="Set",
280+
cancelButton="Cancel",
281+
dismissString="Cancel")
282+
283+
if result != "Apply" and result != "Set":
284+
return
285+
286+
scaleText = cmds.promptDialog(query=True, text=True)
287+
scaleText = scaleText.strip()
288+
289+
try:
290+
scale = float(scaleText)
291+
except ValueError:
292+
cmds.error("Retarget scale factor must be a number: \"%s\"" %
293+
scaleText)
294+
return
295+
296+
# Set the runtime retarget scale, so that we can apply it to other animations going forward.
297+
if result == "Set":
298+
runtimeSettings["retargetScale"] = scale
299+
return
300+
301+
# Apply the retarget scale without setting the runtime value to just this animation.
302+
joints = cmds.ls(type="joint")
303+
304+
progress = utilityCreateProgress("Applying retarget scale...",
305+
len(joints) * 3)
306+
307+
for joint in joints:
308+
for attr in ["translateX", "translateY", "translateZ"]:
309+
keyframes = cmds.keyframe(joint, at=attr, query=True, tc=True)
310+
311+
if not keyframes:
312+
utilityStepProgress(progress, "Applying retarget scale...")
313+
continue
314+
315+
values = cmds.keyframe(joint,
316+
at=attr,
317+
query=True,
318+
valueChange=True)
319+
values = [x * scale for x in values]
320+
321+
for keyframe, value in zip(keyframes, values):
322+
cmds.keyframe(joint,
323+
at=attr,
324+
edit=True,
325+
valueChange=value,
326+
time=(keyframe,))
327+
utilityStepProgress(progress, "Applying retarget scale...")
328+
utilityEndProgress(progress)
329+
330+
331+
def utilityResetRetargetScale():
332+
runtimeSettings["retargetScale"] = 1.0
333+
334+
269335
def utilityGetDagPath(pathName):
270336
selectList = OpenMaya.MSelectionList()
271337
selectList.add(pathName)
@@ -450,10 +516,10 @@ def utilityQueryToggleItem(name):
450516

451517

452518
def utilityLoadSettings():
453-
global sceneSettings
454-
455519
currentPath = os.path.dirname(
456-
os.path.realpath(cmds.pluginInfo("castplugin", q=True, p=True)))
520+
os.path.realpath(cmds.pluginInfo("castplugin",
521+
q=True,
522+
p=True)))
457523
settingsPath = os.path.join(currentPath, "cast.cfg")
458524

459525
try:
@@ -470,10 +536,10 @@ def utilityLoadSettings():
470536

471537

472538
def utilitySaveSettings():
473-
global sceneSettings
474-
475539
currentPath = os.path.dirname(
476-
os.path.realpath(cmds.pluginInfo("castplugin", q=True, p=True)))
540+
os.path.realpath(cmds.pluginInfo("castplugin",
541+
q=True,
542+
p=True)))
477543
settingsPath = os.path.join(currentPath, "cast.cfg")
478544

479545
try:
@@ -540,6 +606,14 @@ def utilityCreateMenu(refresh=False):
540606
cmds.menuItem("editNotetracks", label="Edit Notifications",
541607
annotation="Edit the animations notifications", command=lambda x: utilityEditNotetracks())
542608

609+
cmds.menuItem(divider=True)
610+
611+
cmds.menuItem("retargetScale", label="Retarget Scale",
612+
annotation="Apply a retarget scale factor to an animation", command=lambda x: utilityRetargetScale())
613+
614+
cmds.menuItem("resetRetargetScale", label="Reset Retarget Scale",
615+
annotation="Resets the retarget scale factor", command=lambda x: utilityResetRetargetScale())
616+
543617
cmds.setParent(menu, menu=True)
544618

545619
cmds.menuItem(label="Model", subMenu=True)
@@ -1262,13 +1336,18 @@ def utilityImportSingleTrackData(tracks, property, timeUnit, frameStart, frameBu
12621336
else:
12631337
rest = 0.0
12641338

1339+
if property in ["tx", "ty", "tz"]:
1340+
scaleFactor = runtimeSettings["retargetScale"]
1341+
else:
1342+
scaleFactor = 1.0
1343+
12651344
# Default track mode is absolute meaning that the
12661345
# values are what they should be in the curve already
12671346
if mode == "absolute" or mode is None:
12681347
curveValueBuffer = OpenMaya.MDoubleArray(len(valueBuffer), 0.0)
12691348

12701349
for i, value in enumerate(valueBuffer):
1271-
curveValueBuffer[i] = value
1350+
curveValueBuffer[i] = value * scaleFactor
12721351
# Additive curves are applied to any existing curve value in the scene
12731352
# so we will add it to the sample at the given time
12741353
elif mode == "additive":
@@ -1281,21 +1360,23 @@ def utilityImportSingleTrackData(tracks, property, timeUnit, frameStart, frameBu
12811360
sample = rest
12821361

12831362
if property in ["sx", "sy", "sz"]:
1284-
curveValueBuffer[i] = utilityLerp(
1285-
sample, sample * value, blendWeight)
1363+
curveValueBuffer[i] = utilityLerp(sample,
1364+
sample * value * scaleFactor,
1365+
blendWeight)
12861366
else:
1287-
curveValueBuffer[i] = utilityLerp(
1288-
sample, sample + value, blendWeight)
1367+
curveValueBuffer[i] = utilityLerp(sample,
1368+
sample + value * scaleFactor,
1369+
blendWeight)
12891370
# Relative curves are applied against the resting position value in the scene
12901371
# we will add it to the rest position
12911372
elif mode == "relative":
12921373
curveValueBuffer = OpenMaya.MDoubleArray(len(valueBuffer), 0.0)
12931374

12941375
for i, value in enumerate(valueBuffer):
12951376
if property in ["sx", "sy", "sz"]:
1296-
curveValueBuffer[i] = rest * value
1377+
curveValueBuffer[i] = rest * value * scaleFactor
12971378
else:
1298-
curveValueBuffer[i] = rest + value
1379+
curveValueBuffer[i] = rest + value * scaleFactor
12991380

13001381
if timeBuffer.length() <= 0:
13011382
return (smallestFrame, largestFrame)
@@ -2348,10 +2429,10 @@ def importAnimationNode(node, path):
23482429
if wantedSmallestFrame == OpenMaya.MTime(sys.maxsize, wantedFps):
23492430
wantedSmallestFrame = OpenMaya.MTime(0, wantedFps)
23502431

2351-
sceneAnimationController.setAnimationStartEndTime(
2352-
wantedSmallestFrame, wantedLargestFrame)
2353-
sceneAnimationController.setMinMaxTime(
2354-
wantedSmallestFrame, wantedLargestFrame)
2432+
sceneAnimationController.setAnimationStartEndTime(wantedSmallestFrame,
2433+
wantedLargestFrame)
2434+
sceneAnimationController.setMinMaxTime(wantedSmallestFrame,
2435+
wantedLargestFrame)
23552436
sceneAnimationController.setCurrentTime(wantedSmallestFrame)
23562437

23572438

0 commit comments

Comments
 (0)