Skip to content

pipeline

MayaHost

Bases: HostBase, IWorkfileHost, ILoadHost, IPublishHost

Source code in client/ayon_maya/api/pipeline.py
 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
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
class MayaHost(HostBase, IWorkfileHost, ILoadHost, IPublishHost):
    name = "maya"

    def __init__(self):
        super(MayaHost, self).__init__()
        self._op_events = {}

    def install(self):
        project_name = get_current_project_name()
        project_settings = get_project_settings(project_name)
        # process path mapping
        dirmap_processor = MayaDirmap("maya", project_name, project_settings)
        dirmap_processor.process_dirmap()

        pyblish.api.register_plugin_path(PUBLISH_PATH)
        pyblish.api.register_host("mayabatch")
        pyblish.api.register_host("mayapy")
        pyblish.api.register_host("maya")

        register_loader_plugin_path(LOAD_PATH)
        register_creator_plugin_path(CREATE_PATH)
        register_inventory_action_path(INVENTORY_PATH)
        register_workfile_build_plugin_path(WORKFILE_BUILD_PATH)

        self.log.info("Installing callbacks ... ")
        register_event_callback("init", on_init)

        _set_project()

        if lib.IS_HEADLESS:
            self.log.info((
                "Running in headless mode, skipping Maya save/open/new"
                " callback installation.."
            ))

            return

        self._register_callbacks()

        menu.install(project_settings)

        register_event_callback("save", on_save)
        register_event_callback("open", on_open)
        register_event_callback("new", on_new)
        register_event_callback("before.save", on_before_save)
        register_event_callback("after.save", on_after_save)
        register_event_callback("before.close", on_before_close)
        register_event_callback("before.file.open", before_file_open)
        register_event_callback("taskChanged", on_task_changed)
        register_event_callback("workfile.open.before", before_workfile_open)
        register_event_callback("workfile.save.before", before_workfile_save)
        register_event_callback(
            "workfile.save.before", workfile_save_before_xgen
        )
        register_event_callback("workfile.save.after", after_workfile_save)

        self._register_maya_usd_chasers()

    def open_workfile(self, filepath):
        return open_file(filepath)

    def save_workfile(self, filepath=None):
        return save_file(filepath)

    def work_root(self, session):
        return work_root(session)

    def get_current_workfile(self):
        return current_file()

    def workfile_has_unsaved_changes(self):
        return has_unsaved_changes()

    def get_workfile_extensions(self):
        return file_extensions()

    def get_containers(self):
        return ls()

    @contextlib.contextmanager
    def maintained_selection(self):
        with lib.maintained_selection():
            yield

    def get_context_data(self):
        data = cmds.fileInfo("OpenPypeContext", query=True)
        if not data:
            return {}

        data = data[0]  # Maya seems to return a list
        decoded = base64.b64decode(data).decode("utf-8")
        return json.loads(decoded)

    def update_context_data(self, data, changes):
        json_str = json.dumps(data)
        encoded = base64.b64encode(json_str.encode("utf-8"))
        return cmds.fileInfo("OpenPypeContext", encoded)

    def _register_callbacks(self):
        for handler, event in self._op_events.copy().items():
            if event is None:
                continue

            try:
                OpenMaya.MMessage.removeCallback(event)
                self._op_events[handler] = None
            except RuntimeError as exc:
                self.log.info(exc)

        self._op_events[_on_scene_save] = OpenMaya.MSceneMessage.addCallback(
            OpenMaya.MSceneMessage.kBeforeSave, _on_scene_save
        )

        self._op_events[_after_scene_save] = (
            OpenMaya.MSceneMessage.addCallback(
                OpenMaya.MSceneMessage.kAfterSave,
                _after_scene_save
            )
        )

        self._op_events[_before_scene_save] = (
            OpenMaya.MSceneMessage.addCheckCallback(
                OpenMaya.MSceneMessage.kBeforeSaveCheck,
                _before_scene_save
            )
        )

        self._op_events[_on_scene_new] = OpenMaya.MSceneMessage.addCallback(
            OpenMaya.MSceneMessage.kAfterNew, _on_scene_new
        )

        self._op_events[_on_maya_initialized] = (
            OpenMaya.MSceneMessage.addCallback(
                OpenMaya.MSceneMessage.kMayaInitialized,
                _on_maya_initialized
            )
        )

        self._op_events[_on_scene_open] = (
            OpenMaya.MSceneMessage.addCallback(
                OpenMaya.MSceneMessage.kAfterOpen,
                _on_scene_open
            )
        )

        self._op_events[_before_scene_open] = (
            OpenMaya.MSceneMessage.addCallback(
                OpenMaya.MSceneMessage.kBeforeOpen,
                _before_scene_open
            )
        )

        self._op_events[_before_close_maya] = (
            OpenMaya.MSceneMessage.addCallback(
                OpenMaya.MSceneMessage.kMayaExiting,
                _before_close_maya
            )
        )

        self.log.info("Installed event handler _on_scene_save..")
        self.log.info("Installed event handler _before_scene_save..")
        self.log.info("Installed event handler _on_after_save..")
        self.log.info("Installed event handler _on_scene_new..")
        self.log.info("Installed event handler _on_maya_initialized..")
        self.log.info("Installed event handler _on_scene_open..")
        self.log.info("Installed event handler _check_lock_file..")
        self.log.info("Installed event handler _before_close_maya..")

    def _register_maya_usd_chasers(self):
        """Register Maya USD chasers if Maya USD libraries are available."""

        try:
            import mayaUsd.lib  # noqa
        except ImportError:
            # Do not register if Maya USD is not available
            return

        self.log.info("Installing AYON Maya USD chasers..")

        from .chasers import export_filter_properties  # noqa

        for export_chaser in [
            export_filter_properties.FilterPropertiesExportChaser
        ]:
            mayaUsd.lib.ExportChaser.Register(export_chaser,
                                              export_chaser.name)

before_file_open()

check lock file when the file changed

Source code in client/ayon_maya/api/pipeline.py
596
597
598
599
def before_file_open():
    """check lock file when the file changed"""
    # delete the lock file
    _remove_workfile_lock()

check_lock_on_current_file()

Check if there is a user opening the file

Source code in client/ayon_maya/api/pipeline.py
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def check_lock_on_current_file():

    """Check if there is a user opening the file"""
    if not handle_workfile_locks():
        return
    log.info("Running callback on checking the lock file...")

    # add the lock file when opening the file
    filepath = current_file()
    # Skip if current file is 'untitled'
    if not filepath:
        return

    if is_workfile_locked(filepath):
        # add lockfile dialog
        workfile_dialog = WorkfileLockDialog(filepath)
        if not workfile_dialog.exec_():
            cmds.file(new=True)
            return

    create_workfile_lock(filepath)

containerise(name, namespace, nodes, context, loader=None, suffix='CON')

Bundle nodes into an assembly and imprint it with metadata

Containerisation enables a tracking of version, author and origin for loaded assets.

Parameters:

Name Type Description Default
name str

Name of resulting assembly

required
namespace str

Namespace under which to host container

required
nodes list

Long names of nodes to containerise

required
context dict

Asset information

required
loader str

Name of loader used to produce this container.

None
suffix str

Suffix of container, defaults to _CON.

'CON'

Returns:

Name Type Description
container str

Name of container assembly

Source code in client/ayon_maya/api/pipeline.py
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
@lib.undo_chunk()
def containerise(name,
                 namespace,
                 nodes,
                 context,
                 loader=None,
                 suffix="CON"):
    """Bundle `nodes` into an assembly and imprint it with metadata

    Containerisation enables a tracking of version, author and origin
    for loaded assets.

    Arguments:
        name (str): Name of resulting assembly
        namespace (str): Namespace under which to host container
        nodes (list): Long names of nodes to containerise
        context (dict): Asset information
        loader (str, optional): Name of loader used to produce this container.
        suffix (str, optional): Suffix of container, defaults to `_CON`.

    Returns:
        container (str): Name of container assembly

    """
    container = cmds.sets(nodes, name="%s_%s_%s" % (namespace, name, suffix))

    data = [
        ("schema", "openpype:container-2.0"),
        ("id", AVALON_CONTAINER_ID),
        ("name", name),
        ("namespace", namespace),
        ("loader", loader),
        ("representation", context["representation"]["id"]),
        ("project_name", context["project"]["name"])
    ]
    for key, value in data:
        cmds.addAttr(container, longName=key, dataType="string")
        cmds.setAttr(container + "." + key, str(value), type="string")

    main_container = cmds.ls(AVALON_CONTAINERS, type="objectSet")
    if not main_container:
        main_container = cmds.sets(empty=True, name=AVALON_CONTAINERS)

        # Implement #399: Maya 2019+ hide AVALON_CONTAINERS on creation..
        if cmds.attributeQuery("hiddenInOutliner",
                               node=main_container,
                               exists=True):
            cmds.setAttr(main_container + ".hiddenInOutliner", True)
    else:
        main_container = main_container[0]

    cmds.sets(container, addElement=main_container)

    # Implement #399: Maya 2019+ hide containers in outliner
    if cmds.attributeQuery("hiddenInOutliner",
                           node=container,
                           exists=True):
        cmds.setAttr(container + ".hiddenInOutliner", True)

    return container

