在所有渲染层中获取附加到对象的所有材质 [Maya]
Getting all materials attached to object throughout all render layers [Maya]
问题
在不切换渲染层的情况下将所有 material 附加到对象;从特定对象的其他渲染层获取 materials。
从对象
获取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 -> 选定对象的红色 material。
图层 1 -> 选定对象的绿色 material。
运行 上面的脚本得到 material.
运行 未优化以获取对象 + renderLayer + material。
它可以工作,但它必须切换渲染层才能获取该信息。
我需要一种无需切换渲染层即可获取上述信息的解决方案。
我有一个临时解决方案:
# Gets all shaders attached to the current renderLayer for all objects in "group1"
import maya.cmds as cmds
# Create a dictionary for {"object1": ["material1", "material2"]}
objectToMaterials = {}
# List of renderlayers
renderLayers = [x for x in cmds.ls(type="renderLayer")]
# Pick each renderLayer and switch to it
cmds.select("group1")
allChildren = cmds.listRelatives(ad=1)
for eachChild in allChildren:
if cmds.objectType(eachChild)=="mesh":
eachChild = cmds.listRelatives(eachChild, parent=1)
for eachRenderLayer in renderLayers:
cmds.editRenderLayerGlobals(crl=eachRenderLayer)
# 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)]
objectToMaterials.setdefault(eachChild[0], []).append(eachRenderLayer)
objectToMaterials.setdefault(eachChild[0], []).append(materials[0])
print(objectToMaterials)
# objectToMaterials = {"object1": ["defaultRenderLayer", "RenderLayer_RedPaint"], ["secondRenderLayer", "RenderLayer_BluePaint"], "object2": ["defaultRenderLayer", "RenderLayer_BluePaint"]}
# we need a sandbox case
collection = []
for layer in cmds.ls(type = "renderLayer"):
members = cmds.editRenderLayerMembers( layer, query=True)
for i in members:
shader_engine = cmds.listConnections(cmds.listHistory(i), type = 'shadingEngine')
_shaders = cmds.ls(cmds.listConnections(shader_engine), materials= True)
collection.append(dict(object = members, layer = layer, engine = shader_engine, shaders = _shaders))
print collection
[{'engine': [u'blinn2SG'], 'layer': u'defaultRenderLayer', 'object': [u'pSphere1', u'pSphereShape1', u'pSphere2', u'pSphereShape2'], 'shaders': [u'blinn2']}, {'engine': [u'lambert2SG'], 'layer': u'layer1', 'object': [u'pSphere1'], 'shaders': [u'lambert2']}, {'engine': [u'blinn2SG'], 'layer': u'layer2', 'object': [u'pSphere2'], 'shaders': [u'blinn2']}]
你的方法有问题
materials = [x for x in cmds.ls(cmds.listConnections(shaderGroups), materials=1)]
仅returns所选图层的着色器名称。
解决方案
以下脚本将所有层的所有模型的所有材料按以下顺序存储在字典中;
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 渲染器
(如果你们中的任何人在其他渲染器上取得成功,请添加到答案中)
问题
在不切换渲染层的情况下将所有 material 附加到对象;从特定对象的其他渲染层获取 materials。
从对象
获取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:
截图!
我需要一种无需切换渲染层即可获取上述信息的解决方案。
我有一个临时解决方案:
# Gets all shaders attached to the current renderLayer for all objects in "group1"
import maya.cmds as cmds
# Create a dictionary for {"object1": ["material1", "material2"]}
objectToMaterials = {}
# List of renderlayers
renderLayers = [x for x in cmds.ls(type="renderLayer")]
# Pick each renderLayer and switch to it
cmds.select("group1")
allChildren = cmds.listRelatives(ad=1)
for eachChild in allChildren:
if cmds.objectType(eachChild)=="mesh":
eachChild = cmds.listRelatives(eachChild, parent=1)
for eachRenderLayer in renderLayers:
cmds.editRenderLayerGlobals(crl=eachRenderLayer)
# 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)]
objectToMaterials.setdefault(eachChild[0], []).append(eachRenderLayer)
objectToMaterials.setdefault(eachChild[0], []).append(materials[0])
print(objectToMaterials)
# objectToMaterials = {"object1": ["defaultRenderLayer", "RenderLayer_RedPaint"], ["secondRenderLayer", "RenderLayer_BluePaint"], "object2": ["defaultRenderLayer", "RenderLayer_BluePaint"]}
# we need a sandbox case
collection = []
for layer in cmds.ls(type = "renderLayer"):
members = cmds.editRenderLayerMembers( layer, query=True)
for i in members:
shader_engine = cmds.listConnections(cmds.listHistory(i), type = 'shadingEngine')
_shaders = cmds.ls(cmds.listConnections(shader_engine), materials= True)
collection.append(dict(object = members, layer = layer, engine = shader_engine, shaders = _shaders))
print collection
[{'engine': [u'blinn2SG'], 'layer': u'defaultRenderLayer', 'object': [u'pSphere1', u'pSphereShape1', u'pSphere2', u'pSphereShape2'], 'shaders': [u'blinn2']}, {'engine': [u'lambert2SG'], 'layer': u'layer1', 'object': [u'pSphere1'], 'shaders': [u'lambert2']}, {'engine': [u'blinn2SG'], 'layer': u'layer2', 'object': [u'pSphere2'], 'shaders': [u'blinn2']}]
你的方法有问题
materials = [x for x in cmds.ls(cmds.listConnections(shaderGroups), materials=1)]
仅returns所选图层的着色器名称。
解决方案
以下脚本将所有层的所有模型的所有材料按以下顺序存储在字典中;
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 渲染器 (如果你们中的任何人在其他渲染器上取得成功,请添加到答案中)