Skip to content

Instantly share code, notes, and snippets.

@benmorgantd
Last active January 4, 2019 05:49
Show Gist options
  • Save benmorgantd/92bbda4405b366f7c40e4368c0126a61 to your computer and use it in GitHub Desktop.
Save benmorgantd/92bbda4405b366f7c40e4368c0126a61 to your computer and use it in GitHub Desktop.
Geometry Control using Nodes
import maya.cmds as cmds
import maya.mel as mel
def geoCtrl(name="geoCtrl_1", geo=[], child=None):
"""
:param name: The name for our geometry control.
:param geo: The list of our face selection. You can just pass it cmds.ls(sl=1)
:param child: The child that our control will manipulate.
:return: [ctrl, ctrlShape]
"""
if len(geo) == 0:
return
if not child:
return
# get the object's name
object = geo[0][0:geo[0].index(".")]
objectShape = cmds.pickWalk(object, d="down")[0]
cmds.select(cl=1)
# first duplicate our object
ctrl = cmds.duplicate(object, name=name)[0]
ctrlShape = cmds.pickWalk(ctrl, d="down")[0]
# now create a "transformGeometry" node. This will allow us to get rid of our double transforms
tGeo = cmds.createNode("transformGeometry", n=name + "_transformGEO_#")
# connect our objectShape's outMesh to the inputMesh of the transformGeometry node
cmds.connectAttr(objectShape + ".outMesh", tGeo + ".inputGeometry")
# connect our control's worldInverseMatrix to the transformGeometry's transform input
cmds.connectAttr(ctrl + ".worldInverseMatrix[0]", tGeo + ".transform")
# now connect the transformGeometry's outputGeometry to our ctrlShape's inputGeometry
cmds.connectAttr(tGeo + ".outputGeometry", ctrlShape + ".inMesh")
# select the original selection again but this time on our new mesh. Invert the selection and delete it
cmds.select(cl=1)
for sel in geo:
cmds.select(ctrl + sel[sel.index("."):], add=1)
mel.eval("invertSelection;")
cmds.delete()
cmds.select(cl=1)
# center our control's pivot based on it's new geometry
cmds.xform(ctrl, cp=1)
# unlock all of the keyable transform attributes in our new control
for attr in cmds.listAttr(ctrl, k=1):
cmds.setAttr(ctrl + ".%s" % attr, l=0)
# finally parent our control to the child we wanted
cmds.parentConstraint(ctrl, child, mo=1)
return [ctrl, ctrlShape]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment