I'm looking for a script in Phyton or Mel where is can assign different materials to an OBJ imported file from Rhino and the same materials to the next OBJ file also imported from Rhino.
Is that possible?
Here's what I have tried:
import maya.cmds as cmds
import glob
def importFile(i):
cmds.file(i, i=True, groupReference=True, groupName="myobj")
def materialFile():
cmds.select("myobj")
myMaterial = "blinn2"
cmds.sets( e=True, forceElement= myMaterial + 'SG' )
The obj files parts come in groups and I need to assign a different material to each group. i.e: group 5, group 6, group7
Since you seem to have have the list of objects and corresponding (existing?) shaders, there is no particular difficulty.
Here is the code.
from maya import cmds
# Here we have the list of all our shaders with the
# polygon shapes or polygon faces on which they are assigned
objectsShaders = {'blinn3': ['pCube4.f[1:3]', 'pCube4.f[5]', 'pCubeShape2'],
'blinn4': ['pCube4.f[0]', 'pCube4.f[4]', 'pCubeShape3'],
'blinn5': ['pSphereShape1']}
# Loop through the dictionary
for shader, allObjs in objectsShaders.items():
# First we retrieve all the shading engine
# connected to the current shader
# (We will assume we only have one)
shaderConnections = cmds.listConnections (shader)
sahdingEngine = ''
for connection in shaderConnections:
if cmds.objectType (connection) == 'shadingEngine':
sahdingEngine = connection
break
# For each object of the list,
# we assign the shader to it
for obj in allObjs:
cmds.sets (obj, edit = True, forceElement = sahdingEngine)
# End statement
print 'All the shaders were successfully re-assigned to their coresponding objects.'
When assigning a shader in Maya we actually connect the object shape (or faces) to the shadingEngine, not the shader itself.
Another think is that you don't add the object to the set, it's more like you're assigning the set to the object. That's why it is:
cmds.sets (obj, edit = True, forceElement = sahdingEngine)
And not (like other Maya commands):
cmds.sets (sahdingEngine, edit = True, forceElement = obj)
EDIT
I used blinn for the example but the principle is the same for all shaders (I think).