Maya (Python): Running condition command and scrip

2019-09-11 00:08发布

问题:

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).

A suggestion from my broader question here has lead me to try out the condition and scriptJob commands.

The listener.py code below works when run from within Maya's script editor, but when I import the listener module and run it using the launcher.py code, 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

Note that the condition command requires a mel command syntax (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:

# 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)

回答1:

try that,

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(*args):
    return mc.pluginInfo(vray_plugin_path_2016, q=1, l=True)

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

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

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