def doImportAsset(arg=None): ''' Button: Import Asset. Imports the asset using namespaces, then removes the namespaces. ''' currentImageLibrary = getImageLibrary() currentAssetLibrary = getAssetLibrary() chesspieceTypes = ["CPF", "CPO", "CPD"] cmds.namespace(setNamespace=":") directory = getCategoryFolder() if not directory: return directory = directory.replace(currentImageLibrary, currentAssetLibrary) selected = cmds.textField("currentAssetTFD", query=True, text=True) if not selected: sys.stdout.write("Select an asset.\n") return assetDirectory = selected.partition("_")[0] filename = os.path.join(directory, assetDirectory, "%s.ma" % selected) if currentAssetLibrary == ddConstants.CHAR_ASSETLIBRARY: # due to character naming 'CHAR_character', # assetDirectory needs to be more accurate char_patt = re.compile("^([A-Z]{3}_[a-zA-Z]+)\w*$") if char_patt.match(selected): assetDirectory = char_patt.search(selected).groups()[0] filename = os.path.join(directory, assetDirectory, "chesspiece", "published", "%s.ma" % selected) if not os.path.isfile(filename): sys.stdout.write("File not found: %s.\n" % filename) return importedNodes = cmds.file(filename, i=True, namespace=selected, returnNewNodes=True) topNode = [x for x in importedNodes if cmds.nodeType(x) == "transform" and not cmds.listRelatives(x, shapes=True)] if currentAssetLibrary == ddConstants.CHAR_ASSETLIBRARY: topNode = [x for x in importedNodes if cmds.nodeType(x) == "transform"] if topNode: topNode = ddRemoveNamespaces.do(topNode[0]) ddRemoveDuplicateShaders.do(topNode) deselectAllAssets()
def do(nodes=None): ''' Import selected nodes from reference. @param nodes: One or more GEO or GRP nodes. ''' if not nodes: nodes = cmds.ls(selection=True, long=True) if not isinstance(nodes, list): nodes = [nodes] cmds.namespace(setNamespace=":") # Seems to be still causing issues, but appears to work fine for not without # # Try to turn off Viewport 2.0 to prevent fatal errors. # try: # # making to use currently focused panel and switch pythonically # current_view = cmds.getPanel(withFocus=True) # print 'attempting to update current view panel:: %s' % current_view # cmds.modelEditor(current_view, edit=True, # rendererName="base_OpenGL_Renderer") # print 'switched renderer to base opengl..' # #mel.eval('setRendererInModelPanel base_OpenGL_Renderer modelPanel4;') # except: # pass # Create a temporary top group. topGrp = "tempRefGrp" if cmds.objExists(topGrp): cmds.delete(topGrp) topGrp = cmds.createNode("transform", name="tempRefGrp") newTopNodes = list() for node in nodes: if not cmds.objExists(node): continue if not cmds.referenceQuery(node, isNodeReferenced=True): sys.stdout.write("--> %s is not referenced. Skipping...\n" % node.rpartition("|")[2]) continue currentNode = node referenceFile = cmds.referenceQuery(node, filename=True) tempNamespace = "" # Gather the reference node data. try: tempNamespace = "tempNamespace" cmds.file(referenceFile, edit=True, namespace=tempNamespace) currentNode = "%s:%s" % (tempNamespace, node.rpartition(":")[2]) except: pass # Parent the namespace objects under the temporary group. currentParent = cmds.listRelatives(currentNode, path=True, parent=True) cmds.parent(currentNode, topGrp) # Import from reference and merge namespace with root. cmds.file(referenceFile, importReference=True, mergeNamespaceWithRoot=True) if not tempNamespace == "": if cmds.namespace(exists="%s" % tempNamespace): cmds.namespace(removeNamespace=tempNamespace, mergeNamespaceWithRoot=True) # Find the imported nodes under the temporary group and reparent to original location. topNode = cmds.listRelatives(topGrp, children=True, path=True) newTopNode = topNode[0] if currentParent: newTopNode = cmds.parent(newTopNode, currentParent)[0] else: newTopNode = cmds.parent(newTopNode, world=True)[0] # Fix the GEO instance numbers. done = ddCheckGeoInstanceNumber.do(newTopNode) # Reassign the shaders. done = ddRemoveDuplicateShaders.do(newTopNode) newTopNodes.append(newTopNode) # Delete the temporary group. if cmds.objExists(topGrp): cmds.delete(topGrp) return newTopNodes