This article intends to give an introduction on how we can integrate Maya’s mayapy into your FPTR pipelines using the FPTR Toolkit shell engine to automate workflows running in headless modes.
Mayapy
Mayapy is Maya’s own python interpreter which runs from the command line. It comes in handy when doing batch processing tasks or when accessing Maya’s libraries from external applications.
Scripting in Mayapy
To use Maya Python from within Mayapy, we load and initialize Maya libraries into the interpreter as we will be running this script from outside the Maya script editor.
try:
import maya.standalone
maya.standalone.initialize()
except:
pass
Once Initialized, we can add logic to create our object (in this case, a cup) that we shall later publish as a maya scene file.
import maya.cmds as cmds
def create_cup():
# Create a polygonal cylinder to represent the cup
cup = cmds.polyCylinder(radius=2, height=4, name='cup')[0]
cmds.xform(cup, centerPivots=True)
handle = cmds.polyCylinder(radius=0.5, height=3, name='handle')[0]
cmds.xform(handle, translation=(0, 2, 1.5), rotation=(90, 0, 0))
cmds.parent(handle, cup)
return cup
we can then add some logic to define the file paths to save our file:
def save_scene(file_path):
cmds.file(rename=file_path)
cmds.file(save=True, type='mayaBinary')
Having done this, we can proceed to the bootstrapping phase of the FPTR Toolkit Engine.
Bootstrapping and running an App in FPTR
Bootstrapping, in the context of Flow production Tracking refers to the process of initializing or setting up the Toolkit environment within a particular software application. A Toolkit Engine is essentially a component of Toolkit that allows users to interact with the Toolkit platform within different digital content creation software applications. Each engine is specific to a particular application, enabling Toolkit to integrate seamlessly into various DCC (Digital Content Creation) tools.
FPTR provides a bootstrap API that allows for real-time handling of Toolkit configurations, therefore removing the traditional step of a project setup. This makes it possible to launch an engine using a configuration (https://developers.FPTRsoftware.com/tk-core/descriptor.html#descriptor).
To start, we need to import an sgtk API package from tk-core. This can be done from an existing project or also you could download a standalone copy from GitHub - shotgunsoftware/tk-core: The Flow Production Tracking Pipeline Toolkit Core API .
Code
import sys
sys.path.insert(0, "/path/to/tk-core/python")
import sgtk
authenticator = sgtk.authentication.ShotgunAuthenticator()
user = authenticator.create_script_user(
api_script="Script Name",
api_key=">",
host="https://yoursite.shotgunstudio.com"
)
sgtk.set_authenticated_user(user)
There are typically several ways to authenticate, depending on use case, in our case we use script-based authentication but you’re free to explore user-based authentication.
Since our goal is to run the toolkit code in a standalone python environment separate from the common DCC software like Maya, Nuke…, we will use the tk-shell engine.
Next, we define a Toolkit manager, a project entity and configuration as shown below:
mgr = sgtk.bootstrap.ToolkitManager(user)
mgr.plugin_id = "basic.shell"
project = {"type": "Project", "id": 176}
engine = mgr.bootstrap_engine("tk-shell", entity=project)
With this done, we should be ready to launch an app, or run a few supported commands in the toolkit engine within the current engine context (tk-shell).
Launching a FPTR software entity
Up to this point, we have prepared our code for bootstrapping the tk-shell engine and also a separate code for creating a cup in Maya and saving it as a scene file.
To use the current engine to run mayapy, we need to have mayapy added to FPTR web as a software entity. This can be done by following this guide and appending the path to your mayapy installation folder.
It is however important to note that, for this to work seamlessly, we need to specify tk-shell as the engine when adding the entity as shown below
Arguments
We need to be able to pass in a path to our python script for mayapy to execute. This can be done either statically or dynamically. To pass a static path, we add the script path to the Windows args when defining the software entity and it will be passed whenever we call the engine.execute_command command.
The second way would be to add the path dynamically to the hook_app_launch.py file of the tk_multi_lauchapp app as an environment variable, overwriting the ‘app_args’ variable.
Tk-multi-launchapp
Next we proceed to make a modification to the prepare-apps file contained within the tk-multi-launchapp folder by adding the following condition
elif engine_name == "tk-shell":
return (app_path, app_args)
to this chain inside prepare_apps file located within the tk-multi-launchapp folder.
We do this to avoid running into the following error below as the tk-shell doesn’t have a default launcher.
`[ERROR sgtk.env.project.tk-shell.tk-multi-launchapp] No bootstrap routine found for tk-shell. The engine will not be started.`
tk-multi-launchapp: No bootstrap routine found for tk-shell. The engine will not be started.
With this setup, we should be ready to bootstrap into the tk-shell engine and invoke the engine.execute_command to run mayapy, running our python code to create a cup and save it as a scene file inside our project directory.
More resources
The samples above do not cover all aspects of bootstrapping the toolkit engine. The complete guide can be found on this documentation at
The bootstrap API offers several more options to integrate and automate your studio workflows and deployments. More information can be found here: FPTR Developer Help Center
Complete script
import sys
sys.path.insert(0, "/path/to/tk-core/python")
import sgtk
authenticator = sgtk.authentication.ShotgunAuthenticator()
user = authenticator.create_script_user(
api_script="Script Name",
api_key=">",
host="https://yoursite.fptr.studio.com"
)
sgtk.set_authenticated_user(user)
mgr = sgtk.bootstrap.ToolkitManager(user)
mgr.plugin_id = "basic.shell"
project = {"type": "Project", "id": 176}
engine = mgr.bootstrap_engine("tk-shell", entity=project)
engine.execute_command("mayapy", [])
