【问题标题】:Assigning multiple materials to multiple obj files in Maya在 Maya 中将多个材质分配给多个 obj 文件
【发布时间】:2016-10-25 18:55:43
【问题描述】:

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

这可能吗?

这是我尝试过的:

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 组

【问题讨论】:

  • 你能告诉我们你已经尝试了什么吗? .. 还是你要我们写? ;)
  • @theodox 我在 Phyton 中有这段代码,但它只适用于 blinn 着色器。我无法从渲染器甚至 Lambert 分配着色器。 'code' 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' )'code' obj 文件部分分组,我需要为每个组分配不同的材料。即:第 5 组、第 6 组、第 7 组
  • OBJ 文件通常带有关联的 MTL 文件,你有吗?
  • @theodox 否,因为这个想法是在 Maya 中分配材料。目前我正在使用 Maxwell 渲染,所以我需要应用它的材质
  • @achayan 我已经尝试过了,但是代码只应用了一种材料,我需要在 obj 文件中应用两三种材料

标签: import maya assign


【解决方案1】:

由于您似乎拥有对象列表和相应(现有?)着色器,因此没有特别困难。
这是代码。

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 中分配着色器时,我们实际上将对象形状(或面)连接到着色引擎,而不是着色器本身。
另一个想法是您没有将对象添加到集合中,更像是您将集合分配给对象。这就是它的原因:

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

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

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

编辑
我在示例中使用了 blinn,但所有着色器的原理都是相同的(我认为)。

【讨论】:

  • @UKDP 感谢您的帮助。我现在如何将它与我拥有的代码混合使用?这段代码的一般流程是:所以我需要知道在哪里添加你的代码。这是我在这个问题开头的代码的其余部分:def renderFile(i): cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string") cmds.render(batch=True)def deleteFile(): cmds.select("myobj") cmds.delete()myglob = glob.glob("/Path/to/my/obj/*.obj")for i in myglob: importFile(i) moveFile() materialFile() renderFile(i) deleteFile()
  • @jpits 不幸的是,我不知道您的脚本有太多参数。我想它会在materialFile()renderFile(i) 之间的某个地方,但我不能说...如果您不知道如何完成这项工作,您应该询问了解该项目以及如何正确实施它的人。
  • 好的。我有另一个问题。该代码与我的第一个 obj 文件完美配合。但是当导入我的第二个 obj 时,它给了我一个错误 No object matches name: ,因为它的部分比第一个导入的 obj 少。是否有任何代码忽略错误并完成操作?
  • @jpits No object matches name 表示您的 obj 不存在。也许它没有正确导入场景中,或者 - 如果您在脚本中硬编码对象名称 - 存在一些正交错误或其他问题。
  • 我有两个作为组导入的 obj 文件。第一组由四个多边形组成:metal_01、metal_02、metal_03、metal_04。第二组仅由三个多边形组成:metal_01、metal_02、metal_03。我将脚本中的名称替换为与 obj 部分相同的名称:objectsShaders = {'blinn3': ['metal_01', 'metal_01_Shape'] 在 metal_04 之前相同。因为第二个 obj 没有 metal_04 它给了我一个错误并且不应用其余的材料。我需要忽略这个错误并完成任务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 2015-08-19
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多