Q100351: How to query system Graph State data from inside a C++ Op to react to time related values

Follow

SUMMARY

This article will explain how system arguments of the current Graph State, like the current frame time or the number of samples, can be accessed from a custom Op. 

If you are looking for information on how to write and compile a custom Op, please see Q100326: How to write and test your first custom Op.

 

MORE INFORMATION

A common stumbling block when implementing custom Ops is that cook interface functions returning current time (shutterOpen or shutterClose values) or the number of time samples, may not return the correct result unless the node whose Op chain the custom Op is part of, is set up correctly.

This is relevant, for example, for calls to:

Foundry::Katana::GetCurrentTime(interface)
Foundry::Katana::GetShutterOpen(interface)
Foundry::Katana::GetShutterClose(interface)
Foundry::Katana::GetNumSamples(interface)

To be able to get the expected return value from these functions, it’s necessary to add the System Op Args. It's typically the responsibility of the node (or hosting Op via interface.execOp) to provide these.

If the Op is instantiated via the GenericOp node, its addSystemOpArgs parameter needs to be set to Yes.

If the Op is registered via a custom NodeTypeBuilder, the interface object sent to buildOpChain() has an addOpSystemArgs() method which will add a 'system' GroupAttribute to the Op args. The Op can then retrieve and use this.

NOTE: The relevant NodeTypeBuilder function can be found in the Katana Developer Guide.

 

EXAMPLE IMPLEMENTATION

Attached is an example of a simple NodeTypeBuilder based node type, that creates a single location at /root/world/geo/hello_world of type "hello world", and runs the HelloWorld Op on it. (In this example setting some attributes at /root).

The Op source needs to be compiled before it is ready to use. To do this, please follow the instructions in the $KATANA_ROOT/plugins/Src/README.md file included in the Katana installation directory.

For more detailed guidance, please see Q100326: How to write and test your first custom Op.

 

Once the C++ source is compiled into a .so or .dll file, this file needs to be placed in an Ops sub-folder of a KATANA_RESOURCES directory, and NTB.py needs to be placed in a Plugins sub-folder of a KATANA_RESOURCES directory.

For more information on setting up KATANA_RESOURCES, please see the Katana Installation Guide.

The relevant code to add the 'system' Op arg when appending the Op to the node's Op chain, is the following:

opArgs = FnAttribute.GroupBuilder()
interface.addOpSystemArgs(opArgs)

interface.appendOp('HelloWorld', opArgs.build())

 

This is equivalent to doing the following:

graphState = interface.getGraphState()
opArgs = FnAttribute.GroupBuilder()
opArgs.set('system', graphState.getOpSystemArgs())
interface.appendOp('HelloWorld', opArgs.build())

 

The 'HelloWorld' Op can access this and, for example, set the current time as an attribute at root:

static void cook(Foundry::Katana::GeolibCookInterface& interface)
{
    if (interface.atRoot())
    {
        interface.setAttr("Hello", FnAttribute::StringAttribute("World!"));
        interface.setAttr("CurrentTime", FnAttribute::FloatAttribute(Foundry::Katana::GetCurrentTime(interface)));
    }
    interface.stopChildTraversal();
}

 

TECHNICAL EXPLANATION

GraphState::getOpSystemArgs() returns a group containing the 'timeslice' group ('currentTime', 'shutterOpen', 'shutterClose', 'numSamples') and the 'variables' group (graph state variables). This group should be set as the 'system' Op arg; CookInterface calls such as GetCurrentTime and GetGraphStateVariables are convenience functions that call getOpArg("system.[...]").

 

FURTHER HELP

If you are encountering any issues with the information contained in this article, 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 Q100064: How to raise a support ticket.

 

ATTACHMENTS

We're sorry to hear that

Please tell us why