Below is a snippet of code using the sgtk API to get a context, resolve a template path into an absolute path, and then publish that path.
The code doesn’t actually create a file at the generated path, so you would need to implement that part. For example to tell Maya to export a file to the generated path.
(We have a more detailed step by step guide here.)
import sgtk
# Get the engine we are currently running in.
# (This assumes the code is running in an environment where an engine has already been started.)
current_engine = sgtk.platform.current_engine()
# Grab the sgtk API instance.
tk = current_engine.sgtk
# grab the current context from the engine.
ctx = current_engine.context
# Or alternatively create a new context representing the task we want to publish to.
# ctx = tk.context_from_entity("Task",13153)
# Create the folders just in case they haven't already been created.
tk.create_filesystem_structure("Task",ctx.task["id"])
#### Resolving the path ####
# Get a template instance for for the template we want to resolve into a path.
template = tk.templates["maya_shot_publish"]
# Use the context to resolve as many of the template fields as possible.
fields = ctx.as_template_fields(template)
# Manually fill in the remaining fields that can't be figured out automatically from context.
fields["name"] = "mypublish"
fields["version"] = 2
# Resolve the template path using the field values.
publish_path = template.apply_fields(fields)
#### Registering the Publish File ####
publish_name = fields["name"]
version = fields["version"]
# Register the path as a publish.
sgtk.util.register_publish(tk,
ctx,
publish_path,
publish_name,
version,
published_file_type = "Maya Scene")