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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 | 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"
# Defined by settings
add_for_families: list[str] = []
@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"]
}
scene_type: str = self.scene_type
# Use `families` for lookup in extension mapping and add for families
families = [instance.data["productType"]]
families.extend(instance.data.get("families", []))
# use extension mapping for first family found
for family in families:
if family in ext_mapping:
self.log.debug(
f"Using '{scene_type}' as scene type for '{family}'"
)
scene_type = ext_mapping[family]
break
else:
families_label = ", ".join(set(families))
self.log.debug(
f"Using default '{scene_type}' as scene type because no "
"extension mapping settings found for families: "
f"{families_label}."
)
# Define extract output file path
dir_path = self.staging_dir(instance)
filename = "{0}.{1}".format(instance.name, 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
add_for_families = set(self.add_for_families)
if add_for_families and add_for_families.intersection(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)
file_type = "mayaAscii" if scene_type == "ma" else "mayaBinary"
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=file_type,
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": scene_type,
"ext": 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
|