Object match error when running a Python script from command line Maya

无人久伴 提交于 2019-12-11 08:53:01

问题


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

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?


回答1:


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


来源:https://stackoverflow.com/questions/40624108/object-match-error-when-running-a-python-script-from-command-line-maya

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!