
MPlugs: Connecting Plugs
Connecting two MPlugs together is fairly simple and is accomplished with MDGModifier.connect(). Lets connect two polygon spheres together such that translating one on X will translate the other on Y.
First create the spheres and get them as MObjects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import maya.OpenMaya as OpenMaya import maya.cmds as cmds def getMObject( obj_name ): sel_list = OpenMaya.MSelectionList() try: sel_list.add( obj_name ) obj = OpenMaya.MObject() sel_list.getDependNode( 0, obj ) return obj except: pass cmds.polySphere( name='plugTest' ) sphere_one = getMObject( 'plugTest' ) cmds.polySphere( name='plugTest2') sphere_two = getMObject( 'plugTest2' ) |
Now we can write a function that makes use of MDGModifier.connect() to hook the spheres together. Connect takes two inputs – source MPlug and destination MPlug. In a previous post I covered how to get an MPlug on a node which will come in handy here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
def getPlugByName( inObj, inPlugName ): """ Gets a node's plug as an MPlug. @inObj: MObject. Node to get plug from. @inPlugName: String. Name of plug to get from node. @return: MPlug. @return: None. """ depFn = OpenMaya.MFnDependencyNode( inObj ) try: plug = depFn.findPlug( inPlugName ) return plug except: return None def connectNodes( source_obj, source_plug_name, destination_obj, destination_plug_name ): ''' @param source_obj: MObject. Source object. @param source_plug_name: String. Name of plug on parent node. @param destination_obj: MObject. Destination object. @param destination_plug_name: String. Name of plug on child node. ''' source_plug = getPlugByName( source_obj, source_plug_name ) destination_plug = getPlugByName( destination_obj, destination_plug_name ) MDGMod = OpenMaya.MDGModifier() MDGMod.connect( source_plug, destination_plug ) MDGMod.doIt() |
Here’s everything together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import maya.OpenMaya as OpenMaya import maya.cmds as cmds def getPlugByName( inObj, inPlugName ): """ Gets a node's plug as an MPlug. @inObj: MObject. Node to get plug from. @inPlugName: String. Name of plug to get from node. @return: MPlug. @return: None. """ depFn = OpenMaya.MFnDependencyNode( inObj ) try: plug = depFn.findPlug( inPlugName ) return plug except: return None def connectNodes( source_obj, source_plug_name, destination_obj, destination_plug_name ): ''' @param source_obj: MObject. Source object. @param source_plug_name: String. Name of plug on parent node. @param destination_obj: MObject. Destination object. @param destination_plug_name: String. Name of plug on child node. ''' source_plug = getPlugByName( source_obj, source_plug_name ) destination_plug = getPlugByName( destination_obj, destination_plug_name ) MDGMod = OpenMaya.MDGModifier() MDGMod.connect( source_plug, destination_plug ) MDGMod.doIt() def getMObject( obj_name ): sel_list = OpenMaya.MSelectionList() try: sel_list.add( obj_name ) obj = OpenMaya.MObject() sel_list.getDependNode( 0, obj ) return obj except: pass cmds.polySphere( name='plugTest' ) sphere_one = getMObject( 'plugTest' ) cmds.polySphere( name='plugTest2') sphere_two = getMObject( 'plugTest2' ) connectNodes( sphere_one, 'translateX', sphere_two, 'translateY' ) |