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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 | class ReferenceLoader(plugin.ReferenceLoader):
"""Reference file"""
product_types = {
"model",
"pointcache",
"proxyAbc",
"animation",
"mayaAscii",
"mayaScene",
"setdress",
"layout",
"camera",
"rig",
"camerarig",
"staticMesh",
"skeletalMesh",
"mvLook",
"matchmove",
}
representations = {"ma", "abc", "fbx", "mb"}
label = "Reference"
order = -10
icon = "code-fork"
color = "orange"
def process_reference(self, context, name, namespace, options):
import maya.cmds as cmds
product_type = context["product"]["productType"]
project_name = context["project"]["name"]
# True by default to keep legacy behaviours
attach_to_root = options.get("attach_to_root", True)
group_name = options["group_name"]
# no group shall be created
if not attach_to_root:
group_name = namespace
kwargs = {}
if "file_options" in options:
kwargs["options"] = options["file_options"]
if "file_type" in options:
kwargs["type"] = options["file_type"]
path = self.filepath_from_context(context)
with maintained_selection():
cmds.loadPlugin("AbcImport.mll", quiet=True)
file_url = self.prepare_root_value(path, project_name)
nodes = cmds.file(file_url,
namespace=namespace,
sharedReferenceFile=False,
reference=True,
returnNewNodes=True,
groupReference=attach_to_root,
groupName=group_name,
**kwargs)
shapes = cmds.ls(nodes, shapes=True, long=True)
new_nodes = (list(set(nodes) - set(shapes)))
# if there are cameras, try to lock their transforms
self._lock_camera_transforms(new_nodes)
current_namespace = cmds.namespaceInfo(currentNamespace=True)
if current_namespace != ":":
group_name = current_namespace + ":" + group_name
self[:] = new_nodes
settings = get_project_settings(project_name)
if attach_to_root:
group_name = "|" + group_name
roots = cmds.listRelatives(group_name,
children=True,
fullPath=True) or []
if product_type not in {
"layout", "setdress", "mayaAscii", "mayaScene"
}:
# QUESTION Why do we need to exclude these families?
with parent_nodes(roots, parent=None):
cmds.xform(group_name, zeroTransformPivots=True)
color = plugin.get_load_color_for_product_type(
product_type, settings
)
if color is not None:
red, green, blue = color
cmds.setAttr("{}.useOutlinerColor".format(group_name), 1)
cmds.setAttr(
"{}.outlinerColor".format(group_name),
red,
green,
blue
)
display_handle = settings['maya']['load'].get(
'reference_loader', {}
).get('display_handle', True)
if display_handle:
self._set_display_handle(group_name)
if product_type == "rig":
options["lock_instance"] = (
settings
["maya"]
["load"]
["reference_loader"]
["lock_animation_instance_on_load"]
)
self._post_process_rig(namespace, context, options)
else:
if "translate" in options:
if not attach_to_root and new_nodes:
root_nodes = cmds.ls(new_nodes, assemblies=True,
long=True)
# we assume only a single root is ever loaded
group_name = root_nodes[0]
cmds.setAttr("{}.translate".format(group_name),
*options["translate"])
return new_nodes
def switch(self, container, context):
self.update(container, context)
def update(self, container, context):
with preserve_modelpanel_cameras(container, log=self.log):
super(ReferenceLoader, self).update(container, context)
# We also want to lock camera transforms on any new cameras in the
# reference or for a camera which might have changed names.
members = get_container_members(container)
self._lock_camera_transforms(members)
def remove(self, container):
representation_id: str = container["representation"]
project_name: str = container.get(
"project_name", get_current_project_name()
)
product_type = None
if representation_id:
context: dict = get_representation_context(
project_name, representation_id
)
product_type: str = context["product"]["productType"]
if product_type == "rig":
# Special handling needed for rig containers
self._remove_rig(container)
return
super().remove(container)
def _remove_rig(self, container):
"""Remove linked animation instance no matter if it
is locked or not.
Args:
container (dict): The container to remove.
"""
members = get_container_members(container)
object_sets = set()
for member in members:
object_sets.update(
cmds.listSets(object=member, extendToShape=False) or []
)
super().remove(container)
# After the deletion, we check which object sets are still existing
# because maya may auto-delete empty object sets if they are not locked
# This way we can clean up remaining animation instances that were
# locked
object_sets = cmds.ls(object_sets, type="objectSet")
for object_set in object_sets:
# Only consider empty object sets
members = cmds.sets(object_set, query=True)
if members:
continue
# Only consider locked object sets
locked = cmds.lockNode(object_set, query=True)
if not locked:
continue
# Ignore referenced object sets
if cmds.referenceQuery(isNodeReferenced=object_set):
continue
# Then only here confirm whether this is an animation instance, if so
# then we will want to auto-remove the instance
if get_creator_identifier(object_set) == "io.openpype.creators.maya.animation":
cmds.lockNode(object_set, lock=False)
cmds.delete(object_set)
def _post_process_rig(self, namespace, context, options):
nodes = self[:]
try:
create_rig_animation_instance(
nodes, context, namespace, options=options, log=self.log,
)
except RigSetsNotExistError as exc:
self.log.warning(
"Missing rig sets for animation instance creation: %s", exc)
def _lock_camera_transforms(self, nodes):
cameras = cmds.ls(nodes, type="camera")
if not cameras:
return
# Check the Maya version, lockTransform has been introduced since
# Maya 2016.5 Ext 2
version = int(cmds.about(version=True))
if version >= 2016:
for camera in cameras:
cmds.camera(camera, edit=True, lockTransform=True)
else:
self.log.warning("This version of Maya does not support locking of"
" transforms of cameras.")
def _set_display_handle(self, group_name: str):
"""Enable display handle and move select handle to object center"""
cmds.setAttr(f"{group_name}.displayHandle", True)
# get bounding box
# Bugfix: We force a refresh here because there is a reproducable case
# with Advanced Skeleton rig where the call to `exactWorldBoundingBox`
# directly after the reference without it breaks the behavior of the
# rigs making it appear as if parts of the mesh are static.
# TODO: Preferably we have a better fix than requiring refresh on loads
cmds.refresh()
bbox = cmds.exactWorldBoundingBox(group_name)
# get pivot position on world space
pivot = cmds.xform(group_name, q=True, sp=True, ws=True)
# center of bounding box
cx = (bbox[0] + bbox[3]) / 2
cy = (bbox[1] + bbox[4]) / 2
cz = (bbox[2] + bbox[5]) / 2
# add pivot position to calculate offset
cx += pivot[0]
cy += pivot[1]
cz += pivot[2]
# set selection handle offset to center of bounding box
cmds.setAttr(f"{group_name}.selectHandleX", cx)
cmds.setAttr(f"{group_name}.selectHandleY", cy)
cmds.setAttr(f"{group_name}.selectHandleZ", cz)
|