Houdini 18.0 タスクを実行する方法

Event Handling


On this page


PDG provides an event handling mechanism that can be used to run custom Python code when things happen during the cook. Event hooks are available for fine-grained changes in work items such as a state changes or dependency modifications, as well as more granular graph events like cook completion. The full list of events is documented in the pdg.EventType API reference.

To register an event handler you first need to have a reference to either a pdg.Node, pdg.WorkItem or a pdg.GraphContext. The Node and Graph Context can be accessed from a hou.TopNode once the TOP graph has been cooked. A work item can be found by looking it up using the workItemId on the pdg.Event with pdg.GraphContext.workItemById.

import hou
import pdg

topnode_path = '/tasks/topnet1/genericgenerator1'
top_node = hou.node(topnode_path)

# Ensure the TOP graph has been cooked so that we can
# access the underlying PDG graph
top_node.executeGraph(False, False, False, True)

pdg_node = top_node.getPDGNode()
pdg_context = top_node.getPDGGraphContext()

The API for registering an event handler is the same on node and graph context objects. Each handler is create by defining a function that takes a pdg.Event as its first argument:

# Define a function to handle a work item event
def print_add(event):
    work_item = pdg_context.graph.workItemById(event.workItemId)
    print ("Work item '{}' was added to node {}".format(work_item.name, event.node.name))
    # Add a listener for state changes on this new work item

    def print_workitem_change(event):
        work_item = pdg_context.graph.workItemById(event.workItemId)
        print ('State Change for {}: {} -> {}'.format(work_item.name, event.lastState, event.currentState))
    work_item.addEventHandler(print_workitem_change, pdg.EventType.WorkItemStateChange)

# Define a function to handle a graph level event
def print_done(event):
    print ("Graph cook complete!")

# Register the handlers
node_handler = pdg_node.addEventHandler(print_add, pdg.EventType.WorkItemAdd)
context_handler = pdg_context.addEventHandler(print_done, pdg.EventType.CookComplete)

The best place to put event registration code depends on your set up. One option is to place the code in a shelf tool that registers the handlers when the tool is clicked.

Removing Handlers

Sometimes you may wish to remove event handlers after they have finished executing. One way to do this is to call the removeEventHandler method on the object that was initially used to add the handler:


It is also possible to remove a handler from within the event function itself. To do so you’ll need to include the extra pass_handler=True argument when registering the handler function. This is so that the event function knows which handler it is associated with it. Inside the event function itself you can then call the pdg.EventHandler.removeFromAllEmitters method on the handler:

def print_done_and_remove(handler, event):
    print ("Graph cook complete!")

    # Deregister the handler from all events its listening to

pdg_context.addEventHandler(print_done_and_remove, pdg.EventType.CookComplete, True)

Events that Trigger UI Changes

PDG events are processed in the background on the same cook thread that emitted them. This means that generally speaking it isn’t safe to start another cook, change parameters or open UI dialogs from an event handler function. In some cases you can perform these actions by the using the hdefereval module to ensure that your code runs on the main thread instead in the background:

import hdefereval

# Define a function for displaying our message
def display_message(message):
    import hou
    hou.ui.displayMessage(message, title="Example Message from PDG")

# Define our event handler
def display_event(event):
    hdefereval.executeDeferred(display_message, "An example message created in"
        " a PDG Event but displayed from the main thread!")

pdg_context.addEventHandler(display_event, pdg.EventType.CookComplete)


The only event that can safely recook the current PDG/TOP graph is the CookComplete event. Event handler functions are able to cook other TOP networks, but must use the hdefereval module to do so.




  • 外部のプログラムを実行する方法


  • ファイルタグ


  • フィードバックループ


  • コマンドサーバー


  • PDG Service Manager

    PDG Service Managerは、ワークアイテムのクック時間を短くするために使用される持続型Houdiniセッションのプールを管理します。

  • PDGとレンダーファームスケジューラの統合


  • ワークアイテムのパフォーマンスの可視化


  • Event Handling


  • Tipsとテクニック



  • すべてのTOPsノード


  • Processor Node Callbacks


  • Partitioner Node Callbacks


  • Scheduler Node Callbacks


  • Python API

    ディペンデンシーグラフを扱うためのPython PDGパッケージのクラスと関数。