Skip to content

Instantly share code, notes, and snippets.

@yamahigashi
Created November 25, 2019 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamahigashi/31b5c0c127a569dcbda38dba807a3cac to your computer and use it in GitHub Desktop.
Save yamahigashi/31b5c0c127a569dcbda38dba807a3cac to your computer and use it in GitHub Desktop.
An example of using `inspect` module to treat code as string.
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
import inspect
from textwrap import dedent
import maya.cmds as cmds
def add_command_menu(menu_name, label, parent, command_func, **args):
# type: (Text, Text, callable, Text, Optional[Dict]) -> None
code = inspect.getsource(command_func)
code = dedent("".join(code.splitlines(True)[1:]))
cmds.menuItem(
menu_name,
label=label,
parent=parent,
echoCommand=True,
command=code,
**args
)
# ----
def open_synoptic():
from mgear import synoptic
synoptic.open()
# the top menu
cmds.menu(
"HOGE_MENU_CONTAINER",
label="hoge",
parent='MayaWindow',
tearOff=True,
allowOptionBoxes=True
)
add_command_menu("animation_open_synoptic", "Synoptic", "HOGE_MENU_CONTAINER", open_synoptic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment