Houdini 20.5 Python scripting

Python state Drag and Drop

How to set up your state to respond to drag drop actions.

On this page

Overview

Houdini lets you turn the state into a drag drop receiver. This enables the state to react when users drag and drop something onto the scene viewer.

Handling Drag and Drop events

As a result of the drag event initiated by the user, Houdini will call 3 callbacks sequentially to handle the event. They are all mandatory and must be implemented for handling the event properly.

In order to access the information related to the drag drop event, you need to use these APIs:

The drag drop event callbacks are listed below in order of execution.

Name

Notes

onDragTest

Called by Houdini when an element is dragged into the viewer. The goal of this callback is to decide whether we are interested of handling the event or not. Returning True accepts the event and False rejects it. The event will be submittted to the next Houdini drag drop receivers if rejected.

onDropGetOptions

This callback is called when the dragged element is dropped onto the view. It lets you build a list of options representing the different drop actions to perform for this specific event. The callback is called only if onDragTest has accepted the event.

Houdini uses the option list to populate and display a menu for selecting a drop option. The menu is not displayed however if only one option was added. In this case the lone option is selected by default.

The dictionary passed to this callback contains these items:

drop_options

A dictionary representing the drop option list.

ids

An entry in the drop_options dictionary for storing a list of option identifiers.

labels

An entry in the drop_options dictionary for storing a list of option labels.

onDropAccept

Houdini calls this method with the selected drop option. The method must return True to accept the selection or False to reject it. The drag drop operation is aborted if the option is rejected.

The dictionary passed to this callback contains the following:

drop_selection

The selected option.

Here’s a small code snippet to demonstrate the drag drop support. Check out the $HH/viewer_states/examples/state_dragdrop_demo.hip scene for a more detailed example.

import hou
import viewerstate.utils as su

class State(object):
    def __init__(self, state_name, scene_viewer):
        self.state_name = state_name
        self.scene_viewer = scene_viewer

    def onEnter(self,kwargs):
        self.scene_viewer.setPromptMessage( 
            'Drop a source file in the viewer', hou.promptMessageType.Prompt )

    def onDragTest( self, kwargs ):
        """ Accept text files only """

        if not hou.ui.hasDragSourceData('text/plain'):
            self.scene_viewer.setPromptMessage( 'Invalid drag drop source', 
                hou.promptMessageType.Error )
            return False

        # note: su.dragSourceFilepath returns the sanitized dragged file path
        su.log(su.dragSourceFilepath())

        return True

    def onDropGetOptions( self, kwargs ):
        """ Populate a drop option list with 3 items """

        kwargs['drop_options']['ids'] = ('option1', 'option2', 'option3')
        kwargs['drop_options']['labels'] = ('Option 1', 'Option 2', 'Option 3')

    def onDropAccept( self, kwargs ):
        """ Process the event with the selected option. """

        su.log( kwargs['drop_selection'] )

        return True        

Python scripting

Getting started

Next steps

Reference

  • hou

    Module containing all the sub-modules, classes, and functions to access Houdini.

Guru level

Python viewer states

Python viewer handles

Plugin types