Published using Google Docs
naSwitch.txt

###

#Author: Nathaniel Anozie

#    ogbonnawork at gmail dot com

#Acknowledgement:

#Arun Pragash (pjarunpragash dot webs dot com)        i was inspired by his solution to space switching

#Matt Stoneham (td-matt dot blogspot dot com) i learned about what space switching was

#David Johnson (djx dot com dot au)        i learned about ways of solving the space switching problem

#Davide Lasala (davidelasala dot com)         i learned about how to program with python in maya

#Chad Vernon (chadvernon dot com)        i learned about how to program with python in maya

#Patrick O Brien (Guide to Python introspection, ibm dot com) i learned about finding/calling functions from other python objects

#Ryan Trowbridge (rtrowbridge dot com)        i learned about importing python scripts into maya and python user interfaces in maya

#Greg Elshoff (kickstand dot tv)                i learned about python user interfaces in maya

#

#

#Description: given two control names entered in user-interface make one control move from the other controls pivot

#supports any nurbs/poly/transform where:

#   driven has a (non-world) parent null with one child

#   driver has no incoming connections and/or constraints

#

#tested for Maya 2008

#Date Created: October 23, 2011

#Date Last Revision: October 23, 2011

#Version: 1.0

#Modify at your own risk

###

try:

    #tool classes

    import UISetup

    import MainStorage

    import DataGetter

   

    #user interface classes

    import FunctionObject

    import TextGroup

    import UIGetter

    import UIQuitter

    import Window

    import Layout

    import Button

    import WidgetHandler

    import UserInterfaceChecker

   

    import Transform

    import DisplayEditor

    import SceneCheck

    import maya.cmds as mc

except:

    print 'cannot find a module'

def naSwitch():

    """

    needs no arguments

        Helps user by opening a userinterface with 3 buttons start,stop,quit and two text fields

        the user is to type in the driver in the driver field, and the driven in the driven field

        make sure the driven is in a group, and the driver can be moved, of course they need to be on scene too

       

        assumes 'naSwitch_trash_do_not_touch' is not on scene and can be used by tool

    """

   

    #create trash folder

    #if one already exists delete it and make new one

    #template it and also hide it

    trashFolderName = 'naSwitch_trash_do_not_touch'   #tool deletes this so make sure its not on scene

    sceneChecker = SceneCheck.SceneCheck()

    isTrashFolderOnScene = sceneChecker.isAnyOnStage([trashFolderName])

   

    if isTrashFolderOnScene == False:  

        #make a null with this name on the scene

        makeTrashFolder(trashFolderName)

        #make the interface

        naSwitchCreateUI(trashFolderName)

    else:

        #delete it then make it

        mc.delete(trashFolderName)  

        #make a null with this name on the scene

        makeTrashFolder(trashFolderName)

        #make the interface

        naSwitchCreateUI(trashFolderName)

   

#end def

def makeTrashFolder(_trashFolderName):

    """

    needs arguments

        string name of trash folder

    """

    trashFolderName = _trashFolderName  

    sceneChecker = SceneCheck.SceneCheck()

    isTrashFolderOnScene = sceneChecker.isAnyOnStage([trashFolderName])

 

    if isTrashFolderOnScene == False:  

        trGrp = Transform.TransformGroup(trashFolderName)

        templateCreator = DisplayEditor.DisplayEditor([trashFolderName])

        templateCreator.setTemplateOn()

        templateCreator.setVisibilityOff()

    else:

        print 'a folder by the name of %s exists, please delete it first' %trashFolderName        

#end def

def naSwitchCreateUI(_trashFolderName):

    """

    needs arguments

        string name of trash folder

       

        assumes a window by name 'naSwitchWindow' is not already made

    """

   

    windowName = 'naSwitchWindow'

    layoutName = 'naSwitchLayout' #all widgets should have different name from window

    windowTitle = 'naSwitch'

    buttonHeight = 30

    buttonWidth = 50

    windowHeight = 220

    windowWidth = 150

    layOutWidth =90

    textGrpHeight = 20

    textGrpWidth = 90

    isWindowOnScene = mc.window(windowName, query=True, exists = True)

     

    #make window buttons and setup tool if no window already made

    if isWindowOnScene == False:    

        trashFolderName = _trashFolderName

        #text before fn obj so fn obj can use texts own functions

        textgroup1 = TextGroup.TextGroup(label='Driver',width=textGrpWidth,height=textGrpHeight)

        textgroup2 = TextGroup.TextGroup(label='Driven',width=textGrpWidth,height=textGrpHeight)

        textGetter = UIGetter.UIGetter(objectList = [textgroup1,textgroup2])#for get the text group data

        layoutObject = Layout.Layout(name=layoutName,width=layOutWidth,offset = 5)

        #window before fn obj so fn obj can use window for possible quitting it

        windowObject = Window.Window(name=windowName,title=windowTitle,width =windowWidth,height=windowHeight)

       

        #create storage for lifetime of tool

        storage = MainStorage.MainStorage()

       

        #create object to be talking with Button class

        uiQuit = UIQuitter.UIQuitter()

        quitCallObject = UISetup.naSwitchQuit()

        fnQuit = FunctionObject.FunctionObject()

        fnQuit.setObject([quitCallObject,uiQuit])              #the name of object instance that has something to quit

        fnQuit.setFunction(['onPress','quitUI'])          #the name of function for quitting in that object

        fnQuit.setFunctionArguments([[storage],[windowObject,trashFolderName]]) #the name of window to quit,put all its parameters into a list

        quitBtn = Button.Button(height=buttonHeight,width=buttonWidth,label='quit',functionFromExternal = True, functionObject = fnQuit)

       

        #create object to be talking with Button class

        stopCallingObject = UISetup.naSwitchStop()

        stopConnObj = FunctionObject.FunctionObject()

        stopConnObj.setObject([textGetter,stopCallingObject])

        stopConnObj.setFunction(['get','onPress'])

        stopConnObj.setFunctionArguments([[],[textGetter,storage,trashFolderName]])

       

        stopBtn = Button.Button(height=buttonHeight,width=buttonWidth,label='stop',functionFromExternal = True, functionObject = stopConnObj)

        #connectingObject.call()

       

        #create object to be talking with Button class

        #make storage for entire lifetime of tool

        startCallingObject = UISetup.naSwitchStart()

        connectingObject1 = FunctionObject.FunctionObject()

        connectingObject1.setObject([textGetter,startCallingObject])

        connectingObject1.setFunction(['get','onPress'])

        connectingObject1.setFunctionArguments([[],[textGetter,storage,trashFolderName]])

       

        startBtn = Button.Button(height=buttonHeight,width=buttonWidth,label='start',functionFromExternal = True, functionObject = connectingObject1)

       

       

        windowObject.make()

        layoutObject.make()

        textgroup1.make()

        textgroup2.make()

        startBtn.make()

        stopBtn.make()

        quitBtn.make()

        windowObject.show()    

    else:

        print 'window by same name already exists, did nothing, may want to quit open window'            

    #end if

       

#end def