13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 | class ExtractMayaSceneRaw(plugin.MayaExtractorPlugin, AYONPyblishPluginMixin):
"""Extract as Maya Scene (raw).
This will preserve all references, construction history, etc.
"""
label = "Maya Scene (Raw)"
families = ["mayaAscii",
"mayaScene",
"setdress",
"layout",
"camerarig"]
scene_type = "ma"
@classmethod
def get_attribute_defs(cls):
return [
BoolDef(
"preserve_references",
label="Preserve References",
tooltip=(
"When enabled references will still be references "
"in the published file.\nWhen disabled the references "
"are imported into the published file generating a "
"file without references."
),
default=True
)
]
def process(self, instance):
"""Plugin entry point."""
maya_settings = instance.context.data["project_settings"]["maya"]
ext_mapping = {
item["name"]: item["value"]
for item in maya_settings["ext_mapping"]
}
if ext_mapping:
self.log.debug("Looking in settings for scene type ...")
# use extension mapping for first family found
for family in self.families:
try:
self.scene_type = ext_mapping[family]
self.log.debug(
"Using {} as scene type".format(self.scene_type))
break
except KeyError:
# no preset found
pass
# Define extract output file path
dir_path = self.staging_dir(instance)
filename = "{0}.{1}".format(instance.name, self.scene_type)
path = os.path.join(dir_path, filename)
# Whether to include all nodes in the instance (including those from
# history) or only use the exact set members
members_only = instance.data.get("exactSetMembersOnly", True)
if members_only:
members = instance.data.get("setMembers", list())
if not members:
raise RuntimeError("Can't export 'exact set members only' "
"when set is empty.")
else:
members = instance[:]
# For some families, like `layout` we collect the containers so we
# maintain the containers of the members in the resulting product.
# However, if `exactSetMembersOnly` is true (which it is for layouts)
# searching the exact set members for containers doesn't make much
# sense. We must always search the full hierarchy to actually find
# the relevant containers
selection = list(members) # make a copy to not affect input list
if set(self.add_for_families).intersection(
set(instance.data.get("families", []))) or \
instance.data.get("productType") in self.add_for_families:
containers = self._get_loaded_containers(instance[:])
self.log.debug(f"Collected containers: {containers}")
selection.extend(containers)
# Perform extraction
self.log.debug("Performing extraction ...")
attribute_values = self.get_attr_values_from_data(
instance.data
)
with maintained_selection():
cmds.select(selection, noExpand=True)
with contextlib.ExitStack() as stack:
if not instance.data.get("shader", True):
# Fix bug where export without shader may import the geometry 'green'
# due to the lack of any shader on import.
stack.enter_context(shader(selection, shadingEngine="initialShadingGroup"))
cmds.file(path,
force=True,
typ="mayaAscii" if self.scene_type == "ma" else "mayaBinary",
exportSelected=True,
preserveReferences=attribute_values["preserve_references"],
constructionHistory=True,
shader=instance.data.get("shader", True),
constraints=True,
expressions=True)
if "representations" not in instance.data:
instance.data["representations"] = []
representation = {
'name': self.scene_type,
'ext': self.scene_type,
'files': filename,
"stagingDir": dir_path
}
instance.data["representations"].append(representation)
self.log.debug("Extracted instance '%s' to: %s" % (instance.name,
path))
@staticmethod
def _get_loaded_containers(members):
# type: (list[str]) -> list[str]
refs_to_include = {
cmds.referenceQuery(node, referenceNode=True)
for node in members
if cmds.referenceQuery(node, isNodeReferenced=True)
}
members_with_refs = refs_to_include.union(members)
obj_sets = cmds.ls("*.id", long=True, type="objectSet", recursive=True,
objectsOnly=True)
loaded_containers = []
for obj_set in obj_sets:
if not cmds.attributeQuery("id", node=obj_set, exists=True):
continue
id_attr = "{}.id".format(obj_set)
if cmds.getAttr(id_attr) not in {
AYON_CONTAINER_ID, AVALON_CONTAINER_ID
}:
continue
set_content = set(cmds.sets(obj_set, query=True) or [])
if set_content.intersection(members_with_refs):
loaded_containers.append(obj_set)
return loaded_containers
|