How to invoke a tool from a shelf via Python scripting

   9242   11   5
User Avatar
Member
130 posts
Joined: 4月 2009
Offline
Hello,

Creating new nodes with Python is quite straightforward. Is there a way to use Python to call a tool from any of the shelves, that would create the whole subnet in one go? For example, is the following possible with one-liners in Python:
- add a camera
- convert an object into an RBD object

If the above is doable, is there a way to call from python an equivalent of Ctrl+Lclicking a tool - e.g. to place the camera at current viewpoint?

I'll appreciate any hints.

Greg
User Avatar
Member
194 posts
Joined:
Offline
have a look at the last 2 links:

http://www.sidefx.com/docs/houdini10.0/hom/cookbook/ [sidefx.com]
User Avatar
Member
130 posts
Joined: 4月 2009
Offline
Thanks, Whalerider,

I've checked these HOM examples (as well as the others), but I still fail to find an answer to the issue - I just can't see it in the example code. All of the examples seem to deal with processing individual network nodes, and I'm looking for a way to access a tool shelf.

If it's not a problem, could you show me the Python code required to create a camera? That would probably lead me to all other answers I'm looking for in this topic.

Cheers,
Greg
User Avatar
Member
130 posts
Joined: 4月 2009
Offline
Nevermind, I figured out the solution ;-)

Cheers,
G.
User Avatar
Member
1905 posts
Joined: 11月 2006
Offline
There is currently no real way to tell Houdini to invoke and existing tool like something on the shelf. Your best bet is to just look at the tools you want to use and copy their code into a tool of your own. Just RMB on a shelf tool and click Edit Tool to take a look at what makes it tick. You can then add those calls to your own tool.

There are ways to access elements from the shelves, and you could theoretically have your tool do that, find the script code that is called and then execute that, but this way is a lot simpler.

The following code does just what you've described. It creates a camera at the current view and allows you to select an object to become and RBD object. Since placing the camera down at the current view is handled by Ctrl or Cmd + LMB, we can force the kwargs args for those two options to be true ourselves and get the desired result.


import doptoolutils
import objecttoolutils

kwargs = True
kwargs = True
camera = objecttoolutils.genericCameraLightTool(kwargs, ‘cam’, ‘cam1’)

dop_object = doptoolutils.genericTool(kwargs, ‘rbdobject’)
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
130 posts
Joined: 4月 2009
Offline
Hi Graham,

Huge thanks!

While I managed to figure out the first part of your explanation myself, I gave up with Ctr/Cmd+LMB.

The “Edit Tool” is a fantastic learning tool indeed - helped me figure out that e.g. the new light created with python simply defaults to point, but I can change the type myself with one line of code code.

Thanks again,
Greg
User Avatar
Member
1905 posts
Joined: 11月 2006
Offline
For completeness sake, here's an example of getting the script code from an existing shelf tool and running it. You can leave out the whole loop/dictionary thing and just index directly into the list of tools, but this way lets you choose it by name, rather than list position which you might not know.
shelf = hou.shelves.shelves()

tools = {}
for t in shelf.tools():
tools = t

cam_script = tools.script()

kwargs = True
kwargs = True
exec(cam_script)
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
11 posts
Joined: 7月 2016
Offline
Hi Graham,

I know this is a old thread.

But I am trying to figure out how to run a shelf tool and I cannot work this out.

in the python shell I am trying to Run the .tool() and its not coming up with the tool tip like all other functions. even thought it is documented?

I am Running houdini 17 core.

basically, I just want to be able to call a shelf tool on startup so I am trying to figure out how to do this in python for the 123.py etc.

Cheers for your time.

Or appreciate anyone else help.
User Avatar
Member
11 posts
Joined: 11月 2006
Offline
danielsweeney
Hi Graham,

I know this is a old thread.

But I am trying to figure out how to run a shelf tool and I cannot work this out.

in the python shell I am trying to Run the .tool() and its not coming up with the tool tip like all other functions. even thought it is documented?

I am Running houdini 17 core.

basically, I just want to be able to call a shelf tool on startup so I am trying to figure out how to do this in python for the 123.py etc.

Cheers for your time.

Or appreciate anyone else help.

this works:

your_tool = hou.shelves.tool(“internal_tool_name”)
tool_script = your_tool.script()
exec(tool_script)

refer to this http://www.sidefx.com/docs/houdini/hom/hou/shelves.html [www.sidefx.com]
www.enoni.de
User Avatar
Member
620 posts
Joined: 11月 2013
Offline
hou.SceneViewer.runShelfTool(tool_name) can also do it.
User Avatar
Member
1 posts
Joined: 2月 2020
Offline
I tried to turn an object into RBD with this:

geo.setSelected(True, clear_all_selected=True)
tool = hou.shelves.tool(“dynamics_rbdobjects”)
toolScript=tool.script()
exec(toolScript)

and this:
doptoolutils.genericTool(kwargs, ‘rbdpackedobject’)

But they gave me this error.
assemble.setCreatorState(hou.shelves.runningTool().name())
AttributeError: ‘NoneType’ object has no attribute ‘name’

Is there any other way to turn an object into RBD with pyhton or why these codes are not working?
Thanks in advance.
User Avatar
Member
28 posts
Joined: 10月 2022
Offline
Hey, trying to create the karma material builder with python, but looks like the runShelfTool isnt working in the lop context, could someone give me an hint on how to do it? Thanks.

import hou

selection = hou.selectedNodes()[0] #materialLibrary
context = selection.parent()

context.runShelfTool("vop_karmamtlxsubnet")

#ERROR: AttributeError: 'LopNetwork' object has no attribute 'runShelfTool'
  • Quick Links