0

I wanted to create an set driven key relationship in such a way that if one object visibility is set to ON all remaining objects in the set should turn off .

For example there are 5 switches in my scene where I need the other four switches to turn OFF when I activate one switch.

How can I code such thing in Python?

Thank you.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

2 Answers2

1

Here's a more procedural way to go about it with set driven keys, so you could have as many sets with as many objects as you want.

This will create a single attribute that will drive the visibility of all members in different sets. As you change the driver's value it will only display one set.

import maya.cmds as cmds


driver = "pSphere1"  # Define the object that will hold the switch attribute.
set_names = ["set1", "set2", "set3", "set4"]  # Define set names to effect.

cmds.addAttr(driver, ln="switch", at="long", keyable=True, min=0, max=len(set_names) - 1)  # Create switch attribute on driver.

for i, set_name in enumerate(set_names):
    set_members = cmds.sets(set_name, q=True) or []  # Collect all of the set's members.

    for member in set_members:
        for j in range(len(set_names)):
            cmds.setDrivenKeyframe(member, at="visibility", cd="{}.switch".format(driver), dv=j, v=i == j)  # Set an sdk on each member that will set its visibility

Example of switching driver's value

Green Cell
  • 4,677
  • 2
  • 18
  • 49
0

Use this code as a starting point:

enter image description here

import maya.cmds as cmds

def on1():
    cmds.setAttr('pSphere1.visibility', 1)
def on2():
    cmds.setAttr('pSphere2.visibility', 1)
def on3():
    cmds.setAttr('pSphere3.visibility', 1)
def on4():
    cmds.setAttr('pSphere4.visibility', 1)
def on5():
    cmds.setAttr('pSphere5.visibility', 1)

def off1(): 
    cmds.setAttr('pSphere1.visibility', 0)
def off2():
    cmds.setAttr('pSphere2.visibility', 0)
def off3():
    cmds.setAttr('pSphere3.visibility', 0)
def off4():
    cmds.setAttr('pSphere4.visibility', 0)
def off5():
    cmds.setAttr('pSphere5.visibility', 0)

cmds.window(width=100)
cmds.columnLayout(adjustableColumn=True)
cmds.radioCollection()
rb01 = cmds.radioButton(label='1', onc='on1()', ofc='off1(), off2(), off3(), off4(), off5()', sl=True)
rb02 = cmds.radioButton(label='2', onc='on2()', ofc='off1(), off2(), off3(), off4(), off5()')
rb03 = cmds.radioButton(label='3', onc='on3()', ofc='off1(), off2(), off3(), off4(), off5()')
rb04 = cmds.radioButton(label='4', onc='on4()', ofc='off1(), off2(), off3(), off4(), off5()')
rb05 = cmds.radioButton(label='5', onc='on5()', ofc='off1(), off2(), off3(), off4(), off5()')
cmds.showWindow()

Hope this helps.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220