def extract_GPUCache(self, packager): from reveries import lib from reveries.maya import io, capsule from maya import cmds packager.skip_stage() # Collect root nodes assemblies = set() for node in self.data["outCache"]: assemblies.add("|" + node[1:].split("|", 1)[0]) assemblies = list(assemblies) # Collect all parent nodes out_hierarchy = set() for node in self.data["outCache"]: out_hierarchy.add(node) out_hierarchy.update(lib.iter_uri(node, "|")) # Hide unwanted nodes (nodes that were not parents) attr_values = dict() for node in cmds.listRelatives( assemblies, allDescendents=True, type="transform", fullPath=True) or []: if node not in out_hierarchy: attr = node + ".visibility" locked = cmds.getAttr(attr, lock=True) has_connections = cmds.listConnections(attr, source=True, destination=False) if locked or has_connections: continue attr_values[attr] = False # Export cmds.select(assemblies, replace=True, noExpand=True) entry_file = packager.file_name("ma") cache_file = packager.file_name("abc") package_path = packager.create_package() entry_path = os.path.join(package_path, entry_file) cache_path = os.path.join(package_path, cache_file) with contextlib.nested( capsule.attribute_values(attr_values), # Mute animated visibility channels capsule.attribute_mute(list(attr_values.keys())), ): io.export_gpu(cache_path, self.start_frame, self.end_frame) io.wrap_gpu(entry_path, [(cache_file, "ROOT")]) packager.add_data({"entryFileName": entry_file}) self.add_range_data()
def extract_LightSet(self, packager): from maya import cmds from avalon import maya from reveries.maya import capsule entry_file = packager.file_name("ma") package_path = packager.create_package() # Extract lights # entry_path = os.path.join(package_path, entry_file) self.log.info("Extracting lights..") # From texture extractor try: texture = next(chd for chd in self.data.get("childInstances", []) if chd.data["family"] == "reveries.texture") except StopIteration: file_node_attrs = dict() else: file_node_attrs = texture.data.get("fileNodeAttrs", dict()) with contextlib.nested( maya.maintained_selection(), capsule.attribute_values(file_node_attrs), capsule.no_refresh(), ): cmds.select(self.member, replace=True, noExpand=True) cmds.file(entry_path, options="v=0;", type="mayaAscii", force=True, exportSelected=True, preserveReferences=False, constructionHistory=False, channels=True, # allow animation constraints=False, shader=False, expressions=True) packager.add_data({ "entryFileName": entry_file, })
def process(self, instance): from maya import cmds from avalon import maya from reveries import utils from reveries.maya import capsule staging_dir = utils.stage_dir() filename = "%s.ma" % instance.data["subset"] outpath = "%s/%s" % (staging_dir, filename) instance.data["repr.LightSet._stage"] = staging_dir instance.data["repr.LightSet._files"] = [filename] instance.data["repr.LightSet.entryFileName"] = filename # Extract lights # self.log.info("Extracting lights..") # From texture extractor child_instances = instance.data.get("childInstances", []) try: texture = next(chd for chd in child_instances if chd.data["family"] == "reveries.texture") except StopIteration: file_node_attrs = dict() else: file_node_attrs = texture.data.get("fileNodeAttrs", dict()) with contextlib.nested( maya.maintained_selection(), capsule.attribute_values(file_node_attrs), capsule.no_refresh(), ): cmds.select(instance, replace=True, noExpand=True) cmds.file( outpath, options="v=0;", type="mayaAscii", force=True, exportSelected=True, preserveReferences=False, constructionHistory=False, channels=True, # allow animation constraints=False, shader=False, expressions=True)
def export_gpu(self, outpath, cachepath, cachename, start, end, assemblies, attr_values): from reveries.maya import io, capsule from maya import cmds with contextlib.nested( capsule.no_undo(), capsule.no_refresh(), capsule.evaluation("off"), capsule.maintained_selection(), ): cmds.select(assemblies, replace=True, noExpand=True) with contextlib.nested( capsule.attribute_values(attr_values), # Mute animated visibility channels capsule.attribute_mute(list(attr_values.keys())), ): io.export_gpu(cachepath, start, end) io.wrap_gpu(outpath, [(cachename, "ROOT")])
def process(self, instance): from maya import cmds from reveries import utils from reveries.maya import xgen, capsule, utils as maya_utils staging_dir = utils.stage_dir() # Export preset # (NOTE) Saving as ext `.ma` instead of `.xgip` is because # I'd like to use reference to load it later. # Referencing file that was not `.ma`, `.mb` or other # normal ext will crash Maya on file saving. filename = "%s.ma" % instance.data["subset"] linkfile = "%s.json" % instance.data["subset"] outpath = "%s/%s" % (staging_dir, filename) linkpath = "%s/%s" % (staging_dir, linkfile) instance.data["repr.XGenInteractive._stage"] = staging_dir instance.data["repr.XGenInteractive._files"] = [filename, linkfile] instance.data["repr.XGenInteractive.entryFileName"] = filename instance.data["repr.XGenInteractive.linkFname"] = linkfile child_instances = instance.data.get("childInstances", []) try: texture = next(chd for chd in child_instances if chd.data["family"] == "reveries.texture") except StopIteration: file_node_attrs = dict() else: file_node_attrs = texture.data.get("fileNodeAttrs", dict()) bound_map = dict() clay_shader = "initialShadingGroup" descriptions = instance.data["igsDescriptions"] with capsule.assign_shader(descriptions, shadingEngine=clay_shader): for description in descriptions: desc_id = maya_utils.get_id(description) # Get bounded meshes bound_map[desc_id] = list() for mesh in xgen.interactive.list_bound_meshes(description): transform = cmds.listRelatives(mesh, parent=True, fullPath=True) id = maya_utils.get_id(transform[0]) bound_map[desc_id].append(id) # (NOTE) Separating grooms and bounding meshes seems not able to # preserve sculpt layer data entirely correct. # For example, sculpting long hair strands to really short, # may ends up noisy shaped after import back. # # So now we export the grooms with bound meshes... # # io.export_xgen_IGS_presets(descriptions, outpath) with contextlib.nested( capsule.no_display_layers(instance[:]), # Change to published path capsule.attribute_values(file_node_attrs), capsule.maintained_selection(), ): cmds.select(descriptions) cmds.file(outpath, force=True, typ="mayaAscii", exportSelected=True, preserveReferences=False, channels=True, constraints=True, expressions=True, constructionHistory=True) # Parse preset bounding map with open(linkpath, "w") as fp: json.dump(bound_map, fp, ensure_ascii=False)
def export_ass(self, nodes, outpath, file_node_attrs, has_yeti, start, end, step, expand_procedurals=True): from maya import cmds, mel from reveries.maya import arnold, capsule # Ensure option created arnold.utils.create_options() render_settings = { # Disable Auto TX update and enable to use existing TX "defaultArnoldRenderOptions.autotx": False, "defaultArnoldRenderOptions.use_existing_tiled_textures": True, # Ensure frame padding == 4 "defaultRenderGlobals.extensionPadding": 4, } # Yeti if has_yeti: # In Deadline, this is a script job instead of rendering job, so # the `pgYetiPreRender` Pre-Render MEL will not be triggered. # We need to call it by ourselve, or Yeti will complain about # cache temp dir not exist. mel.eval("pgYetiPreRender;") with contextlib.nested( capsule.no_undo(), capsule.no_refresh(), capsule.evaluation("off"), capsule.maintained_selection(), capsule.ref_edit_unlock(), # (NOTE) Ensure attribute unlock capsule.attribute_states(file_node_attrs.keys(), lock=False), # Change to published path capsule.attribute_values(file_node_attrs), # Fixed render settings capsule.attribute_values(render_settings), ): cmds.select(nodes, replace=True) asses = cmds.arnoldExportAss( filename=outpath, selected=True, startFrame=start, endFrame=end, frameStep=step, expandProcedurals=expand_procedurals, boundingBox=True, # Mask: # Shapes, # Shaders, # Override Nodes, # Operators, # # mask=4152, # No Color Manager mask=6200) # With Color Manager # Change to environment var embedded path root = avalon.api.registered_root().replace("\\", "/") project = avalon.api.Session["AVALON_PROJECT"] for ass in asses: lines = list() has_change = False with open(ass, "r") as assf: for line in assf.readlines(): if line.startswith(" filename "): line = line.replace(root, "[AVALON_PROJECTS]", 1) line = line.replace(project, "[AVALON_PROJECT]", 1) has_change = True lines.append(line) # Remove color manager # (NOTE): If Color Manager included, # may raise error if rendering # in Houdini or other DCC. try: s = lines.index("color_manager_syncolor\n") except ValueError: # No color manager found pass else: e = lines.index("}\n", s) + 1 lines = lines[:s] + lines[e:] has_change = True # Re-write if has_change: with open(ass, "w") as assf: assf.write("".join(lines))
def export_ass(data, start, end, step): arnold_tx_settings = { "defaultArnoldRenderOptions.autotx": False, "defaultArnoldRenderOptions.use_existing_tiled_textures": True, } # Yeti if data["hasYeti"]: # In Deadline, this is a script job instead of rendering job, so # the `pgYetiPreRender` Pre-Render MEL will not be triggered. # We need to call it by ourselve, or Yeti will complain about # cache temp dir not exist. mel.eval("pgYetiPreRender;") with contextlib.nested( capsule.no_undo(), capsule.no_refresh(), capsule.evaluation("off"), capsule.maintained_selection(), capsule.ref_edit_unlock(), # (NOTE) Ensure attribute unlock capsule.attribute_states(data["fileNodeAttrs"].keys(), lock=False), # Change to published path capsule.attribute_values(data["fileNodeAttrs"]), # Disable Auto TX update and enable to use existing TX capsule.attribute_values(arnold_tx_settings), ): cmds.select(data["member"], replace=True) asses = cmds.arnoldExportAss( filename=data["cachePath"], selected=True, startFrame=start, endFrame=end, frameStep=step, expandProcedurals=True, boundingBox=True, # Mask: # Shapes, # Shaders, # Override Nodes, # Operators, # # (NOTE): If Color Manager included, # may raise error if rendering # in Houdini or other DCC. # mask=6200, # With Color Manager # mask=4152) # No Color Manager # Change to environment var embedded path root = avalon.api.registered_root().replace("\\", "/") project = avalon.api.Session["AVALON_PROJECT"] for ass in asses: lines = list() has_change = False with open(ass, "r") as assf: for line in assf.readlines(): if line.startswith(" filename "): line = line.replace(root, "[AVALON_PROJECTS]", 1) line = line.replace(project, "[AVALON_PROJECT]", 1) has_change = True lines.append(line) if has_change: with open(ass, "w") as assf: assf.write("".join(lines))
def process(self, instance): from maya import cmds from avalon import maya from reveries import utils from reveries.maya import lib, capsule, utils as maya_utils staging_dir = utils.stage_dir() filename = "%s.ma" % instance.data["subset"] outpath = "%s/%s" % (staging_dir, filename) linkfile = "%s.json" % instance.data["subset"] linkpath = "%s/%s" % (staging_dir, linkfile) instance.data["repr.LookDev._stage"] = staging_dir instance.data["repr.LookDev._files"] = [filename, linkfile] instance.data["repr.LookDev.entryFileName"] = filename instance.data["repr.LookDev.linkFname"] = linkfile # Serialise shaders relationships # self.log.info("Serialising shaders..") shader_by_id = lib.serialise_shaders(instance.data["dagMembers"]) assert shader_by_id, "The map of shader relationship is empty." # Extract shaders # self.log.info("Extracting shaders..") child_instances = instance.data.get("childInstances", []) try: texture = next(chd for chd in child_instances if chd.data["family"] == "reveries.texture") except StopIteration: file_node_attrs = dict() else: file_node_attrs = texture.data.get("fileNodeAttrs", dict()) with contextlib.nested( maya.maintained_selection(), capsule.ref_edit_unlock(), # (NOTE) Ensure attribute unlock capsule.attribute_states(file_node_attrs.keys(), lock=False), # Change to published path capsule.attribute_values(file_node_attrs), capsule.no_refresh(), ): # Select full shading network # If only select shadingGroups, and if there are any node # connected to Dag node (i.e. drivenKey), then the command # will not only export selected shadingGroups' shading network, # but also export other related DAG nodes (i.e. full hierarchy) cmds.select(instance, replace=True, noExpand=True) cmds.file( outpath, options="v=0;", type="mayaAscii", force=True, exportSelected=True, preserveReferences=False, constructionHistory=False, channels=True, # allow animation constraints=False, shader=True, expressions=True) # Animatable attrs # Custom attributes in assembly node which require to be animated. self.log.info("Serialising 'avnlook_' prefixed attributes..") avnlook_anim = dict() for node in cmds.ls(instance.data["dagMembers"], type="transform"): id = maya_utils.get_id(node) user_attrs = cmds.listAttr(node, userDefined=True) or [] for attr in user_attrs: if not attr.startswith("avnlook_"): continue connected = cmds.listConnections(node + "." + attr, source=False, destination=True, plugs=True) if connected: avnlook_anim[id + "." + attr] = connected surfaces = cmds.ls(instance.data["dagMembers"], noIntermediate=True, type="surfaceShape") # UV Chooser uv_chooser = dict() for chooser in cmds.ls(instance, type="uvChooser"): chooser_id = maya_utils.get_id(chooser) for src in cmds.listConnections(chooser + ".uvSets", source=True, destination=False, plugs=True) or []: geo, attr = src.split(".", 1) geo = cmds.listRelatives(geo, parent=True, path=True)[0] geo_attr = maya_utils.get_id(geo) + "." + attr if chooser_id not in uv_chooser: uv_chooser[chooser_id] = list() if geo_attr not in uv_chooser[chooser_id]: uv_chooser[chooser_id].append(geo_attr) # CreaseSet crease_sets = dict() creases = list() for node in surfaces: creases += cmds.ls(cmds.listSets(object=node), type="creaseSet") creases = list(set(creases)) for cres in creases: # Grouping crease set members with crease level value. level = cmds.getAttr(cres + ".creaseLevel") if level not in crease_sets: crease_sets[level] = list() for member in cmds.ls(cmds.sets(cres, query=True), long=True): node, edges = member.split(".") if node not in instance.data["dagMembers"]: continue # We have validated Avalon UUID, so there must be a valid ID. id = maya_utils.get_id(node) crease_sets[level].append(id + "." + edges) # Arnold attributes arnold_attrs = dict() try: # (TODO) This should be improved. see issue #65 from reveries.maya import arnold except RuntimeError as e: self.log.debug(e) else: ai_sets = dict() for objset in cmds.ls(type="objectSet"): if not lib.hasAttr(objset, "aiOverride"): continue if not cmds.getAttr(objset + ".aiOverride"): continue # Ignore pyblish family instance if (lib.hasAttr(objset, "id") and read(objset + ".id") == "pyblish.avalon.instance"): continue ai_sets[objset] = cmds.ls(cmds.sets(objset, query=True), long=True) # (TODO) Validate only transform nodes in ai set transforms = cmds.ls(cmds.listRelatives(surfaces, parent=True), long=True) for node in transforms: # There must be a valid ID id = maya_utils.get_id(node) attrs = dict() # Collect all `ai*` attributes from shape shape = cmds.listRelatives( node, shapes=True, noIntermediate=True, fullPath=True) or [None] shape = shape[0] if shape is None: continue for attr in cmds.listAttr(shape, fromPlugin=True) or []: value = read(shape + "." + attr) if value is not None: attrs[attr] = value # Collect all override attributes from objectSet for ai_set, member in ai_sets.items(): if node not in member: continue for attr in cmds.listAttr(ai_set, userDefined=True) or []: # Collect all user attributes from objecSet # (NOTE) Some attribute like `castsShadows` does not # startswith "ai", but also affect rendering in # Arnold. value = read(node + "." + attr) if value is not None: attrs[attr] = value arnold_attrs[id] = attrs # VRay Attributes vray_attrs = dict() try: from reveries.maya import vray except RuntimeError as e: self.log.debug(e) else: for node in surfaces: # - shape values = vray.attributes_gather(node) if values: vray_attrs[node] = values # - transfrom parent = cmds.listRelatives(node, parent=True) if parent: values = vray.attributes_gather(parent[0]) if values: vray_attrs[parent[0]] = values relationships = { "shaderById": shader_by_id, "avnlookAttrs": avnlook_anim, "uvChooser": uv_chooser, "creaseSets": crease_sets, "arnoldAttrs": arnold_attrs, "vrayAttrs": vray_attrs, } self.log.info("Extracting serialisation..") with open(linkpath, "w") as f: json.dump(relationships, f)
def extract_LookDev(self, packager): from avalon import maya from reveries.maya import lib, capsule entry_file = packager.file_name("ma") package_path = packager.create_package() # Extract shaders # entry_path = os.path.join(package_path, entry_file) self.log.info("Extracting shaders..") try: texture = next(chd for chd in self.data.get("childInstances", []) if chd.data["family"] == "reveries.texture") except StopIteration: file_node_attrs = dict() else: file_node_attrs = texture.data.get("fileNodeAttrs", dict()) with contextlib.nested( maya.maintained_selection(), capsule.ref_edit_unlock(), # (NOTE) Ensure attribute unlock capsule.attribute_states(file_node_attrs.keys(), lock=False), # Change to published path capsule.attribute_values(file_node_attrs), capsule.no_refresh(), ): # Select full shading network # If only select shadingGroups, and if there are any node # connected to Dag node (i.e. drivenKey), then the command # will not only export selected shadingGroups' shading network, # but also export other related DAG nodes (i.e. full hierarchy) cmds.select(self.member, replace=True, noExpand=True) cmds.file( entry_path, options="v=0;", type="mayaAscii", force=True, exportSelected=True, preserveReferences=False, constructionHistory=False, channels=True, # allow animation constraints=False, shader=True, expressions=True) # Serialise shaders relationships # link_file = packager.file_name("json") link_path = os.path.join(package_path, link_file) self.log.info("Serialising shaders..") shader_by_id = lib.serialise_shaders(self.data["dagMembers"]) # Animatable attrs # Custom attributes in assembly node which require to be animated. self.log.info("Serialising animatable attributes..") animatable = dict() root = cmds.ls(self.data["dagMembers"], assemblies=True) if root: root = root[0] for attr in cmds.listAttr(root, userDefined=True) or list(): animatable[attr] = cmds.listConnections(root + "." + attr, destination=True, source=False, plugs=True) surfaces = cmds.ls(self.data["dagMembers"], noIntermediate=True, type="surfaceShape") # CreaseSet crease_sets = dict() creases = list() for node in surfaces: creases += cmds.ls(cmds.listSets(object=node), type="creaseSet") creases = list(set(creases)) for cres in creases: # Grouping crease set members with crease level value. level = cmds.getAttr(cres + ".creaseLevel") if level not in crease_sets: crease_sets[level] = list() for member in cmds.ls(cmds.sets(cres, query=True), long=True): node, edges = member.split(".") if node not in self.data["dagMembers"]: continue # We have validated Avalon UUID, so there must be a valid ID. id = utils.get_id(node) crease_sets[level].append(id + "." + edges) # Arnold attributes arnold_attrs = dict() try: # (TODO) This should be improved. see issue #65 from reveries.maya import arnold except RuntimeError as e: self.log.debug(e) else: ai_sets = dict() for objset in cmds.ls(type="objectSet"): if not lib.hasAttr(objset, "aiOverride"): continue if not cmds.getAttr(objset + ".aiOverride"): continue # Ignore pyblish family instance if (lib.hasAttr(objset, "id") and read(objset + ".id") == "pyblish.avalon.instance"): continue ai_sets[objset] = cmds.ls(cmds.sets(objset, query=True), long=True) # (TODO) Validate only transform nodes in ai set transforms = cmds.ls(cmds.listRelatives(surfaces, parent=True), long=True) for node in transforms: # There must be a valid ID id = utils.get_id(node) attrs = dict() # Collect all `ai*` attributes from shape shape = cmds.listRelatives( node, shapes=True, noIntermediate=True, fullPath=True) or [None] shape = shape[0] if shape is None: continue for attr in cmds.listAttr(shape, fromPlugin=True) or []: value = read(shape + "." + attr) if value is not None: attrs[attr] = value # Collect all override attributes from objectSet for ai_set, member in ai_sets.items(): if node not in member: continue for attr in cmds.listAttr(ai_set, userDefined=True) or []: # Collect all user attributes from objecSet # (NOTE) Some attribute like `castsShadows` does not # startswith "ai", but also affect rendering in # Arnold. value = read(node + "." + attr) if value is not None: attrs[attr] = value arnold_attrs[id] = attrs # VRay Attributes vray_attrs = dict() try: from reveries.maya import vray except RuntimeError as e: self.log.debug(e) else: for node in surfaces: # - shape values = vray.attributes_gather(node) if values: vray_attrs[node] = values # - transfrom parent = cmds.listRelatives(node, parent=True) if parent: values = vray.attributes_gather(parent[0]) if values: vray_attrs[parent[0]] = values relationships = { "shaderById": shader_by_id, "animatable": animatable, "creaseSets": crease_sets, "arnoldAttrs": arnold_attrs, "vrayAttrs": vray_attrs, } self.log.info("Extracting serialisation..") with open(link_path, "w") as f: json.dump(relationships, f) packager.add_data({ "linkFname": link_file, "entryFileName": entry_file, }) self.log.info("Extracted {name} to {path}".format( name=self.data["subset"], path=package_path))