calling class func from cmds.button

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  10 October 2012
calling class func from cmds.button

i have a class A that creates new renderLayer in maya

when user clicks create button (of class A) , a command is calling a function of the class , say funcA

cmds.button( label="Passes",command=("self.funcA()"))

now, this class A (resides in a file) in a package and is called from another class B

but with the click following error occurs

NameError: file <maya console> line 1: name 'self' is not defined # 

What am i missing

there is another command which works
cmds.button( label=" Close ", command=("cmds.deleteUI(\"" + + "\", window=True)") )

this too has a 'self' being called. But this works
  10 October 2012
thank you chadrik for posting link

It helped to do just that. Just for records, how did you think of using , i mean how does one
think in depth in python. Thank you a lot anyway.

class Callback(object): 
def __init__(self, func, *args, **kwargs): 
self.func = func 
self.args = args 
self.kwargs = kwargs
def __call__(self, *args): 
return self.func( *self.args, **self.kwargs ) 
  10 October 2012
you might also want to look at 'partial' and 'lambda' functions for doing this in the UI button calls.
Founder & CEO : Red9Consultancy
Red9 StudioPack : Red9 StudioPack GitHub
Author: Autodesk Masterclass 2011 - 'Live Animation Binding'
  10 October 2012

yeah I just always use partial in this pattern:

from functools import partial

class someClass:
     def printTheText(self, theTextArg, item = None ):
         print theTextArg

     def makeUI(self):
          theText = 'hello'

so notice you need an extra argument in the function definition (its the ELF object name) .. I more often than not dont need it so set it to a default argument of None so I can call the function from outside the context of the button command call

you can have as many arguments as you like and will work inside or outside of a class scope
  10 October 2012
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 03:36 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.