Last active
January 23, 2020 03:11
-
-
Save renaudll/4fd557d34f726f28c0007558783af679 to your computer and use it in GitHub Desktop.
bebe_magic.py
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
| """ | |
| Connect meshes deformation from a rig to one or multiple destination rigs. | |
| The deformation is adapted to be relative from the rig root. <3 | |
| Usage: | |
| - Select one or multiple geometry or (group containing geometry) from a SOURCE rig. | |
| - Add to the selection one or multiple root ctrl from DESTINATION(s) rigs. | |
| - Run the tool | |
| """ | |
| from maya import cmds | |
| import logging | |
| _LOG = logging.getLogger() | |
| def _connect(src_mesh, dst_mesh, src_ctrl, dst_ctrl): | |
| _LOG.debug("Connect %r %r %r %r", src_mesh, dst_mesh, src_ctrl, dst_ctrl) | |
| multiplyMatrix = cmds.createNode("multMatrix") | |
| cmds.connectAttr(src_ctrl + ".worldInverseMatrix", multiplyMatrix + ".matrixIn[0]") | |
| cmds.connectAttr(dst_ctrl + ".worldMatrix", multiplyMatrix + ".matrixIn[1]") | |
| transformGeometry = cmds.createNode("transformGeometry") | |
| cmds.connectAttr(src_mesh + ".outMesh", transformGeometry + ".inputGeometry", force=True) | |
| cmds.connectAttr(multiplyMatrix + ".matrixSum", transformGeometry + ".transform", force=True) | |
| cmds.connectAttr(transformGeometry + ".outputGeometry", dst_mesh + ".inMesh", force=True) | |
| def _get_namespace(obj): | |
| """ | |
| >>> _get_namespace("namespace:name") | |
| 'namespace' | |
| >>> _get_namespace("name") # broken but we don't have to support it | |
| '' | |
| >>> _get_namespace("|namespace_a:parent|namespace_b:child") | |
| 'namespace_b' | |
| """ | |
| return obj.split("|")[-1].rsplit(":", 1)[0] | |
| def _remove_namespace(obj): | |
| """ | |
| >>> _remove_namespace("namespace:name") | |
| 'name' | |
| >>> _remove_namespace("name") | |
| 'name' | |
| >>> _remove_namespace("|namespace_a:parent|namespace_b:child") | |
| 'child' | |
| """ | |
| return obj.split("|")[-1].rsplit(":", 1)[-1] | |
| def _iter_meshes(sel): | |
| for obj in sel: | |
| for child in cmds.listRelatives(obj, fullPath=True, allDescendents=True): | |
| if cmds.nodeType(child) == "mesh" and not cmds.getAttr(child + ".intermediateObject"): | |
| yield child | |
| def _iter_curves(sel): | |
| for obj in sel: | |
| for shape in cmds.listRelatives(obj, shapes=True) or []: | |
| if cmds.nodeType(shape) == "nurbsCurve": | |
| transform = cmds.listRelatives(shape, parent=True)[0] # a shape always have a parent | |
| yield transform | |
| sel = cmds.ls(selection=True) | |
| # Get meshes (no namespace) | |
| meshes = tuple(_iter_meshes(sel)) | |
| curves = tuple(_iter_curves(sel)) | |
| dst_namespaces = {_get_namespace(curve) for curve in curves} | |
| # Resolve namespaces | |
| src_namespace = _get_namespace(next(iter(meshes))) | |
| ctrl_by_namespace = {_get_namespace(curve): curve for curve in curves} | |
| for dst_namespace in dst_namespaces: | |
| for mesh in meshes: | |
| mesh_name = _remove_namespace(mesh) | |
| mesh = _remove_namespace(mesh) | |
| ctrl = _remove_namespace(ctrl_by_namespace[dst_namespace]) | |
| src_mesh = src_namespace + ":" + mesh_name | |
| dst_mesh = dst_namespace + ":" + mesh_name | |
| src_ctrl = src_namespace + ":" + ctrl | |
| dst_ctrl = dst_namespace + ":" + ctrl | |
| _connect(src_mesh, dst_mesh, src_ctrl, dst_ctrl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment