如何在Maya cmds中跳过通道盒无Scale属性的节点进行验证
解决方案
要跳过那些无法读取Scale属性的transform节点,最可靠的方式是在读取属性时捕获异常,或者提前检查属性的可访问性。以下是两种可行的修改方案:
方案1:异常捕获(推荐)
直接对cmds.getAttr的调用进行异常捕获,只要读取属性时抛出错误,就跳过该节点。这种方式能覆盖所有属性无法读取的情况(包括属性隐藏、锁定、不存在等):
修改validation方法中的循环部分:
for node in self.required_nodes: try: scaleX = cmds.getAttr(node + '.scaleX') scaleY = cmds.getAttr(node + '.scaleY') scaleZ = cmds.getAttr(node + '.scaleZ') except RuntimeError: # 属性无法读取,跳过当前节点 continue if scaleX != 1.0 or scaleY != 1.0 or scaleZ != 1.0: self.valid_failed_list.append(node)
方案2:提前检查属性可访问性
如果需要更精细的控制,可以编写辅助函数提前检查属性是否存在且可读取:
def is_attr_accessible(node, attr_name): full_attr = f"{node}.{attr_name}" # 先检查属性是否存在于节点上 if not cmds.attributeQuery(attr_name, node=node, exists=True): return False # 尝试读取属性,确认是否可访问 try: cmds.getAttr(full_attr) return True except: return False
然后在循环中使用该函数过滤节点:
for node in self.required_nodes: # 检查三个Scale属性是否都可访问 if not all(is_attr_accessible(node, attr) for attr in ['scaleX', 'scaleY', 'scaleZ']): continue scaleX = cmds.getAttr(node + '.scaleX') scaleY = cmds.getAttr(node + '.scaleY') scaleZ = cmds.getAttr(node + '.scaleZ') if scaleX != 1.0 or scaleY != 1.0 or scaleZ != 1.0: self.valid_failed_list.append(node)
为什么之前的attributeQuery无效?
attributeQuery的hidden参数只能检测属性是否被标记为隐藏,但有些节点的Scale属性可能是被设置为不可关键帧(non-keyable)、被锁定,或是被连接到了无法读取的节点上,这些情况attributeQuery无法直接检测到。而异常捕获的方式能覆盖所有读取失败的场景,适用性更强。
内容的提问来源于stack exchange,提问作者Kartikey Sinha
相关产品推荐
相关产品推荐

