One checkbox controls other checkboxes in Maya with Python - python

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))

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.

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()

Related

Maya window adding a row dynamically with python

I've been working on this for a while and I can't find any information about adding a row to a window. I seen it done with pyside2 and qt, witch would work but the users are using multiple versions of Maya (2016 = pyside, 2017=pyside2).
I want it like adding a widget in in pyside. I done it where adding a row is a function like add row 1, add row 2, and add row 3 but the script get to long. I need to parent to rowColumnLayout and make that unique in order to delete that later. Also I have to query the textfield in each row. Maybe a for loop that adds a number to the row? I really don't know but this is what I have so far:
from maya import cmds
def row( ):
global fed
global info
item=cmds.optionMenu(mygroup, q=True, sl=True)
if item == 1:
cam=cmds.optionMenu(mygroup, q=True, v=True)
fed=cmds.rowColumnLayout(nc = 1)
cmds.rowLayout(nc=7)
cmds.text(l= cam )
cmds.text(l=u'Frame Range ')
start = cmds.textField('textField3')
cmds.text(l=u' to ')
finish = cmds.textField('textField2')
cmds.button(l=u'render',c='renderTedd()')
cmds.button(l=u'delete',c='deleteRow()')
cmds.setParent (fed)
def deleteRow ():
cmds.deleteUI(fed, layout=True)
if item == 2:
print item
global red
cam1=cmds.optionMenu(mygroup, q=True, v=True)
red = cmds.rowColumnLayout()
cmds.rowLayout(nc=7)
cmds.text(l= cam1 )
cmds.text(l=u'Frame Range ')
start = cmds.textField('textField3')
cmds.text(l=u' to ')
finish = cmds.textField('textField2')
cmds.button(l=u'render',c='renderTedd()')
cmds.button(l=u'delete',c='deleteRow2()')
cmds.setParent (red)
def deleteRow2 ():
cmds.deleteUI(red, control=True)
def cameraInfo():
info=cmds.optionMenu(mygroup, q=True, sl=True)
print info
def deleteRow ():
cmds.deleteUI(fed, control=True)
def getCamera():
layers=pm.ls(type="renderLayer")
for layer in layers:
pm.editRenderLayerGlobals(currentRenderLayer=layer)
cameras=pm.ls(type='camera')
for cam in cameras:
if pm.getAttr(str(cam) + ".renderable"):
relatives=pm.listRelatives(cam, parent=1)
cam=relatives[0]
cmds.menuItem(p=mygroup,label=str (cam) )
window = cmds.window()
cmds.rowColumnLayout(nr=10)
mygroup = cmds.optionMenu( label='Colors', changeCommand='cameraInfo()' )
getCamera()
cmds.button(l=u'create camera',aop=1,c='row ()')
cmds.showWindow( window )
This is totally doable with cmds. The trick is just to structure the code so that the buttons in each row know and can operate on the widgets in that row; once that works you can add rows all day long.
To make it work you want to do two things:
Don't use the string form of callbacks. It's never a good idea, for reasons detailed here
Do use closures to make sure your callbacks are referring to the right widgets. Done right you can do what you want without the overhead of a class.
Basically, this adds up to making a function which generates both the gui items for the row and also generates the callback functions -- the creator function will 'remember' the widgets and the callbacks it creates will have access to the widgets. Here's a minimal example:
def row_test():
window = cmds.window(title='lotsa rows')
column = cmds.columnLayout()
def add_row(cameraname) :
cmds.setParent(column)
this_row = cmds.rowLayout(nc=6, cw6 = (72, 72, 72, 72, 48, 48) )
cmds.text(l= cameraname )
cmds.text(l=u'Frame Range')
start = cmds.intField()
finish = cmds.intField()
# note: buttons always fire a useless
# argument; the _ here just ignores
# that in both of these callback functions
def do_delete(_):
cmds.deleteUI(this_row)
def do_render(_):
startframe = cmds.intField(start, q=True, v=True)
endframe = cmds.intField(finish, q=True, v=True)
print "rendering ", cameraname, "frames", startframe, endframe
cmds.button(l=u'render',c=do_render)
cmds.button(l=u'delete',c=do_delete)
for cam in cmds.ls(type='camera'):
add_row(cam)
cmds.showWindow(window)
row_test()
By defining the callback functions inside of add_row(), they have access to the widgets which get stored as start and finish. Even though start and finish will be created over and over each time the function runs, the values they store are captured by the closures and are still available when you click a button. They also inherit the value of cameraname so the rendering script can get that information as well.
At the risk of self-advertising: if you need to do serious GUI work using cmds you should check out mGui -- a python module that makes working with cmds gui less painful for complex projects.

WxPython BoxSizer inside Listbook

I'm trying to use wx.Listbook to implement a settings window with multiple pages. I made the Listbook just fine, but when I started adding items to a page, I ran into a problem. The items are displayed on top of each other, so it's impossible to see everything I'm trying to show.
self.global_settings_frame = wx.Frame(parent=self, title="Global Settings", name="Global Settings")
self.global_settings_listbook = wx.Listbook(parent=self.global_settings_frame, style=wx.LB_LEFT)
self.global_settings_file_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_file_box = wx.BoxSizer(orient=wx.VERTICAL)
self.show_full_pathname_checkbox = wx.CheckBox(self.global_settings_file_window, label="Show full pathname")
self.global_settings_file_box.Add(self.show_full_pathname_checkbox, proportion=1)
self.global_default_extension = wx.TextCtrl(self.global_settings_file_window)
self.global_settings_file_box.Add(self.global_default_extension, proportion=1)
self.global_settings_token_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_listbook.InsertPage(0, self.global_settings_file_window, "Files")
self.global_settings_listbook.InsertPage(1, self.global_settings_token_window, "Token Defnition")
self.global_settings_frame.Show()
When I comment out the second element, the uncommented part works fine:
self.global_settings_frame = wx.Frame(parent=self, title="Global Settings", name="Global Settings")
self.global_settings_listbook = wx.Listbook(parent=self.global_settings_frame, style=wx.LB_LEFT)
self.global_settings_file_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_file_box = wx.BoxSizer(orient=wx.VERTICAL)
self.show_full_pathname_checkbox = wx.CheckBox(self.global_settings_file_window, label="Show full pathname")
self.global_settings_file_box.Add(self.show_full_pathname_checkbox, proportion=1)
self.global_default_extension = wx.TextCtrl(self.global_settings_file_window)
self.global_settings_file_box.Add(self.global_default_extension, proportion=1)
self.global_settings_token_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_listbook.InsertPage(0, self.global_settings_file_window, "Files")
self.global_settings_listbook.InsertPage(1, self.global_settings_token_window, "Token Defnition")
self.global_settings_frame.Show()
But I think the BoxSizer isn't working right because when I comment out the previous line (the one adding the CheckBox to the BoxSizer), the display is the same.
I've tried using separate panels for each element and then putting those panels in the BoxSizer, but that also didn't work (I can show you what that looks like if necessary). So it looks like I'm not using the BoxSizer correctly, but I don't understand how I am supposed to use it in this case. What I want is a page of a ListBook that contains a CheckBox and a TextCtrl (for single line text entry). Can you help?
As I can see, you never assign any sizer to your page(s).
You should just do it :
......
self.global_settings_file_box.Add(self.global_default_extension, proportion=1)
self.global_settings_file_window.SetSizer(self.global_settings_file_box)
self.global_settings_token_window = wx.Panel(parent=self.global_settings_listbook)
......
Regards
Xav'

Display hierarchy of selected object only

I am creating a UI where the user selects an object,
the UI will display its hierarchy of the selected
object.
It is somewhat similar to Outliner but I am unable to
find any documentation/ similar results to what I am
trying to obtain. And btw, I am coding using python...
Even so, is this even possible to do it in the first
place?
Allow me to provide a simple example below:
Say if I selects testCtrl, it will only displays
testCtrl, loc and jnt without showing the Parent (Grp 01)
Eg. Grp 01 --> testCtrl --> loc --> jnt
import maya.cmds as cmds
def customOutliner():
if cmds.ls( sl=True ):
# Create the window/UI for the custom Oultiner
newOutliner = cmds.window(title="Outliner (Custom)", iconName="Outliner*", widthHeight=(250,100))
frame = cmds.frameLayout(labelVisible = False)
customOutliner = cmds.outlinerEditor()
# Create the selection connection network; Selects the active selection
inputList = cmds.selectionConnection( activeList=True )
fromEditor = cmds.selectionConnection()
cmds.outlinerEditor( customOutliner, edit=True, mainListConnection=inputList )
cmds.outlinerEditor( customOutliner, edit=True, selectionConnection=fromEditor )
cmds.showWindow( newOutliner )
else:
cmds.warning('Nothing is selected. Custom Outliner will not be created.')
Make the window:
You want to use the treeView command (documentation) for this. I'm placing it in a formLayout for convenience.
from maya import cmds
from collections import defaultdict
window = cmds.window()
layout = cmds.formLayout()
control = cmds.treeView(parent=layout)
cmds.formLayout(layout, e=True, attachForm=[(control,'top', 2),
(control,'left', 2),
(control,'bottom', 2),
(control,'right', 2)])
cmds.showWindow(window)
Populate the tree view:
For this, we'll use a recursive function so you can build up the hierarchy with nested listRelatives calls (documentation). Start with the result of old faithful ls -sl:
def populateTreeView(control, parent, parentname, counter):
# list all the children of the parent node
children = cmds.listRelatives(parent, children=True, path=True) or []
# loop over the children
for child in children:
# childname is the string after the last '|'
childname = child.rsplit('|')[-1]
# increment the number of spaces
counter[childname] += 1
# make a new string with counter spaces following the name
childname = '{0} {1}'.format(childname, ' '*counter[childname])
# create the leaf in the treeView, named childname, parent parentname
cmds.treeView(control, e=True, addItem=(childname, parentname))
# call this function again, with child as the parent. recursion!
populateTreeView(control, child, childname, counter)
# find the selected object
selection = cmds.ls(sl=True)[0]
# create the root node in the treeView
cmds.treeView(control, e=True, addItem=(selection, ''), hideButtons=True)
# enter the recursive function
populateTreeView(control, selection, '', defaultdict(int))
Comparison of window to outliner.
I've replaced the spaces with X so you can see what's happening. Running this code will use spaces though:
You'll want to read up on the documentation to improve on this, but this should be a great starting point. If you want a live connection to the selection, make a scriptJob to track that, and be sure to clear the treeView before repopulating.

Group Gtk.RadioToolButtons?

I've been trying for days to find a way to group RadioToolButtons in pygobject without success. There is no *.RadioToolButton.join_group(*) method like RadioButtons.
Here is what I've been trying:
## Toolbar
self.mainWindow.mainBox.mainToolbar = Gtk.Toolbar()
self.mainWindow.mainBox.mainToolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
self.mainWindow.mainBox.mainToolbar.set_style(Gtk.ToolbarStyle.BOTH)
self.mainWindow.mainBox.mainToolbar.radioGroup = list() # *.radioGroup = [] Does not work either.
## Left toolbar separator
self.mainWindow.mainBox.mainToolbar.leftSeparator = Gtk.SeparatorToolItem(draw = False)
self.mainWindow.mainBox.mainToolbar.leftSeparator.set_expand(True)
## Overview toggle button
self.mainWindow.mainBox.mainToolbar.overviewRadio = Gtk.RadioToolButton(Gtk.STOCK_HOME)
self.mainWindow.mainBox.mainToolbar.overviewRadio.set_group(self.mainWindow.mainBox.mainToolbar.radioGroup)
self.mainWindow.mainBox.mainToolbar.overviewRadio.set_is_important(True)
self.mainWindow.mainBox.mainToolbar.overviewRadio.set_label("Overview")
self.mainWindow.mainBox.mainToolbar.overviewRadio.connect("clicked", self.on_overviewRadio_clicked)
self.mainWindow.mainBox.mainToolbar.overviewRadio.set_border_width(4)
## Basic settings toggle button
self.mainWindow.mainBox.mainToolbar.basicRadio = RadioToolButton(Gtk.STOCK_PROPERTIES)
self.mainWindow.mainBox.mainToolbar.basicRadio.set_group(self.mainWindow.mainBox.mainToolbar.radioGroup)
self.mainWindow.mainBox.mainToolbar.basicRadio.set_is_important(True)
self.mainWindow.mainBox.mainToolbar.basicRadio.set_label("Basic")
self.mainWindow.mainBox.mainToolbar.basicRadio.connect("clicked", self.on_basicRadio_clicked)
self.mainWindow.mainBox.mainToolbar.basicRadio.set_border_width(4)
## Right toolbar separator
self.mainWindow.mainBox.mainToolbar.rightSeparator = Gtk.SeparatorToolItem(
draw = False)
self.mainWindow.mainBox.mainToolbar.rightSeparator.set_expand(True)
(Not all of my code - *.show_all() is not the issue)
Here is what I get:
What am I doing wrong? How can I group these two buttons?
Create the second radio button so it's in the first radio button's group using:
Gtk.RadioToolButton.new_with_stock_from_widget(first_button, Gtk.STOCK_PROPERTIES)
PS. Looks like the UI task you are trying to accomplish might be better done with a Notebook?

Dynamic Widget creation based of configparser file

I am trying a piece of python based tkinter code with following objective:
(Please go through the objective, then I will take an example to explain what exactly i require and then in the end will post the script I have written)
Reads from a config file, which is implemented using configparser module.
Based on options read from this file it automatically generates widget.
These widgets are restricted to only labels and entry box as of now.
Every entry box is associated with a variable. It is hence needed to generate a variable
automatically whenever a entry box is declared.
Now when the user enters any value in the entry box, and presses calculate button a list is
generated with combination of values entered by user( in a specific format).
Example:
Let the configparser file has following content:
[widget]
label = ani_label,sham_label
entry = ani,sham
The list generated for this case will be like this:
out_list = ['-ani','< ani >','-sham','< sham >']
< ani > means value stored in ani variable
And below is the code that i have tried.
from Tkinter import *
from Tkinter import Tk
import Tkinter as tk
import ttk
import ConfigParser
import sys
############ Initialize ###############################
parser_read = ConfigParser.ConfigParser()
parser_read.read('option_read.config')
config_list = {}
config_list['label'] = parser_read.get('widget','label').split(',')
config_list['entry'] = parser_read.get('widget','entry').split(',')
######
def calculate():
#Will include the list generation part
pass
#######
root = Tk()
root.title("NRUNTEST GUI VERSION 1")
#
menuframe = ttk.Frame(root)
menuframe.grid(column=0,row=0)
menuframe.columnconfigure(0,weight=1)
menuframe.rowconfigure(0,weight=1)
#
mainframe_label = ttk.Frame(root)
mainframe_label.grid(column=1,row=0)
mainframe_label.columnconfigure(0,weight=1)
mainframe_label.rowconfigure(0,weight=1)
#
mainframe_entry = ttk.Frame(root)
mainframe_entry.grid(column=2,row=0)
mainframe_entry.columnconfigure(0,weight=1)
mainframe_entry.rowconfigure(0,weight=1)
#
general_label= Label(menuframe,text="Please Enter the Values Below").grid(column=1,row=0,sticky=(E))
compiler_label= ttk.Label(menuframe,text="Compiler")
compiler_label.grid(column=1,row=1,sticky=W)
#
calculate_button = ttk.Button(menuframe, text="Calculate", command=calculate).grid(column=1,row=2,sticky=(W,E))
#Automatic Widget declaration ###
for x in config_list['label']:
x = ttk.Label(mainframe_label,text=x).grid()
for x in config_list['entry']:
#print x
var = lambda: sys.stdout.write(x)
x = ttk.Entry(mainframe_entry,textvariable = x).grid()
root.mainloop()
The content of option_read.config is
[widget]
label : animesh_label,sharma
entry : animesh_entry,sharma
STATUS:
I can create the required widgets automatically. But I am not able to create the variables dynamically to store the entry box values.
Once the variable has been calculated, I can write the calculate function on my own.
Please advice how i can proceed.
If you have any better way to meet my requirements, please do suggest.
Also do ping me if you require any more inputs or my query is not clear.
The easiest way to do this, IMO, is to use a dict to store the references to the dynamically created variables. You could use the label as the key. For example:
vars = {}
for x in config_list['entry']:
vars[x] = StringVar()
entry = ttk.Entry(mainframe_entry, textvariable=vars[x])
entry.grid()
By the way... are you aware that if you do something like x=ttk.Entry(...).grid(...), x does not contain a reference to the widget? It contains the result of the call to grid, which is None.

Categories

Resources