ls()

Yields containers from active Maya scene

This is the host-equivalent of api.ls(), but instead of listing assets on disk, it lists assets already loaded in Maya; once loaded they are called 'containers'

Yields:

Type Description

dict[str, Any]: container

Source code in client/ayon_maya/api/pipeline.py
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
def ls():
    """Yields containers from active Maya scene

    This is the host-equivalent of api.ls(), but instead of listing
    assets on disk, it lists assets already loaded in Maya; once loaded
    they are called 'containers'

    Yields:
        dict[str, Any]: container

    """
    container_names = _ls()
    for container in sorted(container_names):
        container_data = parse_container(container)
        if container_data:
            yield container_data

on_after_save()

Check if there is a lockfile after save

Source code in client/ayon_maya/api/pipeline.py
559
560
561
def on_after_save():
    """Check if there is a lockfile after save"""
    check_lock_on_current_file()

on_before_close()

Delete the lock file after user quitting the Maya Scene

Source code in client/ayon_maya/api/pipeline.py
587
588
589
590
591
592
593
def on_before_close():
    """Delete the lock file after user quitting the Maya Scene"""
    log.info("Closing Maya...")
    # delete the lock file
    filepath = current_file()
    if handle_workfile_locks():
        remove_workfile_lock(filepath)

on_before_save()

Run validation for scene's FPS prior to saving

Source code in client/ayon_maya/api/pipeline.py
554
555
556
def on_before_save():
    """Run validation for scene's FPS prior to saving"""
    return lib.validate_fps()

on_new()

Set project resolution and fps when create a new file

Source code in client/ayon_maya/api/pipeline.py
658
659
660
661
662
663
664
def on_new():
    """Set project resolution and fps when create a new file"""
    log.info("Running callback on new..")
    with lib.suspended_refresh():
        lib.set_context_settings()

    _remove_workfile_lock()

on_open()

On scene open let's assume the containers have changed.

Source code in client/ayon_maya/api/pipeline.py
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
def on_open():
    """On scene open let's assume the containers have changed."""

    from ayon_core.tools.utils import SimplePopup

    # Validate FPS after update_task_from_path to
    # ensure it is using correct FPS for the folder
    lib.validate_fps()
    lib.fix_incompatible_containers()

    if any_outdated_containers():
        log.warning("Scene has outdated content.")

        # Find maya main window
        parent = lib.get_main_window()
        if parent is None:
            log.info("Skipping outdated content pop-up "
                     "because Maya window can't be found.")
        else:

            # Show outdated pop-up
            def _on_show_inventory():
                host_tools.show_scene_inventory(parent=parent)

            dialog = SimplePopup(parent=parent)
            dialog.setWindowTitle("Maya scene has outdated content")
            dialog.set_message("There are outdated containers in "
                              "your Maya scene.")
            dialog.on_clicked.connect(_on_show_inventory)
            dialog.show()

    # create lock file for the maya scene
    check_lock_on_current_file()

on_save()

Automatically add IDs to new nodes

Any transform of a mesh, without an existing ID, is given one automatically on file save.

Source code in client/ayon_maya/api/pipeline.py
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
def on_save():
    """Automatically add IDs to new nodes

    Any transform of a mesh, without an existing ID, is given one
    automatically on file save.
    """
    log.info("Running callback on save..")
    # remove lockfile if users jumps over from one scene to another
    _remove_workfile_lock()

    # Generate ids of the current context on nodes in the scene
    nodes = lib.get_id_required_nodes(referenced_nodes=False,
                                      existing_ids=False)
    for node, new_id in lib.generate_ids(nodes):
        lib.set_id(node, new_id, overwrite=False)

    # We are now starting the actual save directly
    global _about_to_save
    _about_to_save = False

on_task_changed()

Wrapped function of app initialize and maya's on task changed

