代码之家  ›  专栏  ›  技术社区  ›  Ghoul Fool

导入时对象的名称

  •  1
  • Ghoul Fool  · 技术社区  · 6 年前

    我无法获取引用对象的名称(要重命名它),无法使用python将多个对象导入到maya中:

    import maya.cmds as cmds
    import os
    
    myFolder = r"D:\temp\objs"
    objFiles = cmds.getFileList(folder = myFolder, filespec = "*.%s" % "OBJ")
    for item in objFiles:
      fname = os.path.join(myFolder, item)
      x = cmds.file(fname, i = True) 
    

    结果x是对象的路径名,而不是大纲视图中显示的对象的名称。

    要重命名它,正确的引用是什么?

    1 回复  |  直到 6 年前
        1
  •  2
  •   theodox    6 年前

    你发布的代码只是进行导入。你在寻找你在循环的每一步导入的对象?

    隔离导入对象的简单方法是在导入时指定命名空间。然后,可以使用新的命名空间快速定位对象:

    for eachfile in list_of_files:
        # make a namespace.  In production you might want to double
        # check to make sure the same namespace does not already exist
        import_ns = os.path.splitex(os.path.basename(eachfile))[0]  
        cmds.file(eachfile, i=True, ns = import_ns)
    
        # this gets all of the imported stuff:
        imported_objects = cmds.ls (import_ns + ":*") 
    
        # now you can loop over it and rename as needed.
    

    您只能从 imported_objects 通过使用类型标志向ls添加第二个调用,即

        imported_shapes = cmds.ls(imported_objects, type='shape')