简体   繁体   中英

“Sourcing” python scripts with multiple defs - MAYA

So I've been doing a lot of research and couldn't find a proper answer. I'm quite new to python so sorry if this is a simple question.

So, basically, I'm creating an UI that has a button that should call a function from another .py file. What I did so far is append the file's folder to sys.path and import the .py file as something else. Example, let's say I'm importing myTools.py:

import myTools as mt

Now I can successfully access all functions within myTools via mt.mainFunction() or anything with the mt. prefix.

Now my question:

When I run mt.myFunction() directly it works just fine. Problem is that mainFunction() is another UI that calls different functions at different times. All these functions are on the myTools file.. but Maya won't find them because when they are called within the mainFunction() they don't have the mt prefix. I mean, I could run those defs on the userSetup.py but it's quite a big code and I wanted to do that the cleanest way :)

Any ideas?

Thanks in advance!

EDIT: So I just realized is only one function that isn't working. I'm getting this error: # Error: NameError: file <maya console> line 1: name 'annotationToLocator' is not defined #

Because of that error, I thought that my mainfunction couldn't find any other function on the module.

The actual code where I declare this function:

jobNum = cmds.scriptJob(e=['SelectionChanged', 'annotationToLocator()'])

def annotationToLocator ():
selList = cmds.ls(sl=True)
for item in selList:
    if '_ANN' in str(item):
        cmds.select(item,d=True)
        newItem = str(item).replace('_ANN', '_LOC')
        cmds.select(newItem,add=True)

A couple of weird things about this: 1) It works perfectly when I run the code directly.

2) I'm importing the module on the userSetup file.. I'm getting the error above not only when I try to actually run the function that calls this one, but also when Maya starts..

I tried commenting the scriptjob line and now it works just fine, although obviously now I don't have the scriptjob running. I think is some issue with modules and scriptjobs?!

I'm sorry, I know I got off of the original question path here! :)

This sounds like typical python behaviour and should work correctly. Each module has it's own global scope and each function defined in that module will have access to everything defined in that scope.

So in the myTools module each function has access to each other by name, and every function defined in your main module will have access to the mt module object and can get the functions as it's attributes.

You problem stems from using string references to your function. While that works, it only works if they function you're calling by string is in the global python scope -- which usually means it only works in the listener.

The better way to do any maya callbacks is by passing the functions directly to the callback as function objects, not as strings:

  import mymodule
  cmds.scriptJob(e=('somethingSelected', mymodule.fancyfunction))

Note that mymodule.fancyfunction is passed without parens : you are telling Maya "use this function." If you did it as mymodule.fancyfunction() you'd be telling Maya to use the result of a call to the function , not the function itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM