Skip to content

Instantly share code, notes, and snippets.

@david-rhodes
Created December 16, 2021 08:46
Show Gist options
  • Save david-rhodes/982f28885ed6902feb33499f34a4566c to your computer and use it in GitHub Desktop.
Save david-rhodes/982f28885ed6902feb33499f34a4566c to your computer and use it in GitHub Desktop.
Maya Python Decorators
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