Created
November 25, 2019 08:46
-
-
Save yamahigashi/31b5c0c127a569dcbda38dba807a3cac to your computer and use it in GitHub Desktop.
An example of using `inspect` module to treat code as string.
This file contains 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 re | |
import math | |
import inspect | |
import textwrap | |
import maya.cmds as cmds | |
import exprespy.cmd | |
def rotate_driver(COUNT, IN, OUT): | |
# initialization | |
if not COUNT: | |
from maya.api.OpenMaya import ( | |
MVector, | |
MQuaternion, | |
MEulerRotation, | |
) | |
from math import atan2, pi as _PI | |
from math import sin, cos, tan | |
# ---- snip ---------------------- | |
input_rot = MEulerRotation(IN.get(AT_ROTATE, ZERO3), IN.get(AT_ROTATE_ORDER, 0)).asQuaternion() | |
OUT.rotate = input_rot | |
def create_node(func, name, rewrite_map): | |
code = inspect.getsource(func) | |
code = textwrap.dedent("".join(code.splitlines(True)[1:])) | |
for src, dst in rewrite_map: | |
code = re.sub(r"{}(\W)".format(src), "{}\\1".format(dst), code) | |
exp_node = cmds.createNode("exprespy", name=name) | |
cmds.setAttr("{}.code".format(exp_node), code, type="string") | |
exprespy.cmd.setCode(exp_node, code, raw=False) | |
return exp_node |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment