Problems with simple Python Interface


#1

I’m struggling to reference UI controls outside of the init any clues how I get around this scope issue??

Thanks


import maya.cmds as cmds
class cameraToolsGUI():
	def changePreset(self, arg):
		self.spn_filmgateWidth.value = 100 #this line doesn't work.....
	

	# class constructor - builds window, but no show yet
	def __init__(self):
		
		self.WIN = cmds.window(widthHeight=(420, 100), title = "Camera Tools")
		#cmds.columnLayout(adj=1)
		scrollLayout = cmds.scrollLayout(
				horizontalScrollBarThickness=16,
				verticalScrollBarThickness=16)
		cmds.rowColumnLayout( numberOfColumns=1 )
		
		cmds.optionMenu('ddl_cameraPresets', label='Camera Database', changeCommand=self.changePreset) 
		cmds.floatSliderGrp('spn_filmgateWidth', label='Film Width', field=True, minValue=0, maxValue=100, fieldMinValue=0, fieldMaxValue=50000, value=0, precision=2)
		cmds.showWindow()
		
	def show(self):
		cmds.showWindow(self.WIN)


#2

You only can access members if you created them somewhere.
In pseudocode:

class makeUI():
    def __init__(self):
        self.floatSlider = floatSliderGroup(...)

    def setValue(self, value):
        self.floatSlider.value.set(value)

You created a floatSliderGrp, but you did not assign it to class member. But even if you would have done it, it woudn’t work anyway (at least not this way) because ui functions of the maya.cmds module returns a string, not a UI object. You can solve this either by using the string:

class makeUI():
    def __init__(self):
        self.floatSlider = cmds.floatSliderGroup(...) # returns a string

    def setValue(self, value):
        cmds.floatSliderGrp(self.floatSlider, edit=True, value=value) # use string to identify the UI element

Or you use PyMel. PyMel returns objects and you can use it the pythonic object oriented way.


import pymel.core as pm
class makeUI():
    def __init__(self):
        self.floatSlider = pm.floatSliderGroup(...) # returns an object

    def setValue(self, value):
        self.floatSlider.value=value

Please remember, this is pseudocode, it may differ a bit in real life.


#3

Thank you very much for your pseudo answers… Not quite working I’m afraid!


#4

Moving to Pyside which hopefully should negate these problems.

Thanks


#5

PySide wont help if you dont understand the way class members and ui objects work. Haggi’s answer is pretty much what you needed. I’d go one step further and suggest that pymel is a better choice when using maya’s native ui layouts, and is also more similar to the way PySide works than maya.cmds is…

Here’s your code rewritten (and working) with that in mind…

import pymel.core as pm

class cameraToolsGUI():
    def changePreset(self, arg):
        self.spn_filmgateWidth.setValue(float(arg))
		
    def __init__(self):
		
        self.WIN = pm.window(widthHeight=(420, 100), title = "Camera Tools")
        scrollLayout = pm.scrollLayout(horizontalScrollBarThickness=16,verticalScrollBarThickness=16)
        pm.rowColumnLayout( numberOfColumns=1 )
		
        self.ddl_cameraPresets = pm.optionMenu('ddl_cameraPresets', label='Camera Database', changeCommand=self.changePreset )
        for w in (0, 10, 50, 100):
            pm.menuItem(label=`w`, p=self.ddl_cameraPresets)
        self.spn_filmgateWidth = pm.floatSliderGrp('spn_filmgateWidth', label='Film Width', field=True, minValue=0, maxValue=100, fieldMinValue=0, fieldMaxValue=50000, value=0, precision=2)
		
        self.WIN.show()
		

ui = cameraToolsGUI()

The important thing to notice is that the layout commands return the object class that they create. You can then access methods of that class to do the things you need.
For example … try the following one line at a time so you can see the output at each step…

floatSliderGrp = pm.floatSliderGrp() # returns the FloatSliderGrp class

dir(floatSliderGrp) # lists the methods of the FloatSliderGrp, Notice one called setValue

floatSliderGrp .setValue(67) # set the value

David