Quick Connect Update

So my perfectionism got the better of me.

I rewrote most of the script to catch possible errors that could occur with adding these attributes.
Those errors could include:
1. The driver object already has an attribute of that name
2. The driven object already has that attribute connected to something else
– An attribute can only have one input but can have many outputs.
3. The user didn’t enter all the required information

If any of those are true, nothing happens. Just as you’d want it to.

So here’s the updated code. I ended up using a class as a workaround to using global variables.

Code:

import maya.cmds as cmds

class addAttr(object):
    def __init__(self,driven,driver):
        self.driven = driven
        self.driver = driver

    def setDriven(self):
        print cmds.ls(sl=1,o=1)[0]
        self.driven = cmds.ls(sl=1,o=1)[0]

    def setDriver(self):
        print cmds.ls(sl=1,o=1)[0]
        self.driver = cmds.ls(sl=1,o=1)[0]

    def quickAddAttr(self):
        if cmds.textField("attrName",q=1,text=1) == "" or self.driver == "" or self.driven == "":
            cmds.confirmDialog(t="error",m="Please Enter All Required Information")
            return
        temp = cmds.textField("attrName",q=1,text=1)
        attrName = temp.replace(" ","_")
        if attrName in cmds.listAttr(self.driver,k=1):
            cmds.confirmDialog(t="error",m="Attribute Name Already Exists")
            return

        drivenVal = cmds.optionMenu("drivenVal",q=1,v=1).lower()
        print drivenVal
        if cmds.listConnections(self.driven + "." + drivenVal,scn=1) != None:
            cmds.confirmDialog(t="error",m="\"" + self.driven + "." + drivenVal + "\"" + " Already Has a Connection")
            return

        cmds.addAttr(self.driver,ln=attrName,at="double",dv=0,k=1)

        cmds.connectAttr(self.driver + "." + attrName,str(self.driven) + "." + drivenVal)
        cmds.select(cl=1)

if cmds.window("quickAddAttrWin",exists=True):
    cmds.deleteUI("quickAddAttrWin")

cmds.window("quickAddAttrWin",title="Quick Connect",s=0)

cmds.columnLayout()

cmds.textField("attrName",pht="Attribute Name")

cmds.button(label="Set Driver",command="newAttr.setDriver()")
cmds.button(label="Set Driven",command="newAttr.setDriven()")

cmds.text("Driven Value")
cmds.optionMenu("drivenVal")

cmds.menuItem(label="rX")
cmds.menuItem(label="rY")
cmds.menuItem(label="rZ")

cmds.button(label="Add Attribute",command="newAttr.quickAddAttr()")
newAttr = addAttr("","")
cmds.showWindow("quickAddAttrWin")

cmds.window("quickAddAttrWin",edit=1,wh=(120,130),tlb=1)
Advertisements
Image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s