Python Noob question - passing the Var from one Def to another one

I am learning Python and stuck with the task of passing the Var from one Def to another one inside a Class.
This is a Maya script which duplicates selected objects.
For other parts of the script i need to store both the list of initially selected objects as well as list of duplicated objects.
Would be very thankful for the help. Not sure what I’m doing wrong.

When executing this i get en error - Error: # Error: ValueError: No object matches name: *_Gradient_Source #

which makes me feel that self.initial_selection was not passed from the first Def.
(first Def seems to be running without problems)

I have also tried putting everything inside one Def and it works fine,
which again makes me feel that the problem is with passing the Variable from first Def to the Second one.

from maya import cmds

class gradient():

    def __init__(self):
        self.initial_selection = []
        self.the_selection = []


    def selection(self):

        self.initial_selection = cmds.ls(selection=True)
        # Check if something is selected, if not - error message
        if not self.initial_selection:
            nothing_selected_error_message = 'Nothing is selected. Select an object!'
            raise RuntimeError(nothing_selected_error_message)


    def duplication(self):

        # Duplicate and rename with _Gradient_Source
        for each_object in self.initial_selection:
            cmds.duplicate(each_object, name='%s' % each_object + '_Gradient_Source')

        # Hide original objects
        cmds.select(self.initial_selection)
        cmds.hide(self.initial_selection)

        # Select duplicates with _Gradient_Source name
        cmds.select('*_Gradient_Source')
        self.the_selection = cmds.ls(selection=True)
class gradient():
    
    def __init__(self):
        self.initial_selection = []
        self.the_selection = []
    
    
    def selection(self):
    
        self.initial_selection = cmds.ls(selection=True)
        # Check if something is selected, if not - error message
        if not self.initial_selection:
            nothing_selected_error_message = 'Nothing is selected. Select an object!'
            raise RuntimeError(nothing_selected_error_message)
    
    
    def duplication(self):
    
        # Duplicate and rename with _Gradient_Source
        for each_object in self.initial_selection:
            cmds.duplicate(each_object, name='%s' % each_object + '_Gradient_Source')
    
        # Hide original objects
        cmds.select(self.initial_selection)
        cmds.hide(self.initial_selection)
    
        # Select duplicates with _Gradient_Source name
        cmds.select('*_Gradient_Source')
        self.the_selection = cmds.ls(selection=True)
        
g = gradient()
g.selection()
print(g.initial_selection, g.the_selection)
# ([u'pCube1', u'pSphere1'], [])
g.duplication()
print(g.initial_selection, g.the_selection)
# ([u'pCube1', u'pSphere1'], [u'pCube1_Gradient_Source', u'pSphere1_Gradient_Source'])

So it appears to work for me. Unless I am misunderstanding the question.

1 Like

Hi Iurii,

Maybe Robert’s reply will help you. But I have 2 points to add:

  1. “def” is short for “defining” a function. It is more clear if you say “functions” instead of “defs”.

  2. The code you posted doesn’t do anything on its own because you just posted the class. Make sure to post the whole exact code you are running, and then it is easier for others to check for any bugs or typos in your code.

2 Likes

since cmds.duplicate() returns a list of the new items, you should just store that directly rather than deal with selecting and listing the selection

# Duplicate and rename with _Gradient_Source
for each_object in self.initial_selection:
    self.the_selection.extend(cmds.duplicate(each_object, name='%s' % each_object + '_Gradient_Source'))
1 Like

Thank you so much Robert, your post got me thinking and i realized i was not running the python file in Maya correctly.
So i was trying to run it using:

from GradientBaker import GradientBaker_Logic
reload(GradientBaker_Logic)

GradientBaker.GradientBaker_Logic.gradient().selection()
GradientBaker.GradientBaker_Logic.gradient().duplication()

which (my guess) was running it somehow incorrectly.

I tried to do something similar to your example and it worked perfectly! Thanks again!

from GradientBaker import GradientBaker_Logic
reload(GradientBaker_Logic)

g = GradientBaker.GradientBaker_Logic.gradient()
g.selection()
g.duplication()

Clesage, thanks, your comment is absolutely true and actually pointing to the source of the problem,
sorry for the confusion, i am still learning and got lost in the terminology (rather poor prior codding experience)

Leocov, oh, that is great! did not know that! Thanks! This is something that i need to adjust going forward
and adding new functions to this script.

Guys, thanks again, this is an amazing forum!

the reason this did not work is because you called your method on different instances of the class and duplication() could only be called after selection(), you could add some error checking in duplication() to make sure self.initial_selection is not empty.

3 Likes

Cool, thanks! I didn’t realize that was two separate instances