首页 > 解决方案 > 如何使用 Maya python 列出一个节点属性的所有值?

问题描述

我正在尝试列出一个节点的所有属性及其值(来自 Mash 网络),但是当属性没有值时出现错误,即使我使用 try/except 循环

attributes = cmds.listAttr('MASH_A_Repro')
for attribute in attributes:
    myAttr='MASH_A_Repro.'+attribute
    try :
        print 'Attribute %s Value %s' % (attribute, cmds.getAttr(myAttr) )
    except KeyError:
        print 'erreur'  

错误:RuntimeError:文件第 10 行:消息属性没有数据值。#

在这种情况下,第一个属性是“消息”并且没有值。我怎样才能绕过这个?

标签: pythonmaya

解决方案


您想解决消息属性——因为错误表明它们没有数据。

这可以防止你爆炸:

import maya.cmds as cmds

for item in cmds.listAttr('polyPlane1'):
try:
    print cmds.getAttr('polyPlane1.' + item)
except RuntimeError:
    pass

但是你仍然会得到一个恼人的错误打印输出。listAttr您可以通过限制对可写属性的调用来进行廉价的预检查:

for item in cmds.listAttr('polyPlane1', w=True):
try:
    print item, cmds.getAttr('polyPlane1.' + item)
except RuntimeError:
    pass

推荐阅读