Skip to content

Instantly share code, notes, and snippets.

@kramester
Last active July 13, 2023 11:40
Show Gist options
  • Save kramester/9c50df656668c4c973a1a3f47a4b8299 to your computer and use it in GitHub Desktop.
Save kramester/9c50df656668c4c973a1a3f47a4b8299 to your computer and use it in GitHub Desktop.
Bootstrapping tk-nuke to deadline instances
## Add this to your init.py that sits at the root of your
## NUKE_PATH. Your NUKE_PATH env variable should be set
## on all the render nodes so they automatically look here
import os
import nuke
# If this instance of nuke is a deadline render, bootstrap shotgun
if os.environ.get('DEADLINE_RENDER'):
nuke.pluginAddPath('./shotgun_bootstrap')
# rename this to init.py in your setup. Github wouldnt let me have a gist with 2 identical filenames.
# This init.py would be placed in a shotgun_bootstrap folder next you your main init.py in the NUKE PATH
import sys
import os
# If your render nodes can access the same tk-core install location as
# artist workstations, retrieve its path from the environment and ensure
# it is in the PYTHONPATH
TK_CORE_PATH = os.environ["SHOTGUN_SGTK_MODULE_PATH"]
if TK_CORE_PATH not in sys.path:
sys.path.append(TK_CORE_PATH)
import sgtk
# Authenticate using a pre-defined script user.
sa = sgtk.authentication.ShotgunAuthenticator()
# Here we retrieve credentials from environment variables, assuming a script user
# will be used when rendering. This should be typically be handled by your render
# farm administrators.
SG_SITE_URL = os.environ["SHOTGUN_SITE"]
SG_SCRIPT_USER = os.environ["SHOTGUN_FARM_SCRIPT_USER"]
SG_SCRIPT_KEY = os.environ["SHOTGUN_FARM_SCRIPT_KEY"]
user = sa.create_script_user(
api_script=SG_SCRIPT_USER,
api_key=SG_SCRIPT_KEY,
host=SG_SITE_URL
)
# Start up a Toolkit Manager with our script user
mgr = sgtk.bootstrap.ToolkitManager(sg_user=user)
# Set the base pipeline configuration from the environment variable:
mgr.base_configuration = os.environ["SHOTGUN_CONFIG_URI"]
# Disable Shotgun lookup to ensure that we are getting the Pipeline
# Configuration defined in SHOTGUN_CONFIG_URI, and not a dev or override
# Pipeline Configuration defined in Shotgun.
mgr.do_shotgun_config_lookup = False
# Set a plugin id to indicate to the bootstrap that we are starting
# up a standard Nuke integration
mgr.plugin_id = "basic.nuke"
# Retrieve the Toolkit context from environment variables:
# SHOTGUN_SITE: The Shotgun site url
# SHOTGUN_ENTITY_TYPE: The Shotgun Entity type, e.g. Shot
# SHOTGUN_ENTITY_ID: The Shotgun Entity id, e.g. 1234
sg_entity = mgr.get_entity_from_environment()
# Now start up the Nuke engine for a given Shotgun Entity
# nuke_engine = mgr.bootstrap_engine("tk-nuke", entity=sg_entity)
####### START OF WORKAROUND #######
# This workaround may not be necessary with the latest
# code updates to tk's frameworks, but i havnt tried it yet.
# If it is fixed, can remove code below, and just
# uncomment the "nuke_engine" line above
# bootstrap into the project context first
SG_PROJECT_ID = int(os.environ["SHOTGUN_PROJECT_ID"])
nuke_engine = mgr.bootstrap_engine("tk-nuke", entity={'type': 'Project', 'id': SG_PROJECT_ID})
# sync filesystem structure first
nuke_engine.sgtk.synchronize_filesystem_structure()
# get the context we want
ctx = nuke_engine.sgtk.context_from_entity_dictionary(sg_entity)
# change to the context we want
nuke_engine.change_context(ctx)
# this code needs to be inserted into your SubmitNukeToDeadline script if you are using deadline's submission script.
###### INSERT AFTER THIS LINE, line 1308 in my file
#if groupBatch or dialog.separateJobs.value():
# fileHandle.write(EncodeAsUTF16String("BatchName=%s\n" % batchName))
# Populating environment variables from running Nuke:
# http://developer.shotgunsoftware.com/tk-core/platform.html#sgtk.platform.create_engine_launcher
current_engine = sgtk.platform.current_engine()
launcher = sgtk.platform.create_engine_launcher(
current_engine.sgtk,
current_engine.context,
current_engine.name
)
# Get a dictionary with the following keys:
# SHOTGUN_SITE: The Shotgun site url
# SHOTGUN_ENTITY_TYPE: The Shotgun Entity type, e.g. Shot
# SHOTGUN_ENTITY_ID: The Shotgun Entity id, e.g. 1234
environment = launcher.get_standard_plugin_environment()
# Get the current pipeline config descriptor
environment["SHOTGUN_CONFIG_URI"] = current_engine.sgtk.configuration_descriptor.get_uri()
# Get the current tk-core installation path
# using the env variable SHOTGUN_BUNDLE_CACHE_PATH that we've set for our studio
# environment, I puzzle together the path to the tk-core libraries.
BUNDLE_CACHE_PATH = os.environ['SHOTGUN_BUNDLE_CACHE_PATH']
environment["SHOTGUN_SGTK_MODULE_PATH"] = os.path.join(BUNDLE_CACHE_PATH, 'app_store/tk-core', current_engine.sgtk.version, 'python')
# Get the project ID for a workaround during bootstrapping
environment["SHOTGUN_PROJECT_ID"] = current_engine.context.project['id']
# write out shotgun env variables so we can bootstrap shotgun on the render slaves
env_index = 0
for k, v in environment.iteritems():
if v is not None:
fileHandle.write(EncodeAsUTF16String("EnvironmentKeyValue{0}={1}={2!s}\n".format(env_index, k, v)))
env_index += 1
else:
pass
# set this env variable on the job we send to deadline so that
# the init.py in the NUKE_PATH knows this is a deadline render and we need to
# bootstrap shotgun
fileHandle.write(EncodeAsUTF16String("EnvironmentKeyValue{0}={1}={2!s}\n".format(env_index, "DEADLINE_RENDER", "1")))
###### INSERT BEFORE THIS LINE
#fileHandle.close()
@andybclarke
Copy link

Hi, I've been looking for a solution to this for so long! I've tried implementing your solution above but I'm getting errors, any chance you could help? Many thanks:

Preparing job #1 for submission..
Exception in thread Thread-17:
Traceback (most recent call last):
File "/Applications/Nuke12.1v1/Nuke12.1v1.app/Contents/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/Applications/Nuke12.1v1/Nuke12.1v1.app/Contents/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Volumes/library/library/DeadlineRepository10/submission/Nuke/Main/SubmitNukeToDeadline.py", line 1537, in SubmitJob
current_engine = sgtk.platform.current_engine()
NameError: global name 'sgtk' is not defined

@kramester
Copy link
Author

you just need to add this line to the top of your SubmitNukeToDeadline.py:
import sgtk

this assumes that your launching the deadline submitter from a bootstrapped nuke session which already has access to sgtk of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment