gpt4 book ai didi

python - 在所有渲染层中获取附加到对象的所有 Material [Maya]

转载 作者:太空宇宙 更新时间:2023-11-04 00:41:28 26 4
gpt4 key购买 nike

问题

在不切换渲染层的情况下获取附加到对象的所有 Material ;从特定对象的其他渲染层获取 Material 。

从对象中获取 Material 的代码

# Gets all shaders attached to the current renderLayer for all objects in "group1"
import maya.cmds as cmds

cmds.select("group1")
allChildren = cmds.listRelatives(ad=1)
for eachChild in allChildren:
# Get the shader groups attached to this particular object
shaderGroups = cmds.listConnections(cmds.listHistory(eachChild))
if shaderGroups is not None:
# Get the material attached to the shader group
materials = [x for x in cmds.ls(cmds.listConnections(shaderGroups), materials=1)]
print materials

我所知道的获取放置在其他渲染层中的信息的唯一其他方法是切换到它们,然后运行相同的代码...有人有任何其他建议吗?

编辑:

cmds.listConnections()cmds.listHistory() 以及以上两者的组合似乎提供了渲染层,如果附加到对象我从那里获取 Material ; IE。 - materials = [u'Car_Paint_Material', u'RenderLayer_BluePaint', u'RenderLayer_RedPaint']

编辑#2:

截图!

defaultRenderLayer -> Red material for selected objectdefaultRenderLayer -> 所选对象的红色 Material 。

layer1             -> Green material for selected objectlayer1 -> 所选对象的绿色 Material 。

Run above script to get material运行上面的脚本来获取 Material 。

Run un-optimized to get object + renderLayer + material运行未优化的对象 + renderLayer + Material 。它可以工作,但它必须切换渲染层才能获取该信息。

我需要一种无需切换渲染层即可获取上述信息的解决方案。

最佳答案

你的方法有问题

materials = [x for x in cmds.ls(cmds.listConnections(shaderGroups), materials=1)] 仅返回所选图层的着色器名称。

解决方案

以下脚本将所有层的所有模型的所有 Material 按以下顺序存储在字典中;

meshName : {layerName : shader/MaterialName}

它存储在名为objectList的字典中

该脚本已在具有 1203 个对象和 2 个渲染层(+ 默认渲染层)的场景中进行了测试。它在几分之一秒内执行。

meshList = cmds.ls(type='mesh')
shaderList = {}
for mesh in meshList:
shaderList[mesh]={}

#get render layers
renderLayers = [x for x in cmds.ls(type="renderLayer")]

for eachLayer in renderLayers:
currentRenderLayerName = eachLayer
attrib = currentRenderLayerName+'.outAdjustments'
numberOfConnectedMeshes = len(cmds.getAttr(attrib, multiIndices=True))

for currentMeshIndex in range(numberOfConnectedMeshes):

queryConnection = currentRenderLayerName + '.outAdjustments['+str(currentMeshIndex)+']'
# sometimes if you delete an object, the connection might still be active.
# validating it with an if condition avoids errors
if cmds.listConnections(queryConnection+'.outPlug') != None:

# following line should technically return a mesh name, however it returns the transform object name
currentTransformObject = cmds.listConnections(queryConnection+'.outPlug')[0]
# following line is important as the 'currentTransformObject' does is the not mesh name.
# if it does return the mesh object then just change 'currentTransformObject' to 'currentMeshName'
# and comment out the following line
currentMeshName = cmds.listRelatives(currentTransformObject)[0]

currentMeshShadingGroup = cmds.listConnections(queryConnection+'.outValue')[0]
currentShadingGroupSurfaceShader = cmds.listConnections(currentMeshShadingGroup+'.surfaceShader')

shaderList[currentMeshName][currentRenderLayerName] = currentShadingGroupSurfaceShader

# If you only have objects on the defaultRenderLayer
# above code ignored those objects.
# following will add them to the shaderList dict
for eachMesh in shaderList:
if shaderList[eachMesh]=={}:
currentRenderLayerName = "defaultRenderLayer"
materialAppliedToObject = cmds.ls(cmds.listConnections(cmds.listConnections(
cmds.listHistory(cmds.listRelatives(eachMesh, parent=1)))),
mat=1)[0]
shaderList[eachMesh][currentRenderLayerName] = materialAppliedToObject

这在示例场景中返回了以下三个对象。

shaderList
# Result:
{
u'pSphereShape0':
{
u'defaultRenderLayer': [u'lambert2'],
},
u'pSphereShape1':
{
u'defaultRenderLayer': [u'anisotropic1'],
u'layer2': [u'anisotropic3'],
u'layer1': [u'anisotropic2']
},
u'pCubeShape1':
{
u'defaultRenderLayer': [u'phong1'],
u'layer2': [u'phong3'],
u'layer1': [u'phong2']
},
u'pConeShape1':
{
u'defaultRenderLayer': [u'blinn1'],
u'layer2': [u'blinn3'],
u'layer1': [u'blinn2']
}
} #

查询给定网格的表面着色器到相应的层使用以下形式

shaderList['pCubeShape1']['layer2']

# Result: [u'phong3']
# notice it returns a list.
# you have to use the index if you need only the name
#`objectList['pCubeShape1']['layer2'][0]`

如何定制?

您可以通过直接编辑代码来重新安排 dict 的顺序,或者更好的选择是您可以查询选定对象或一组对象的着色器列表,并按所需顺序将其存储在新变量中.

重要

如果对象未添加到渲染层,则不会返回相应的着色器信息。

测试

Mental Ray/软件/硬件 2.0 渲染器(如果你们中的任何人在其他渲染器上取得成功,请添加到答案中)

关于python - 在所有渲染层中获取附加到对象的所有 Material [Maya],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41748884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com