question about userSetup.py


#1

Hi, I have experience with other 3D software and script languages but I’m kinda new with Maya and Python and I’m having a problem with the userSetup.py.

I’m trying to loading a module with the [userSetup.py].
This module is called [mytoolsLoader] and it is in the same folder(maya/scripts).
This loader [mytoolsLoader] imports another module, called [setProject], and creates an event to execute that module main function.
I want to use this setup so I can add more modules and functions to the loader without having to change the userSetup.py,making it easier to update.

Long story short, it doesn’t work.

If I load [setProject] directly with [userSetup.py], it works.

If I load [setProject] through [mytoolsLoader], it doesn’t.

If I import the [setProject] module in [userSetup.py] but still execute it and create an event with [mytoolsLoader], it works.

and I have no idea why. What am I doing wrong?

This is how I’d like to do it, but doesn’t work:

userSetup.py inside maya/scripts

#userSetup.py
import mytoolsLoader
mytoolsLoader.main()

toolsLoader.py inside maya/scripts

#toolsLoader
from maya import cmds
from mytools import setProject
def main():
    cmds.scriptJob( event=("SceneOpened", "setProject.main()") )

setProject.py inside the mytools folder (maya/scripts/mytools)

#setProject
def main():
    code........

So to make it work, I’ve added “from mytools import setProject” to my userSetup.py as a workaround.


#2

The story is useful info, but “it doesnt work” is a poor substitute for an error message. Please provide the stack trace or at least describe how it fails.

David


#3

Thanks for your reply and sorry for the lack of details.

With [toolsLoader.py] I’m importing my tool [setProject] and creating an event to trigger when an scene is opened.
The event is successfully registered, but the module [setProject] isn’t loading. Since the event works, I know [toolsLoader.py] is being executed.

I get this error everytime the event triggers:

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

So it seems [setProject] isn’t being imported by [toolsLoader.py].

I tried putting the import sentence inside the function ( toolsLoader.main() ), but it didn’t work.
Then I put the import sentence in the [userSetup.py] before executing [toolsLoader.py] and it worked.

I don’t understand why [toolsLoader.py] isn’t importing the module [setProject]


#4

I dont often use maya.cmds so I’m a bit fuzzy on that, but I believe the problem stems from your use of a string to specify the script in the scriptJob event. This would lead to a scope problem.

With pymel I would do it as follows…

#toolsLoader
import pymel.core as pm
from mytools import setProject
def main():
    pm.scriptJob( event=("SceneOpened", setProject.main) ) # not "setProject.main()"

Might be the same with cmds.

David


#5

Thanks! the problem was the string.

It works perfect now with pymel or cmd.