Q100499: Custom scripts execute multiple times on launch

SYMPTOMS

When executing custom Python scripts on Nuke launch, these scripts appear to be launched multiple times.  

An example can be seen when exporting a log file on launch, using the following Python commands in the init.py file:  

import logging
filePath = 'C:\Users\%USERNAME#\Documents\example.log'

logging.basicConfig(filename=filePath,level=logging.INFO)
logging.info("Logging from within init.py") logging.info("GUI: %s\n", nuke.env.get("gui"))

NOTE: Replace filePathwith your dedicated path on Windows, Linux or Mac, ending with a ‘.log’ filename like example.log. 

The example.log file output will show multiple entries and look like:

exampleLog.PNG

CAUSE

With the release of Nuke 11, the Frame Server has replaced the Background Render when rendering image sequences. The Frame Server launches additional render processes to reduce render times, which means that Nuke will launch additional processes depending on your 'frame server processes to run' settings. 

More information is available in the following article: Q100489: Multiple Nuke processes created on launch

Users have found that when the Frame Server is enabled, their custom Python scripts are launched multiple times which may cause problems if the script are expected to run only once.

RESOLUTION

Disabling the Frame Server removes the additional spawned Nuke processes and stops the execution of scripts from happening multiple times.

To continue using the Frame Server however and launch Python commands to be execute only on the main Nuke thread, the script commands can be set up in an ‘if’ statement, to run only if the nuke.env.get("gui") value returned is ‘True’.

This behaviour is demonstrated in the following code snippet which can be executed as part of the init.py file:

import logging 
isGui = nuke.env.get("gui")
filePath = 'C:\Users\%USERNAME#\Documents\example.log'

#example of print displayed multiple times per number of Frame Server processes
logging.basicConfig(filename=filePath,level=logging.INFO)
logging.info("Logging from within init.py") logging.info("GUI: %s\n", isGui)

#example of print forced to display only on main Nuke GUI thread if isGui == True : logging.info("------Running Script in GUI only------\n\n") 

NOTE: Replace filePathwith your dedicated path on Windows, Linux or Mac, ending with a ‘.log’ filename like example.log. 

The example.log file output will now look like:

exampleLog_1.PNG

The Python commands outside of the isGui test are still executed multiple times, however, the command inside the 'if' statement is only executed for the main GUI Nuke process.

FURTHER READING

For more information on accessing Nuke’s init.py file, please refer to Q100048: Nuke Directory Locations​​​

To learn more about rendering using the Frame Server, please refer to: Nuke Online Help - Rendering using the Frame Server

To find out how to render with the Frame Server via Python, please refer to: Q100461: How to render with the Frame Server using Python​​​

To set up Frame Server processes on external machines, please refer to: Nuke Online Help - Using the Frame Server on external machines