Assigning multiple materials to multiple obj files in Maya

 ̄綄美尐妖づ 提交于 2019-12-12 03:06:25

问题


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


回答1:


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).



来源:https://stackoverflow.com/questions/40142581/assigning-multiple-materials-to-multiple-obj-files-in-maya

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!