Mel code to Python checkboxUI

Hi All,

I need to convert some UI related mel codes to Python. Any help would be appreciated.

The code is below.

checkBox -l “Name " -v 1 -w 65 -h 20 -cc " textField -e -en (checkBox -q -v artistSwitch)artist” artistSwitch;
textField -tx getenv "USERNAME" -w 340 -ebg 0 -enable 1 artist;

Both the lines are interrelated where the -cc flag is querying the textfield.

Hi @plabon, welcome back! Lets see if we can break this down a little… :slight_smile:

checkBox -l “Name " -v 1 -w 65 -h 20 -cc

This the checkbox, -cc is the change command which is enacting:

textField -e -en ( checkBox -q -v artistSwitch )artist” artistSwitch;

Which its enabled state is being defined by the value recoved from:

checkBox -q -v artistSwitch

This seems to make sense in the second part:

textField -tx getenv "USERNAME" -w 340 -ebg 0 -enable 1 artist;

A text field text is set using the environment variable USERNAME, with a width of 340, no background and enabled state set to True,

Ok so the artistSwitch checkbox state triggers a textfield artist which it’s enabled state is set based on the state of the artistSwitch itself. So a check box enables the state of a text field based on its state. This textfield’s text is the enviroment variable USERNAME.

So layman speak - you enable/disable a checkbox which pops up a textfield with a username thats editable or not based on the state of the checkbox.

def change_command():

    def _check_state():
        return cmds.checkBox("artistSwitch", query=True, value=True)

    cmds.textField("artist", edit=True, enabled=_check_state())

cmds.textField("artist", text=cmds.getenv("USERNAME"), width=340, enableBackground=False, enable=True)

cmds.checkbox("artistSwitch", label="Name", value=True, width=65, height=20, changeCommand=change_command)

Thanks Chalk,

So for two lines of mel we need to write two functions in Python.

What if there are multiple checkboxes and text fields like this…as for example:

checkBox -l “Name " -v 1 -w 65 -h 20 -cc " textField -e -en (checkBox -q -v artistSwitch)artist” artistSwitch;
textField -tx getenv "USERNAME" -w 340 -ebg 0 -enable 1 artist;

checkBox -l “Scene " -v 1 -w 65 -h 20 -cc " textField -e -en (checkBox -q -v sceneSwitch)sceneId” sceneSwitch ;
textField -tx file -q -namespace -w 340 -ebg 0 -enable 1 sceneId ;

checkBox -l “Date " -v 1 -w 65 -h 20 -cc " textField -e -en (checkBox -q -v dateSwitch)date” dateSwitch;
textField -tx date -f "DD.MM.YY _ hh:mm " -w 340 -ebg 0 -enable 1 date ;

checkBox -l “Camera " -v 1 -w 65 -h 20 -cc " textField -e -en (checkBox -q -v cameraSwitch)camName” cameraSwitch;
textField -tx $camName -w 340 -ebg 0 camName;

Is there any shorter method to compile together?

It looks like its doing the same thing each time - so you could do something like list/dictionary comprehension and loop through them.

{‘date’: ‘dateSwitch’, ‘camera’:’cameraSwitch’, ...}

The issue is the text fields values are derived in different ways so that might make it more tricky. Id break it into two parts:

  1. Set all the text fields values.
  2. Loop through each text field and its corresponding checkbox (example above) and set the enabled state. basically same function in my last post but your passing a textfield and checkbox arg. Pull out the textfield value setting into another block.