Store created elements created from function/button

74 views
Skip to first unread message

Simen Chris

unread,
Oct 15, 2013, 11:12:37 AM10/15/13
to python_in...@googlegroups.com

Hey guys, I'm creating a rigging script in PyMel, in my UI I have a button that calls a function that creates a template leg (20 different objects). The issue I'm having is that when I'm finished with positioning the template-objects I'm gonna need to work with them in the net function, so I click another button "Build Rig", what I'm doing to get the template-objects is to use the same (hardcoded) names as I did when the were created, one problem with that is that if the names got changed because of another object with the same name, it won't find them.

I'm using classes, and I know that if I didn't call the function from a button I could just return the objects in the template-function, but as I'm calling the function from a button I don't know if it's even possible to store the return statements from a function. I could use a textScrollList to store the names in, but if I do I can't call the function outside of the UI, also I think it's a bit messy.

Another thing, as I'm using PyMel, I know that each object I create is created within the PyMel class, which means that I'm normally able to rename the object through the instance, but I don't know how to take use of that when calling the function (again) from a buttom.


Any tips would be greatly appreciated! Cheers

dgovil

unread,
Oct 15, 2013, 5:09:03 PM10/15/13
to python_in...@googlegroups.com
If you're using a class, you can have a instance level dict ie self.foo = {}
and then whatever method your button calls can update that dictionary which can be used by any other method of the class.

Simen Chris

unread,
Oct 17, 2013, 6:03:32 AM10/17/13
to python_in...@googlegroups.com
Thanks for your response, do you mean that I create a class that is called "globally" before the UI, so I have an instance of that running before the button is pushed? And that way I can write to/get from the instance of that class? Sorry I'm still in the process of learning how to work with classes :)

Justin Israel

unread,
Oct 17, 2013, 6:15:33 AM10/17/13
to python_in...@googlegroups.com
I think what dgovil is suggesting, is to make use of the internal state of your class, which is being used to represent your UI and contains your button callbacks:

class MyInterface(object):

    def __init__(self):
        self.__currentRig = {}

    def button1_clicked(self):
        results = getResults()
        self.__currentRig['first_part'] = results

    def button2_clicked(self):
        results = getOtherResults()
        previewStuff = self.__currentRig['first_part']
        newResults = doMore(results, newResults)
        self.__currentRig['second_part'] = newResults

    def newRig(self):
        self.__currentRig = {}



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/6ce314d3-8c15-47bc-bb81-3a15e496452f%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Simen Chris

unread,
Oct 17, 2013, 9:53:45 AM10/17/13
to python_in...@googlegroups.com
Thanks Justin, I'm sorry but I don't get it :( Internal state of a class, that sounds super advanced, have no idea what that means :S
 
I've uploaded the code I'm working on btw: http://pastebin.com/nz9EDTmM
 
 
import pymel.core as pm
class TestRigUI(object):
    def __init__(self):
        self.winName = 'testRiggerWin'
        self.winTitle = 'Test Rigger'
    #Delete window if it exists
    def deleteExisting(self):
        if pm.window(self.winName, exists=True):
            pm.deleteUI(self.winName, window=True)
    #Set preferences of window
    def setWindowPrefs(self, **prefs):
        if pm.windowPref(self.winName, exists=False):
            pm.windowPref(self.winName)
        pm.windowPref(self.winName, edit=True, **prefs)
    #Build the elements in the window
    def populateGui(self):
        #Build the tab for building the template
        with pm.frameLayout(l='Build Template', mw=5, mh=5, bs='out') as frame:
            with pm.columnLayout(adjustableColumn=True):
                #Create the button to build the template
                pm.button(label='Build The Template', command='TestRig().buildTemplate()')
        #Build the tab for building the rig
        with pm.frameLayout(l='Build Rig', mw=5, mh=5, bs='out') as frame:
            with pm.columnLayout(adjustableColumn=True):
                #Create the button to build the rig
                pm.button(label='Build The Rig', command='TestRig().buildRig()')
    #Build the actual window
    def buildGUI(self):
        #Call the method to delete the window if it already exists
        self.deleteExisting()
        #Call the method to set the window-preferences
        self.setWindowPrefs(width=300, height=100)
        #Add the window content
        with pm.window(self.winName, title=self.winTitle, sizeable=True):
            with pm.columnLayout(adjustableColumn=True):
                gui = self.populateGui()
 
class TestRig(object):
    def buildTemplate(self):
        #Create the template locators
        lLeg = pm.spaceLocator(name='l_leg_loc')
        lKnee = pm.spaceLocator(name='l_knee_loc')
        lFoot = pm.spaceLocator(name='l_foot_loc')
        rLeg = pm.spaceLocator(name='r_leg_loc')
        rKnee = pm.spaceLocator(name='r_knee_loc')
        rFoot = pm.spaceLocator(name='r_foot_loc')
        #here goes some code
        print 'now place the locators'
    def buildRig(self):
        #Somehow get the name of the locators created
        #then do awesome stuff
        print 'i need the naaames'
TestRigUI().buildGUI()
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
 

Justin Israel

unread,
Oct 17, 2013, 3:15:33 PM10/17/13
to
It may sound super advanced, but you are actually already using those concepts :-)

