【问题标题】:Import single OBJ files into Maya, moving and rotating it with Python/MEL将单个 OBJ 文件导入 Maya,使用 Python/MEL 移动和旋转它
【发布时间】:2016-12-26 03:36:05
【问题描述】:

您好,我有这段代码可以使用 Python 将 OBJ 文件导入 Maya

**

import maya.cmds as cmds

pathOfFiles = "/Path/to/the/files/folder/"
fileType = "obj"

files = cmds.getFileList(folder=pathOfFiles, filespec='*.%s' % fileType)
if len(files) == 0:
    cmds.warning("No files found")
else:
    for f in files:
        cmds.file(pathOfFiles + f, i=True)

**

它会导入该文件夹中的所有 obj 文件。

但是,我需要的是:

  1. 一次导入单个 OBJ 文件
  2. 移动和旋转导入的文件
  3. 应用已在 Maya 中创建的材质
  4. 渲染
  5. 删除文件 6 对下一个文件重复该过程

是否可以用 Python 或 MEL 来实现

【问题讨论】:

    标签: python import rendering maya


    【解决方案1】:

    这看起来是一个有趣的挑战,所以这是我尝试回答的问题:

    import maya.cmds as cmds
    import glob
    
    #1. Import an individual OBJ file at once
    def importFile(i):
        cmds.file(i, i=True, groupReference=True, groupName="myobj")
    
    #2. Move and rotate the imported file
    def moveFile():
        cmds.select("myobj")
        # Add the X,Y,Z cordinates to change scale, translate and rotate below
        cmds.scale(1,1,1)
        cmds.move(0,0,0)
        cmds.rotate(0,90,0)
    
    #3. Apply a Material already created in Maya
        def materialFile():
        cmds.select("myobj")
        myMaterial = "lambert2" + "SG" #replace lambert2 with your material
        cmds.sets(forceElement=myMaterial)
    
    #4. Render
    def renderFile(i):
        cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string")
        cmds.render(batch=True)
    
    #5. Delete the imported file
    def deleteFile():
        cmds.select("myobj")
        cmds.delete()
    
    # Add the path to your obj files. Make sure to leave the /*.obj at the end
    myglob = glob.glob("/Users/OSX/Desktop/objs/*.obj") 
    
    for i in myglob:
        importFile(i)
        moveFile()
        materialFile()
        renderFile(i)
        deleteFile()
    

    因为您有一个需要脚本执行的个别事情的列表,所以我已将您列表中的每个要求划分为自己的函数。这应该使脚本更加模块化,并有望易于编辑和重用。

    Python 在这类任务上工作得更好,因为 MEL 没有函数,相反,它有一些像函数一样工作的过程,但从我的经验来看效果并不好。

    【讨论】:

    • K先生您好,感谢您的回答。在我更改了一些变量后,它似乎正在工作。但是,有些问题我无法解决。首先,它将图像保存在放置 obj 但无法打开的同一文件夹中。只有 png 和 exr 文件。此外,它不适用该材料。将变量 myMaterial 更改为在 Maya 中创建的变量,但它仍然没有得到它。怎样才能应用一种以上的材料?物体需要两种材料。如何设置多个渲染相机?欢呼
    • 嗨@Mr-k 我有一个问题,那就是它只将一个 blinn 着色器应用于整个作品,而不再适用。我需要应用 Maxwell 渲染材质并将 maxwell 设置为渲染器。
    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多