在Maya中为多个obj文件分配多个材质 [英] Assigning multiple materials to multiple obj files in Maya

查看:589
本文介绍了在Maya中为多个obj文件分配多个材质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Phyton或Mel中的脚本,可以将不同的材料分配给来自Rhino的OBJ导入文件,并将相同的材料分配给从Rhino导入的下一个OBJ文件。

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.

这可能吗?

以下是我的尝试:

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

obj文件部分分组,我需要分配每组不同的材料。即:第5组,第6组,第7组

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

在Maya中指定着色器时,我们实际上将对象形状(或面)连接到shadingEngine,而不是着色器本身。

另一种想法是你没有将对象添加到集合中,它更像是将集合分配给对象。这就是原因:

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)

而不是(像其他Maya命令一样):

And not (like other Maya commands):

cmds.sets (sahdingEngine, edit = True, forceElement = obj)

编辑

我使用blinn作为示例,但所有着色器的原理相同(我认为)。

EDIT
I used blinn for the example but the principle is the same for all shaders (I think).

这篇关于在Maya中为多个obj文件分配多个材质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