class TestRigUI(object):

    def __init__(self):
        self.winName = 'testRiggerWin'
        self.winTitle = 'Test Rigger'

winName and winTitle are attributes of the instance of TestRigUI. Each new instance of TestRigUI will get its own internal state, having individual values of those two attributes. It is by this approach that you can store more state across the multiple functions that need to be called to finalize the operation.

The first problem in your code is that your button callbacks in the main UI are creating temporary instances of TestRig(), calling the method, and throwing them away. 

pm.button(label='Build The Template', command='TestRig().buildTemplate()')

If you didn't need state between the method calls in the first place, then technically you wouldn't even need a class here, and each one of those could just be functions. But for the purpose of a TestRig storing the state across its build operation, you will want to create and store discreet instances.

Here is a version of your code (mind you, it is only a very quick concept of how you could approach the solution)

First thing to notice is that I have decided to track multiple rig instances, and the current rig instance:

class TestRigUI(object):

    def __init__(self):
        self.winName = 'testRiggerWin'
        self.winTitle = 'Test Rigger'

        self.rigs = []
        self.currentRig = None

Then I created callback functions to handle the buttons:

def newRig(self):
    rig = TestRig()
    self.rigs.append(rig)
    self.currentRig = rig
    return rig

def buildCurrentRig(self):
    self.currentRig.buildRig()

And instead of using a string for the button commands, I use a reference to the callables:

pm.button(label='Build The Template', command=self.newRig)
...
pm.button(label='Build The Rig', command=self.buildCurrentRig)

Every time you click the Build Template button, it will create a new TestRig instance, store it in the list, and set it to be the current one. When you blick the Build Rig button, it will call buildRig() on the current one (obviously there is no guard yet to prevent someone from pressing the buttons multiple times in different orders)

Now for saving the state in your TestRig. Its basically the same thing as we are doing in the UI class for storing rigs:

class TestRig(object):

    def __init__(self):
        self.locators = {}

    def buildTemplate(self):
        #Create the template locators
        self.locators = dict(
            lLeg = pm.spaceLocator(name='l_leg_loc')
            ...
            rFoot = pm.spaceLocator(name='r_foot_loc')
        )
 
    def buildRig(self):
        print 'i need the naaames'
        print '... and I can get them from', self.locations

Hope that helps!



To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Simen Chris

unread,
Oct 18, 2013, 5:06:34 AM10/18/13
to python_in...@googlegroups.com
Wow, thank you so much! Exactly what I was looking for, and now I can even get the PyMel functionality that lets me rename the locators, and still get the names in the rig-function! Awesome man, thanks again :D

On Thursday, October 17, 2013 9:13:52 PM UTC+2, Justin Israel wrote:
It may sound super advanced, but you are actually already using those concepts :-)

class TestRigUI(object):

    def __init__(self):
        self.winName = 'testRiggerWin'
        self.winTitle = 'Test Rigger'

winName and winTitle are attributes of the instance of TestRigUI. Each new instance of TestRigUI will get its own internal state, having individual values of those two attributes. It is by this approach that you can store more state across the multiple functions that need to be called to finalize the operation.

The first problem in your code is that your button callbacks in the main UI are creating temporary instances of TestRig(), calling the method, and throwing them away. 

pm.button(label='Build The Template', command='TestRig().buildTemplate()')

If you didn't need state between the method calls in the first place, then technically you wouldn't even need a class here, and each one of those could just be functions. But for the purpose of a TestRig storing the state across its build operation, you will want to create and store discreet instances.

Here is a version of your code (mind you, it is only a very quick concept of how you could approach the solution)

First thing to notice is that I have decided to track multiple rig instances, and the current rig instance:

class TestRigUI(object):

    def __init__(self):
        self.winName = 'testRiggerWin'
        self.winTitle = 'Test Rigger'

        self.rigs = []
        self.currentRig = None

Then I created callback functions to handle the buttons:

def newRig(self):
    rig = TestRig()
    self.rigs.append(rig)
    self.currentRig = rig
    return rig

def buildCurrentRig(self):
    self.currentRig.buildRig()

And instead of using a string for the button commands, I use a reference to the callables:

pm.button(label='Build The Template', command=self.newRig)
...
pm.button(label='Build The Rig', command=self.buildCurrentRig)

Every time you click the Build Template button, it will create a new TestRig instance, store it in the list, and set it to be the current one. When you blick the Build Rig button, it will call buildRig() on the current one (obviously there is no guard yet to prevent someone from pressing the buttons multiple times in different orders)

Now for saving the state in your TestRig. Its basically the same thing as we are doing in the UI class for storing rigs:

class TestRig(object):

    def __init__(self):
        self.locators = {}

    def buildTemplate(self):
        #Create the template locators
        self.locators = dict(
            lLeg = pm.spaceLocator(name='l_leg_loc')
            ...
            rFoot = pm.spaceLocator(name='r_foot_loc')
        )
 
    def buildRig(self):
        print 'i need the naaames'
        print '... and I can get them from', self.locations

Hope that helps!



To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages