如何用Python修改Maya中Arnold区域灯光的形状?
解决Maya中用Python修改Arnold区域灯光形状的问题
问题根源
你注释的代码行失效有两个原因:
aiLightShape是枚举类型属性,不能用type="string"直接设置字符串值;- 硬编码形状节点名称(如
PFXKeyShape)不可靠,Maya中形状节点命名可能因场景状态变化。
修正方案
Arnold区域灯光的aiLightShape枚举对应关系:
- rectangle: 0
- disk: 1
- ellipse: 2
- sphere: 3
- cylinder: 4
你可以用两种方式设置:
- 直接使用枚举索引(性能更高效)
- 指定
type="enum"后用字符串设置(可读性更强)
同时建议通过transform节点动态获取形状节点,避免硬编码名称出错。
修正后的完整代码
import maya.cmds as cmds import mtoa.utils as mutils PFX = "PS_" if cmds.objExists(f"{PFX}*"): cmds.delete(f"{PFX}*") def Lights(): ### Key Light ### # 创建Arnold区域灯光并获取transform节点 mutils.createLocator('aiAreaLight', asLight=True) light_transform = cmds.rename('aiAreaLight1', f"{PFX}Key") # 动态获取灯光形状节点 light_shape = cmds.listRelatives(light_transform, shapes=True, fullPath=True)[0] # 移动、旋转、缩放灯光 cmds.move(21.517,6.564,25.303, light_transform) cmds.rotate(11.583,45,0, light_transform) cmds.scale(8.902,8.902,8.902, light_transform) # 方法1:用枚举索引设置为disk形状 cmds.setAttr(f"{light_shape}.aiLightShape", 1) # 方法2:用枚举字符串设置(二选一即可) # cmds.setAttr(f"{light_shape}.aiLightShape", "disk", type="enum") # 设置其他灯光属性 cmds.setAttr(f"{light_shape}.color", 1,.744,.609) cmds.setAttr(f"{light_shape}.exposure", 0) cmds.setAttr(f"{light_shape}.intensity", 2025) cmds.setAttr(f"{light_shape}.aiSamples", 3) Lights()
关键说明
cmds.listRelatives(light_transform, shapes=True, fullPath=True)能准确获取灯光的形状节点,避免因重命名或场景冲突导致的节点名称错误;- 两种枚举设置方式任选其一即可,根据自己的需求选择可读性或性能优先级。
内容的提问来源于stack exchange,提问作者fraugedawg
相关产品推荐
相关产品推荐

