def test_dgmod(self): persp = Node("persp") front = Node("front") side = Node("side") # SIMPLE CONNECTION ################ # start undo uobj = undo.StartUndo() dgmod = undo.DGModifier() assert len(sys._maya_stack) == 1 dgmod.connect(persp.message, front.isHistoricallyInteresting) dgmod.doIt() # create undo step del (uobj) assert len(sys._maya_stack) == 0 cmds.undo() # undo connection # check connection - should be undone assert not persp.message.misConnectedTo( front.isHistoricallyInteresting) cmds.redo() # redo it and check connection assert persp.message.misConnectedTo(front.isHistoricallyInteresting) # connect and break existing conenction uobj = undo.StartUndo() dgmod = undo.DGModifier() dgmod.disconnect(persp.message, front.isHistoricallyInteresting) dgmod.connect(side.message, front.isHistoricallyInteresting) dgmod.doIt() del (uobj) assert side.message.misConnectedTo(front.isHistoricallyInteresting) cmds.undo() # old connection should be back assert persp.message.misConnectedTo(front.isHistoricallyInteresting) # undo first change cmds.undo() # EMPTY DOIT ################ undo.startUndo() dgmod = undo.DGModifier() dgmod.doIt() undo.endUndo() cmds.undo()
def mconnectTo( self, destplug, force=True ): """Connect this plug to the right hand side plug :param destplug: the plug to which to connect this plug to. :param force: if True, the connection will be created even if another connection has to be broken to achieve that. If False, the connection will fail if destplug is already connected to another plug :return: destplug allowing chained connections a.connectTo(b).connectTo(c) :raise RuntimeError: If destination is already connected and force = False""" mod = undo.DGModifier( ) # is destination already input-connected ? - disconnect it if required # Optimization: We only care if force is specified. It will fail otherwise if force: destinputplug = destplug.minput() if not destinputplug.isNull(): # handle possibly connected plugs if self == destinputplug: # is it us already ? return destplug # disconnect mod.disconnect( destinputplug, destplug ) # END disconnect existing # END destination is connected # END force mode mod.connect( self, destplug ) # finally do the connection try: mod.doIt( ) except RuntimeError: raise RuntimeError("Failed to connect %s to %s as destination is already connected or incompatible" % (self, destplug)) # END connection failed handling return destplug
def mconnectMultiToMulti(self, iter_source_destination, force=False): """Connect multiple source plugs to the same amount of detsination plugs. :note: This method provides the most efficient way to connect a large known amount of plugs to each other :param iter_source_destination: Iterator yielding pairs of source and destination plugs to connect :param force: If True, existing input connections on the destination side will be broken automatically. Otherwise the whole operation will fail if one connection could not be made. :note: Both iterators need to yield the same total amount of plugs :note: In the current implementation, performance will be hurt if force is specified as each destination has to be checked for a connection in advance""" mod = undo.DGModifier( ) for source, dest in iter_source_destination: if force: destinputplug = dest.minput() if not destinputplug.isNull(): if source == destinputplug: continue # END skip this plug if it is already connected mod.disconnect(destinputplug, dest) # END destination is connected # END handle force mod.connect(source, dest) # END for each source, dest pair mod.doIt() return mod
def mdisconnectFrom( self, other ): """Disconnect this plug from other plug if they are connected :param other: MPlug that will be disconnected from this plug :return: other plug allowing to chain disconnections""" try: mod = undo.DGModifier( ) mod.disconnect( self, other ) mod.doIt() except RuntimeError: pass return other
def mdisconnectInput( self ): """Disconnect the input connection if one exists :return: self, allowing chained commands""" inputplug = self.minput() if inputplug.isNull(): return self mod = undo.DGModifier( ) mod.disconnect( inputplug, self ) mod.doIt() return self
def mdisconnectOutputs( self ): """Disconnect all outgoing connections if they exist :return: self, allowing chained commands""" outputplugs = self.moutputs() if not len( outputplugs ): return self mod = undo.DGModifier() for destplug in outputplugs: mod.disconnect( self, destplug ) mod.doIt() return self