0

I am making a UI in Maya with Python. My goal is to make the master checkbox be able to control other checkboxes so that when the master checkbox is checked, all the other checkboxes are checked and vice versa. The function is as same as the Game Exporter/Animation Clips section in Maya. You can add multiple clips and use the master checkbox to control all clips. Does anyone know how to do that with Python?

The example (image) of what I want to achieve:

I've tried to use changeCommand, onCommand, and offCommand to edit other checkboxes, but it didn't work. The code is like this (clipOn is multiple checkboxes, and masterCheckbox is the master checkbox that can control others):

clipOn = cmds.checkBox(label = '')
masterCheckbox = cmds.checkBox(label = '', onCommand = lambda x: cmds.checkBox(clipOn, editable = True, onCommand = True))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ZTZML
  • 11
  • 1
  • 5
  • if you get error message then show it in question. – furas Aug 05 '19 at 23:09
  • You need to update a list with your "+" function. Each time the widget is added, you append the checkbox. On the cc flag command of the master you simply parse this list to copy the value of the master – DrWeeny Aug 11 '19 at 14:47

2 Answers2

0

It's late and this is untested, but something like the following should work. Maya's 'onCommand'/ 'offCommand' parameters require a string or command to be returned. I used enumerate with list comprehension so that it should return a string for each value as it loops through and edits the checkboxes (rather than a single list). Of course you can always just call a regular function, which is arguably an easier and more straightforward approach.

master = cmds.checkBox(label='All')
check1 = cmds.checkBox(label='One')
check2 = cmds.checkBox(label='Two')
check3 = cmds.checkBox(label='Three')

checkAll = lambda state: [cmds.checkBox(c, edit=1, value=state) for i, c in enumerate([check1,check2,check3])][i]
cmds.checkBox(master, edit=1, onCommand=checkAll(True), offCommand=checkAll(False))

I would have thought:

checkAll = lambda state: cmds.checkBox([check1, check2, check3], edit=1, value=state)

would have worked. But it appears the checkBox command doesn't allow you to pass in multiple objects. By the way, if you want to edit the 'onCommand' cmds.checkBox(clipOn, editable = True, onCommand = True)), you would pass a new command in not a bool value. Lastly, there is a 'checkBoxGrp' MEL command that allows creating and editing groups of checkboxes, however ultimately, I don't think it does anything different then what you can accomplish with the standard 'checkBox' command.

m3trik
  • 333
  • 2
  • 13
0

I would advise something like this :

from functools import partial
clipList = []

def addClip(*args):
    newClip = cmds.checkBox(label='clip', p=mainLayout)
    global clipList
    clipList.append(newClip)

def setAllClips(ckb_master, *args):
    value = cmds.checkBox(ckb_master, q=True, v=True)
    if clipList:
        for clip in clipList:
            cmds.checkBox(clip, e=True, v=value)

mainLayout = cmds.columnLayout()
master = cmds.checkBox(label='All', p=mainLayout, cc=partial(setAllClips, master))
addClip()
DrWeeny
  • 2,487
  • 1
  • 14
  • 17