Eunice Panduro
Eunice Panduro

Reputation: 25

Calling a variable outside a class

How can I get the value of an intSliderGrp defined inside a class and use it on a function outside said class?

I'm trying to get my main UI all inside a class so I can just call it inside maya, but I need the value set on a slider to modify a function outside the classUI:

import maya.cmds as cmds

def setSubdivision(*args):
    obj = cmds.ls(sl = True)
    asubd = cmds.intSliderGrp(sliderSet, query = True , value = True)
    for i in obj:
        cmds.setAttr('%s.aiSubdivIterations' %i, int(asubd))
        
class subdivisionUI():
    
    windowName = "ArnoldSubdivisionWindow"

    def show(self):

        if cmds.window(self.windowName, query =  True, exists =  True, width = 150):
            cmds.deleteUI(self.windowName)

        cmds.window(self.windowName)
        
        mainColumn = cmds.columnLayout(adjustableColumn = True)
        
        cmds.text(l='Set subdivisions to selected objects', al = 'center')

        column2 = cmds.rowLayout(numberOfColumns = 2, adjustableColumn=2, columnAlign=(1, 'right'))
        sliderSet = cmds.intSliderGrp(l = "Subdivisions", s =1, min = 0, max = 20, field = True)
        b = cmds.button(l = 'Set')
        cmds.button(b, e = True , command = setSubdivision, width = 50 ) 

        

        cmds.showWindow()
        
        
subdivisionUI().show()

It's my first time using classes so I'm still trying to understand how they work and the proper use.

Upvotes: 0

Views: 70

Answers (1)

haggi krey
haggi krey

Reputation: 1978

At the moment you do not use the advantage of classes for UI creation in Maya. The big advantage is that you can keep everything encapsulated in this class without the need of any external functions or global variables. If you try this approach your problem disappears:

import maya.cmds as cmds

        
class subdivisionUI():
    
    windowName = "ArnoldSubdivisionWindow"
    
    def __init__(self):
        if cmds.window(self.windowName, query =  True, exists =  True, width = 150):
            cmds.deleteUI(self.windowName)

        self.window = cmds.window(self.windowName)
        mainColumn = cmds.columnLayout(adjustableColumn = True)
        cmds.text(l='Set subdivisions to selected objects', al = 'center')

        column2 = cmds.rowLayout(numberOfColumns = 2, adjustableColumn=2, columnAlign=(1, 'right'))
        self.sliderSet = cmds.intSliderGrp(l = "Subdivisions", s =1, min = 0, max = 20, field = True)
        b = cmds.button(l = 'Set')
        cmds.button(b, e = True , command = self.setSubdivision, width = 50 ) 
        
    def setSubdivision(self, *args):
        obj = cmds.ls(sl = True)
        asubd = cmds.intSliderGrp(self.sliderSet, query = True , value = True)
        for i in obj:
            cmds.setAttr('%s.aiSubdivIterations' %i, int(asubd))

    def show(self):
        cmds.showWindow(self.window)
        
        
subdivisionUI().show()

Upvotes: 2

Related Questions