从命令行Maya运行Python脚本时出现对象匹配错误

从命令行Maya运行Python脚本时出现对象匹配错误

问题描述:

我在Python中有此脚本,该脚本正在从命令行运行到maya文件中:

I have this script in Python which I'm running into a maya file from a command line:

import maya.standalone
maya.standalone.initialize("Python")
import maya.cmds as cmds
from maya import cmds
import maya.mel as mel
import glob


def importFile(i):
    cmds.file(i, i=True, groupReference=True, groupName="myobj")


def materialFile():
    if cmds.objExists('Panel*'):
        cmds.select("Panel*", replace=True)
        myMaterial = "BlueGlass"
        cmds.sets( e=True, forceElement= myMaterial + 'SG' ) 

    if cmds.objExists('Body*'):
        cmds.select("Body*", replace=True)
        myMaterial3 = "Silver"
        cmds.sets( e=True, forceElement= myMaterial3 + 'SG' )

但是当我尝试运行批处理文件时出现此错误:

But I get this error when I try to run the batch file:

File "/Users/../Scripts/MayaVectorScript.py", line 23, in materialFile
        cmds.sets( e=True, forceElement= myMaterial + 'SG' ) 
TypeError: No object matches name: BlueGlassSG

在Hypershade中,着色器BlueGlass连接到具有相同名称BlueglassSG的着色器组(SG),并且脚本可从maya中的UI进行操作.

In the Hypershade, the shader BlueGlass is connected to a shader group (SG) with the same name BlueglassSG and the script works from the UI inside maya.

我是否需要在脚本中加载插件或其他东西才能使其在批处理文件中运行?

Do I need to load a plugin or something in the script to make it run in the batch file?

通过使用listConnections而不是使用名称来获取阴影组,可以减少一些潜在的错误.名称通常可以使用,但不能保证.如果您输入的材料名称不正确,这仍然行不通,但可以让您更清楚地弄清楚被弄乱的位置:

You can reduce some of the potential for error by using listConnections to get the shading groups instead of using names. Names usually works but it's not guaranteed. This will still not work if you type the material name incorrectly, but it should make it clearer where you've gotten messed up:

import maya.cmds as cmds

def get_sg(shader):
    sgs =  cmds.ls(cmds.listHistory(shader, f=True) or [''], type='shadingEngine') or [None]
    return sgs[0]

def assign(geometry, shader):
    if not geometry:
       cmds.error("No objects to assign")
    sg = get_sg(shader)
    if not sg:
        cmds.error('could not find shader ' + shader)
    cmds.sets(geometry, fe=sg)

assign(cmds.ls('Panel*'), 'BlueGlass')
assign(cmds.ls('Body*'), 'Silver')