Source code in client/ayon_maya/api/pipeline.py
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
def on_task_changed():
    """Wrapped function of app initialize and maya's on task changed"""
    # Run
    menu.update_menu_task_label()

    workdir = os.getenv("AYON_WORKDIR")
    if os.path.exists(workdir):
        log.info("Updating Maya workspace for task change to %s", workdir)
        _set_project()

        # Set Maya fileDialog's start-dir to /scenes
        frule_scene = cmds.workspace(fileRuleEntry="scene")
        cmds.optionVar(stringValue=("browserLocationmayaBinaryscene",
                                    workdir + "/" + frule_scene))

    else:
        log.warning((
            "Can't set project for new context because path does not exist: {}"
        ).format(workdir))

    global _about_to_save
    if not lib.IS_HEADLESS and _about_to_save:
        # Let's prompt the user to update the context settings or not
        lib.prompt_reset_context()

parse_container(container)

Return the container node's full container data.

Parameters:

Name Type Description Default
container str

A container node name.

required

Returns:

Type Description

Optional[dict[str, Any]]: The container schema data for this container if it meets all the required metadata.

Source code in client/ayon_maya/api/pipeline.py
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def parse_container(container):
    """Return the container node's full container data.

    Args:
        container (str): A container node name.

    Returns:
        Optional[dict[str, Any]]: The container schema data for this container
         if it meets all the required metadata.

    """
    data = lib.read(container)

    required = ["id", "name", "namespace", "loader", "representation"]
    missing = [key for key in required if key not in data]
    if missing:
        log.warning("Container '%s' is missing required keys: %s",
                    container, missing)
        return

    # Backwards compatibility pre-schemas for containers
    data["schema"] = data.get("schema", "openpype:container-1.0")

    # Append transient data
    data["objectName"] = container

    return data

workfile_save_before_xgen(event)

Manage Xgen external files when switching context.

Xgen has various external files that needs to be unique and relative to the workfile, so we need to copy and potentially overwrite these files when switching context.

Source code in client/ayon_maya/api/pipeline.py
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
def workfile_save_before_xgen(event):
    """Manage Xgen external files when switching context.

    Xgen has various external files that needs to be unique and relative to the
    workfile, so we need to copy and potentially overwrite these files when
    switching context.

    Args:
        event (Event) - ayon_core/lib/events.py
    """
    if not cmds.pluginInfo("xgenToolkit", query=True, loaded=True):
        return

    import xgenm

    current_work_dir = os.getenv("AYON_WORKDIR").replace("\\", "/")
    expected_work_dir = event.data["workdir_path"].replace("\\", "/")
    if current_work_dir == expected_work_dir:
        return

    palettes = cmds.ls(type="xgmPalette", long=True)
    if not palettes:
        return

    transfers = []
    overwrites = []
    attribute_changes = {}
    attrs = ["xgFileName", "xgBaseFile"]
    for palette in palettes:
        sanitized_palette = palette.replace("|", "")
        project_path = xgenm.getAttr("xgProjectPath", sanitized_palette)
        _, maya_extension = os.path.splitext(event.data["filename"])

        for attr in attrs:
            node_attr = "{}.{}".format(palette, attr)
            attr_value = cmds.getAttr(node_attr)

            if not attr_value:
                continue

            source = os.path.join(project_path, attr_value)

            attr_value = event.data["filename"].replace(
                maya_extension,
                "__{}{}".format(
                    sanitized_palette.replace(":", "__"),
                    os.path.splitext(attr_value)[1]
                )
            )
            target = os.path.join(expected_work_dir, attr_value)

            transfers.append((source, target))
            attribute_changes[node_attr] = attr_value

        relative_path = xgenm.getAttr(
            "xgDataPath", sanitized_palette
        ).split(os.pathsep)[0]
        absolute_path = relative_path.replace("${PROJECT}", project_path)
        for root, _, files in os.walk(absolute_path):
            for f in files:
                source = os.path.join(root, f).replace("\\", "/")
                target = source.replace(project_path, expected_work_dir + "/")
                transfers.append((source, target))
                if os.path.exists(target):
                    overwrites.append(target)

    # Ask user about overwriting files.
    if overwrites:
        log.warning(
            "WARNING! Potential loss of data.\n\n"
            "Found duplicate Xgen files in new context.\n{}".format(
                "\n".join(overwrites)
            )
        )
        return

    for source, destination in transfers:
        if not os.path.exists(os.path.dirname(destination)):
            os.makedirs(os.path.dirname(destination))
        shutil.copy(source, destination)

    for attribute, value in attribute_changes.items():
        cmds.setAttr(attribute, value, type="string")