Maya open latest scene

As our first task, we implement an automatic scene open in Maya upon launch. It will check if there is a previous snapshot published on the task, if not it tries to locate a template scene, based on the task, to load and start from.

Prerequisites

  1. A shot created in ftrack, with proper timeline and fps set.

  2. The previous custom location plugin deployed, and configured storage scenario set up (preferable).

  3. A Maya template scene to use when no previous published snapshot file exists, present in project folder @ _TEMPLATES/maya/<task type>_template.mb

Implementation

All DCC tools goes into the file custom_commands.py:

mypipeline/ftrack-connect-pipeline-maya/source/ftrack_connect_pipeline_maya/utils/custom_commands.py

 1# :coding: utf-8
 2# :copyright: Copyright (c) 2022 ftrack
 3import logging
 4import os
 5import threading
 6from functools import wraps
 7import shutil
 8
 9from Qt import QtWidgets, QtCompat
10
11import ftrack_api
12
13
14def scene_open(session, logger):
15    '''Load latest scene, or generate new from template.'''
16    context_id = os.getenv('FTRACK_CONTEXTID')
17    task = session.query('Task where id={}'.format(context_id)).one()
18    path_snapshot_open = path_snapshot_load = None
19    path_snapshot, message = get_save_path(
20        context_id, session, extension='.mb', temp=True
21    )
22    location = session.pick_location()
23    for version in session.query(
24        'AssetVersion where '
25        'task.id={} order by date descending'.format(
26            task['id'],
27        )
28    ).all():
29        # Find a snapshot
30        component = session.query(
31            'Component where '
32            'name="snapshot" and version.id={}'.format(version['id'])
33        ).first()
34        if component:
35            try:
36                path_snapshot_open = location.get_filesystem_path(component)
37            except ftrack_api.exception.ComponentNotInLocationError as e:
38                cmds.confirmDialog(message=str(e))
39
40    if path_snapshot_open is None:
41        # Copy Maya template scene
42        path_template = os.path.join(
43            location.accessor.prefix,
44            task['project']['name'],
45            '_TEMPLATES',
46            'maya',
47            '{}_template.mb'.format(task['type']['name'].lower()),
48        )
49        if os.path.exists(path_template):
50            logger.info(
51                'Copying Maya template {} to {}'.format(
52                    path_template, path_snapshot
53                )
54            )
55            shutil.copy(path_template, path_snapshot)
56            path_snapshot_load = path_snapshot
57        else:
58            logger.warning(
59                'Maya template not found @ {}!'.format(path_template)
60            )
61    else:
62        # Make a copy in temp
63        logger.info(
64            'Copying most recent snapshot {} to {}'.format(
65                path_snapshot_open, path_snapshot
66            )
67        )
68        shutil.copy(path_snapshot_open, path_snapshot)
69        path_snapshot_load = path_snapshot
70
71    if path_snapshot_load:
72        # Load the scene
73        logger.info('Loading scene: {}'.format(path_snapshot_load))

We are not going into detail what the scene_open function does, but it tries to locate a previous published snapshot and if not found - a new one is copied from a template and saved to the temp folder and opened.

Finally, to have this run during Maya startup, we add it to userSetup.py:

mypipeline/ftrack-connect-pipeline-maya/resources/scripts/userSetup.py

1def initialise():
2    ..
3
4    maya_utils.init_maya()
5
6    maya_utils.scene_open(session, logger)