[Houdini][Python] Get the current opened context?

Hi,

I have this code that creates new node:
new_node = hou.node('/obj').createNode('node_name')

Is there a way to retrieve the current opened context so that this part changes depending on the opened context?
hou.node('/obj')

Are you looking for hou.pwd()?

1 Like

Thanks for the response @BigRoyNL
but it somehow always returns the topmost context.

For example, whether I am at hou.node('/obj') or at hou.node('/obj/geo1')
it always gives me a the following hou.node
<hou.Node at />

Am I missing something?

How do you mean - where you are ā€˜currentlyā€™ at?

When calling hou.pwd() from e.g. a node callback thatā€™s when it returns the current node as that is the node executing the logic. For example you could use it within a Python SOP node. (which I think it actually also does by default when creating a node).

Getting the current ā€œcontextā€ from the UI however is slightly more involved. This is due to how you can e.g. have multiple network views open that each point to a different context. Which one would you want? :slight_smile:

Where and when are you trying to retrieve the current context? And what would your expected result be?

Ah gotcha. Iā€™m not using a Python SOP node.

My use case is creating a custom PySide UI with buttons for my commonly modified nodes.
When I click the buttons, it creates a node on the currently opened context.

For testing, Iā€™m using the Python Shell which unfortunately gives me the <hou.Node at /> result.

You could try and get the active pane in Houdini in a multitude of ways. Potentially hou.Desktop.paneTabUnderCursor or hou.Desktop.paneUnderCursor could help?

Once you have the right pane and it is a PathBasePaneTab or alike you can do pane.pwd().

Does this point you into the right direction?

Basically youā€™d need to get explicit as to which specific UI element of Houdini you want to take the current context from - since you might have multiple.

1 Like

Thanks for the response.

This will sound stupid for asking this, so sorry in advance. But I canā€™t use the paneTabUnderCursor or paneUnderCursor.

It gives me an error of
missing 1 required positional argument: 'self'

I checked the documentation and it does not accept arguments. Iā€™m guessing self here needs to be implemented in a Class.

I tried that too like literraly
print (hou.Desktop.paneUnderCursor(self))

but it gives me another error of
TypeError: in method 'Desktop_paneUnderCursor', argument 1 of type 'HOM_Desktop *'

Itā€™s a method on a class. The Desktop class should first be instantiated as an instance - thatā€™s why you are getting the error.

What that means in this case is that you will need to get the active desktop first with hou.ui.curDesktop

desktop = hou.ui.curDesktop()
desktop.paneUnderCursor()

There might also be multiple active desktops apparently, see hou.ui.desktops.

Iā€™m not behind a machine with Houdini currently - so not entirely sure this is the way to use it.

1 Like

Ah gotcha. Thanks for sticking through me.
It now works as expected :slight_smile:

Hereā€™s the code I used for reference:

desktop = hou.ui.curDesktop()
pane =  desktop.paneTabUnderCursor()
current_context = pane.pwd()
current_context.createNode('node_name')