Passing any integer to a module always returns 0

Passing any integer to a module always returns 0

Khenley8
Participant Participant
509 Views
2 Replies
Message 1 of 3

Passing any integer to a module always returns 0

Khenley8
Participant
Participant

Hi there, I am trying to split code into modules, and make them all accessible from a single GUI.
Ran into an issue where I need to pass user input from the GUI to an imported module class.

 

The problem is that the result is always 0 when the module runs, I cannot work out why this would be.
Here is a recreation highlighting the problem:

--------------------------------------------------
class Example
    def __init__(self, user_input):
        self.user_input = user_input

    def function(self, *args):
        print('The example class dictates that ' + str(self.user_input) + ' was your number.')

---------------------------------------------------

import maya.cmds as mc
import example_class

 

class Window(object):
    def __init__(self):
        self.window = 'Window'
        self.title = 'Title'
        self.size = (400, 400)

        if mc.window(self.window, ex=True):
        mc.deleteUI(self.window, window=True)

        self.window = mc.window(self.window, t=self.title, wh=self.size)
        mc.columnLayout(adj=True)

        self.number = mc.intSliderGrp(l='Pick a Number:', adj=True, min=1, max=100, field=True)
        result = mc.intSliderGrp(self.number, q=True, v=True)

        my_example_class = example_class.Example(result)

        mc.button(l='Print number from this class', c=self.print_result)
        mc.button(l='Print number from example class', c=my_example_class.function)

        mc.showWindow()

    def print_result(self, *args):
        class_result = mc.intSliderGrp(self.number, q=True, v=True)
        print('This class dictates that ' + str(class_result) + ' was your number.')

my_window = Window()
Window()

---------------------------------------------------

 

If I move the slider to 63 for example, this is the print result from both functions:

 

"This class dictates that 63 was your number."
"The example class dictates that 0 was your number."

 

No doubt after this issue is (hopefully) solved I will likely run into another one straight away but this is what the learning process is all about. 🙂

 

0 Likes
Accepted solutions (1)
510 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

your problem is, that you initialise the example class only once when creating the window. This way the default value 0 gets stored as user_input.

 

With Maya UI's in my experience it is best to call functions/initialise classes from external modules in a local dummy function that gets called when needed after UI creation. Like this:

 

import maya.cmds as mc
import example_class

 

class Window(object):
    def __init__(self):
        self.window = 'Window'
        self.title = 'Title'
        self.size = (400, 400)

        if mc.window(self.window, ex=True):
            mc.deleteUI(self.window, window=True)

        self.window = mc.window(self.window, t=self.title, wh=self.size)
        mc.columnLayout(adj=True)

        self.number = mc.intSliderGrp(l='Pick a Number:', adj=True, min=1, max=100, field=True)
        


        mc.button(l='Print number from this class', c=self.print_result)
        mc.button(l='Print number from example class', c= self.print_external)

        mc.showWindow()

    def print_result(self, *args):
        class_result = mc.intSliderGrp(self.number, q=True, v=True)
        print('This class dictates that ' + str(class_result) + ' was your number.')
    
    def print_external(self, *args):
        result = mc.intSliderGrp(self.number, q=True, v=True)
        my_example_class = example_class.Example(result)
        my_example_class.function()
        

        

my_window = Window()
Window()

 

 

So when your button gets clicked, it calls the function print_external. Inside print external, the Example class gets initialised with the current value of the slider and then the function that the value is called.

 

I hope it helps!

 

 

Message 3 of 3

Khenley8
Participant
Participant

Apologies for the late response on this one, thanks a million!
This is just what I needed, thanks to you I was able to finish putting my rigging toolkit together.