Need help passing second argument to event for scriptJob in maya

I am here with the hope to find the answer, I am stuck at a prolemo…

I have a class GUI():
def init(self):

constructor

that has two methods one is

def update(self):

do some task and return

and second methods is createUI(self):

now in this second method I have few attrColorSliderGrp & attrControlGrp

cmds.attrControlGrp("contour",attribute='conturSG.miContourWidth')
cmds.attrColorSliderGrp("ColorSlider", l='Color', at='WireframeSG.miContourColor')
cmds.scriptJob(parent="contour",e=["SelectionChanged","update()"])

but the problem lies with in the scriptJob event’s second argument call to update method…

everytime a selection is changed i get error saying[I]

Error: name ‘update’ is not defined

Traceback (most recent call last):

File “<maya console>”, line 1, in <module>

NameError: name ‘update’ is not defined # [/I]

i tried to use explcit call

inst=GUI(self.message)
the second argument for event as GUI.update(inst)

still didnt work end up with similar erro then i tried to use partial i got error saying # NameError: name ‘partial’ is not defined #

how should i pass the second argument to event in case of scriptJob … I am stuck here … :(:

I dont get any error and everything goes smooth if I am not using OOP’s approach… but their must be somepart I am missing that I am not doing right…

i think i cannot do an explicit call to method of the same class…

alright I got this working, its strange if I am working in non-OOP approach I have to pass the second argument i.e. call to function or script in inverted commas but when following OOP approach i have just to pass second argument as self.update without inverted commas…

Not too strange really.

The string method will resolve it to a name in the global namespace. You have to make sure that the name you’re calling is defined in that namespace:



def i_am_a_callaback():
    print 'hello world'

_globals = globals()  # globals is the python function which returns the 
                              #  root python namespace as  a dictionary
                              # here it's stuck into a local variable called _globals for convenience....
_globals['callback_name'] = i_am_a_callback  # stuff the callback into the name 'callback_name'

# and in your maya code:
cmds.scriptJob(parent="contour",e=["SelectionChanged","callback_name();"])

On the other hand, the no-quotes method is passing a callable function directly as the callback target:


cmds.scriptJob(parent="contour",e=["SelectionChanged", self.callback])

It’s exactly the same as passing a callable to a button or control callback in UI stuff

Incidentally, in your initial sample you were calling ‘update()’ where you should have been calling ‘self.update()’…

Would you mind showing your code? I’m not sure I follow what you’re saying, but I’m getting similar errors.

I started another thread a few hours ago before I found this one if you’d like to see what I’m trying: http://tech-artists.org/forum/showthread.php?6359-Problems-with-creating-a-module-that-can-execute-condition-and-scriptJob-commands&p=30529#post30529

This is my latest attempt, but please follow the link to see a better example (I’m just trying everything I can think of at this point):

import_manip_mod = __import__('mmvCmds.contextualCmds.file_manip')
file_manip_mod = sys.modules["mmvCmds.contextualCmds.file_manip"]
file_manip_open_fn = getattr(file_manip_mod, 'open_file')

import maya.cmds as mc
mc.scriptJob(runOnce=True, e=["idle", "file_manip_open_fn('{0}')".format(open_file_path)], permanent=True)

I get this error:


# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name 'file_manip_open_fn' is not defined //

Wait… this just worked but not quite. It runs the command, but not during the idle time. Is this correct?:

mc.scriptJob(runOnce=True, e=["idle", `file_manip_open_fn('{0}'.format(open_file_path))`], permanent=True)

don’t pass the python code as string unless you have to. Wherever a python callback is accepted (that’s not everywhere in Maya’s api, but mostly everywhere), try one of these:


# notice that we're passing a function, not function call
mc.scriptJob(runOnce=True, e=["idle", myObject.myMethod], permanent=True)
mc.scriptJob(runOnce=True, e=["idle", myGlobalFunction], permanent=True)

# when in doubt, wrap into temporary function; remember that in Python you can 
# declare functions anywhere in the code, even inside other functions
open_file_path = '...'
def idle_handler(*args):
   # here's where you solve the 'how to pass the argument into the handler' problem - 
   # use variable from outer scope
   file_manip_open_fn(open_file_path)
mc.scriptJob(runOnce=True, e=["idle", idle_handler], permanent=True)


That’s exactly what I needed. Thank you so much for the helpful explanation!