HDK
|
hscript
script is a scripting language interpreted by Houdini. For quick evaluation, you can type the hscript
commands in the Textport window in Houdini. The hscript
has many useful commands that you may want to invoke in the C++ code, for example to source own script file (using source
command) or to display a dialog box (using message
command). There is an CMD_Manager class to facilitate the script invocation, and its CMD_Manager::execute() method can be used to run the command.
The CMD_Manager::execute() signature looks like this:
At the minimum, you need to pass a string representing the hscript
command, which will be executed. If you provide the output stream, any output produced by the command will be written there. Also, if you pass in an error stream pointer, any error messages will be printed there.
The command manager instance is owned by the OP_Director and can be obtained by OP_Director::getCommandManager(). The OP_Director instance, in turn, is accessible via the global function OPgetDirector(). OP_Director class represents the root of the node hierarchy (ie, the node "/"), and therefore there is only one instance of this class. This class has several OP_Managers as its children.
In addition to being the root of the node hierarchy, OP_Director's role is to hold references to several other global instances of classes such as OP_CommandManager, OP_BundleList, OP_Take, OP_OTLManager. Also, OP_Director maintains some global states, such as currently selected nodes OP_Director::getPickedNodes() or current working directory OP_Director::getCwd(), which represents the path of a node the director considers to be the current.
The current working directory is particularly relevant for executing hscript
commands because some of them operate with respect to that directory. For example opadd
will create a new node inside the current working directory. You can use pwd
and cd
commands to print and change the current directory. You can also use the C++ methods OP_Director::getCwd() and OP_Director::setCwd() to achieve the same thing.
Let's say that we want to pop up a message dialog and detect which button was pressed by the user. The hscript
command that displays the dialog is called message
, and its -b
switch defines the buttons (separated by a comma). The -d
switch specifies the default button.
We will run the message
command and retrieve the button index from the output stream. To obtain the command manager object, we query the global director.
Now, we need to parse the output stream and determine what number was printed out. We obtain the string using UT_OStrStream::str()
but, after that, we have to unlock the buffer using the freeze(0)
method.
And now we can provide feedback to test if the code works correctly.