Hi,
is there some way to access the shader names stored in a Vray Proxy with Python or MEL?
I would like to connect shaders based on name to a VRayMeshMtl.
And what does the attribute “Assign shaders by name” in the VRayMeshMtl do? It is not explained in the documentation.
I am trying to write something that will allow me to automatically connect shaders that already exist in the scene with the same names as those in the shader list of the proxy. The above is very useful, thanks for sharing.
I am however a novice when it comes to scripting so was hoping that someone would be able to lend a hand and offer any pointers on how best to achieve what I want to do?
Hi,
here is a basic script that connects shaders with identical names to the VrayMeshMaterial. It works on either a selection or all VrayMeshMaterials in the scene.
import maya.cmds as cmds
def assign_shader():
'''
This method connects shaders with the same name to a VRayMeshMaterial.
If something is selected, it works on the selection, otherwise it works on the whole scene.
Author: Florian von Behr f.vonbehr at thescope.studio
Installation:
Copy the script to your Maya script directory.
Load it in Maya with this command from the script editor:
import vrayProxyTools.py
vrayProxyTools.assign_shader()
'''
vray_proxys = cmds.ls(sl=True) # vrayProxy node name (transform node)
# if nothing is selected
if not vray_proxys:
# get all VRayMeshMaterial in the scene
vray_mesh_materials = cmds.ls(type="VRayMeshMaterial")
print vray_mesh_materials
# if something is selected, get the VRayMeshMaterials from the selection
else:
vray_mesh_materials = []
for proxy in vray_proxys:
vray_proxy_shape = cmds.listRelatives(proxy, s=True)[0] # get the shape node
vray_proxy_mesh = cmds.listConnections(vray_proxy_shape + '.inMesh')[0] # get the proxy node
vray_mesh_materials = cmds.listConnections(vray_proxy_mesh + '.fileName2', type='VRayMeshMaterial') # get the shader
vray_mesh_materials.append(vray_mesh_materials[0]) # add shader to list
print vray_mesh_materials
# if the list of shaders is emtpty
if not vray_mesh_materials:
print("There are no VRayMeshMaterials in the scene.")
else:
for material in vray_mesh_materials:
cmds.setAttr(material + ".assignShadersByName", 1)
for index in range(0, cmds.getAttr(material + '.shaders', s=True)):
# get the shader name
shaderName = cmds.getAttr(material + '.shaderNames[{}]'.format(index))
# check if a shader with that name exists in scene
if cmds.objExists(shaderName) is True:
# and then test if it is already connected
if not cmds.isConnected(shaderName + ".outColor", material + ".shaders" + "[" + str(index) + "]"):
# if not, connect it
cmds.connectAttr(shaderName + ".outColor", material + ".shaders" + "[" + str(index) + "]", f=True)
else:
# If a shader with the name does not exist, let the user know which shader
print "Shader {0} is missing!".format(shaderName)