Maya Python开发带参数的对象复制UI工具时出现无效语法错误求助
Maya自定义复制工具语法报错问题排查
问题描述
用户正基于Python开发Maya端对象复制工具:已完成纯脚本形式的基础复制能力,计划制作可编辑的自定义UI窗口,支持配置随机生成种子、Shield类待复制对象的生成数量参数;但运行编写的脚本时持续返回「# 错误:无效语法 #」的提示,需要定位报错原因、梳理实现逻辑。
排查发现的具体问题
- 首要语法报错根因:Python要求函数定义行末尾必须携带冒号,代码中
def ShieldCreation (pSeedNumber, pShieldAmount)行遗漏了末尾冒号,是直接触发无效语法报错的原因 - 缺失模块导入:代码使用
functools.partial为UI按钮绑定带参回调,但顶部未导入functools模块,修复语法错误后运行会触发「名称未定义」的报错 - 残留无效测试代码:
ShieldCreation函数末尾的result: [u'pCylinder1']是无意义的测试残留,Maya默认使用的Python2环境不支持该类写法,会直接触发语法报错,且代码本身无实际执行作用 - 回调链路断连:
applyCallback函数内仅完成了UI参数查询、选中对象列表获取的步骤,未实际调用核心的ShieldCreation执行逻辑,会导致点击Apply按钮后无任何复制效果 - 边界判断缺失:
ShieldCreation函数直接取选中列表的第一个元素作为待复制对象,未判断场景中是否存在选中对象,空选中状态下会触发索引越界报错
修正后可正常运行的完整代码
import maya.cmds as cmds import random import functools def createUI(pWindowTitle, pApplyCallback): windowID = 'myWindowID' if cmds.window( windowID, exists=True): cmds.deleteUI (windowID) cmds.window( windowID, title=pWindowTitle, sizeable=True, resizeToFitChildren=True) cmds.rowColumnLayout (numberOfColumns=3, columnWidth=[ (1,75), (2,60), (3, 60)], columnOffset=[ (1,'right', 3) ] ) cmds.text( label='Random Seed:') seedNumber = cmds.intField( minValue=0, maxValue=10000, value=0 ) cmds.separator (h=10, style='none') cmds.text( label='Shield Amount:') shieldAmount = cmds.intField( minValue=0, maxValue=1000, value=0 ) cmds.separator (h=10, style='none') cmds.separator (h=10, style='none') cmds.separator (h=10, style='none') cmds.separator (h=10, style='none') cmds.separator (h=10, style='none') cmds.button( label='Apply', command=functools.partial( pApplyCallback, seedNumber, shieldAmount) ) def cancelCallback(*pArgs): if cmds.window( windowID, exists=True): cmds.deleteUI (windowID) cmds.button( label='Cancel', command=cancelCallback) cmds.showWindow() def ShieldCreation (pSeedNumber, pShieldAmount): random.seed(pSeedNumber) result = cmds.ls( orderedSelection = True) transformName = result[0] instanceGroupName = cmds.group( empty=True, name = transformName + '_instance_grp#') for i in range( 0, pShieldAmount): instanceResult = cmds.instance( transformName, name = transformName + '_instance#') cmds.parent ( instanceResult, instanceGroupName ) x = random.uniform(-10, 10) y = random.uniform(0, 20) z = random.uniform(-10, 10) cmds.move( x, y, z, instanceResult) xRot = random.uniform(0, 360) yRot = random.uniform(0, 360) zRot = random.uniform(0, 360) cmds.rotate( xRot, yRot, zRot, instanceResult) scalingFactor = random.uniform (0.3, 1.5) cmds.scale( scalingFactor, scalingFactor, scalingFactor, instanceResult) cmds.hide (transformName) cmds.xform( instanceGroupName, centerPivots = True) def applyCallback ( pSeedNumber, pShieldAmount, *Args): seedNumberVal = cmds.intField( pSeedNumber, query=True, value=True) shieldAmountVal = cmds.intField( pShieldAmount, query=True, value=True) selectionList = cmds.ls( selection = True, type = 'transform' ) if not selectionList: cmds.warning("请先选中需要复制的Shield对象") return ShieldCreation(seedNumberVal, shieldAmountVal) createUI( 'My Title', applyCallback)
内容的提问来源于stack exchange,提问作者Dmitry
相关产品推荐
相关产品推荐

