Search Tips

Search is based on keyword.

`Ex: "Procedures"`

Do not search with natural language

`Ex: "How do I write a new procedure?"`

Give Feedback Support Portal

# Q100439: Modifying the global Graph State Variables on startup

Warning: This article contains links to external websites

SUMMARY
Launching Katana with global Graph State Variables set predefines accessible information about the context of the Katana project, which can then be accessed by the node graph or custom tools. Likewise, artists can have the predefined variables set such as file paths to shows or project folders. This article will guide the steps on modifying a Katana project’s global Graph State Variables on Katana startup.

For information regarding Graph State Variables please look at our Katana User Guide: Graph State Variables.

The approach involves using Katana startup scripts and callbacks working together to set the Graph State Variables.

MORE INFORMATION
A Katana startup script is a file named "init.py" located in the Startup folder, under a path defined in the KATANA_RESOURCES environment variable. Alternatively, you can use a startup script in the form of an "init.py" file placed in the “.katana” folder in your $HOME directory. The Python code in "init.py" will run on Katana startup.

The following is an example of how to set the global Graph State variable via Python, for example by running it in the Python tab in Katana’s GUI:

    def AddGlobalGraphStateVariable(name, options):
        variablesGroup = NodegraphAPI.GetRootNode().getParameter('variables')
        variableParam = variablesGroup.createChildGroup(name)
        variableParam.createChildNumber('enable', 1)
        variableParam.createChildString('value', options[0])
        optionsParam = variableParam.createChildStringArray('options', len(options))
        for optionParam, optionValue in zip(optionsParam.getChildren(), options):
            optionParam.setValue(optionValue, 0)
        return variableParam.getName()

However, when trying to se t the global Graph State Variable directly through the "init.py" script one will quickly find that the changes to the Graph State Variable did not hold.

In order to set a Graph State Variable on a project, that project (including the new, blank project that’s loaded when starting up a fresh Katana session) needs to be loaded to access the root node, where global Graph State Variables are stored along with other project settings. The startup script runs before the project has loaded in a Katana session. Consequently, "init.py" can't directly access the Global State Variables.

To solve this, callbacks are needed in the startup script. A callback is a piece of Python code added to the Katana environment that runs automatically when various events (such as creating a node or loading a script) happen in Katana. In this case, we need to set onStartupComplete or onSceneLoad callback to run a function that sets the global Graph State Variable right after startup or project load. For more information about callbacks please see the Knowledge Base article, Q100385: Callbacks and event handlers in Katana.

Here is the resulting example "init.py" that sets the global Graph State Variable on Katana launch. (The "init.py" script is borrowed from the sample script provided by the KATANA_RESOURCES/plugins/Src/Resources/Examples/Startup folder):

    from Katana import Callbacks
    import logging

    def onStartupComplete(**kwargs):
        log = logging.getLogger("Startup Example")
        import NodegraphAPI
        name = "Variable Name"
        options = ('Option 1','Option 2','Option 3')

        variablesGroup = NodegraphAPI.GetRootNode().getParameter('variables')
        variableParam = variablesGroup.createChildGroup(name)
        variableParam.createChildNumber('enable', 1)
        variableParam.createChildString('value', options[0])
        optionsParam = variableParam.createChildStringArray('options', len(options))
        for optionParam, optionValue in zip(optionsParam.getChildren(), options):
                optionParam.setValue(optionValue, 0)
        log.info("Katana is now fully initialized and ready for use.")

    log = logging.getLogger("Startup Example")
    log.info("Registering onStartupComplete callback...")
    Callbacks.addCallback(Callbacks.Type.onStartupComplete, onStartupComplete)

The example “init.py” startup script is attached below.

If you have further questions, or if you are having any trouble working with using startup scripts to set the global Graph State Variables, then please open a Support ticket and let us know the issue you are encountering and the troubleshooting steps you have taken so far.

For more information on how to open a Support ticket, please refer to the 'Using the Support Portal' article.

FURTHER READING

- Katana User Guide: Graph State Variables
- Katana User Guide: Katana Resources
- Q100385: Callbacks and event handlers in Katana

ATTACHMENT

Attachments

- init.py(2 KB)