Created
December 16, 2021 08:46
-
-
Save david-rhodes/982f28885ed6902feb33499f34a4566c to your computer and use it in GitHub Desktop.
Maya Python Decorators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import msvcrt | |
import maya.cmds as cmds | |
import functools | |
def interactive(fn): | |
EXIT = "exitObject" | |
@functools.wraps(fn) | |
def wrapped(*args, **kwargs): | |
if cmds.objExists(EXIT): | |
cmds.delete(EXIT) | |
while(not fn()): | |
# refresh is what allows user interaction as this is happening | |
cmds.refresh() | |
# this is our emergency exit from the infinite loop | |
# assign the creation of this object to a shortcut | |
if cmds.objExists(EXIT): | |
print "Escaping . . . " | |
cmds.delete(EXIT) | |
break | |
return wrapped | |
# only queries the selection once to save processing | |
# wrapping like this could allow passing arguments to interactive (such as selection or frame count) | |
def groundCollision(): | |
sel = cmds.ls(sl = True) | |
if sel: | |
@interactive | |
def collide(): | |
x = cmds.getAttr("{0}.ty".format(sel[0])) | |
if x < 0: | |
cmds.setAttr("{0}.ty".format(sel[0]), 0) | |
collide() | |
# intractive could accept a max duration before exiting, or the name of the exit object | |
@interactive | |
def recordAnimation(): | |
sel = cmds.ls(sl = True) | |
if sel: | |
cmds.setKeyframe(sel) | |
cmds.currentTime(cmds.currentTime(q = True) + .3, e = True) | |
# optional: this allows a natural exit | |
#if cmds.currentTime(q = True) > 100: | |
#return True | |
# thse will run in consecutive order | |
recordAnimation() | |
# needs selection beforehand | |
groundCollision() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment