Problems with creating a module that can execute condition and scriptJob commands?

I’m creating a UI tool that loads during Maya’s startup, and executes some modules AFTER VRay has initialized (otherwise an error is thrown).

The listener.py code below works when run from within Maya’s script editor, but when I turn this snippet into a package module that is then imported from another module, I get this error:


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

Below is the listener code. When it works, it should print ‘hey’ in Maya’s console whenever you turn on VRay (it will print ‘hey’ when you run the code if VRay is already on). Note that the condition command requires a mel command, which is why I’m using ‘python()’ (seems to be a bug), so just calling the normal function doesn’t work and gives an error that procedure cannot be found.

Here’s the listener:


# vray_listener.py

import os

import maya.cmds as mc
import maya.mel as mel

vray_plugin_path_2016   = os.path.join('C:', os.sep, 'Program Files', 'Autodesk', 'Maya2016', 'vray', 'plug-ins', 'vrayformaya.mll')

#-----------------------------------------------------------------------
def is_vray_loaded():
    return mc.pluginInfo(vray_plugin_path_2016, q=1, l=True)

#-----------------------------------------------------------------------
def hey():
    print 'hey'

mc.condition('vray_initialized', initialize=True, d='idle', s='python("is_vray_loaded()");')

mc.scriptJob(ct=['vray_initialized', 'hey()'])

Here’s the launcher that I use to execute the module from Maya:

# launcher.py

import sys

vray_listener_path = 'S:/path/to/module'

if vray_listener_path not in sys.path:
    sys.path.append(vray_listener_path)

import vray_listener
reload(vray_listener)

To get this up an running, just save a python file named “vray_listener.py” anywhere on your computer and take note of the directory. Then put that directory as the variable for “vray_listener_path”. Then run the “launcher.py” code from Maya’s script editor.

Thanks for any help!

Mike

Script Jobs (and calls to python-execute through MEL) execute inside Maya’s python namespace (global namespace), not the module namespace they are created in.

To call a function inside a module from within a script-job, you will have to specify the fully qualified path to the function you want to execute.
In your case “vray_listener.is_vray_loaded()”.
You can also get the module’s name dynamically, so you are still safe if you change the module structure:
name+".is_vray_loaded()"

You will also have to do this for the “hey()” inside the scriptjob.