Python - set callback for dynamically created parms

   421   2   0
User Avatar
Member
16 posts
Joined: 7月 2020
Offline
Hey, I'm kida stuck on how to do this - in short, i've got a shelf tool that adds an ordered menu UI to selected nodes. And I need a callback when the selection changes in this menu. Easy with static UI, but I couldn't figure it out with dynamically created one. This is a simplified code:

import hou
node = hou.selectedNodes()[0]

menu_items = ["aaa", "bbb", "ccc", ]

if node.parm("select_texture_node") is None:
    ordered_menu_parm = hou.MenuParmTemplate(
        name="select_texture_node",
        label="Select Texture Node",
        menu_items=menu_items,
        script_callback=hou.MenuParmTemplate.setScriptCallback(node.parm("select_texture_node"), print('pressed'))    
        )
        
    new_parm_group = node.parmTemplateGroup()
    
    new_parm_group.addParmTemplate(ordered_menu_parm)
    
    node.setParmTemplateGroup(new_parm_group)

This line is obviously a wrong syntax that crashes Houdini:
script_callback=hou.MenuParmTemplate.setScriptCallback(node.parm("select_texture_node"), print('pressed'))

Not sure how it should look to make it work. Docs not helping. ChatGPT was giving me BS on this one. Any hints?

thanks, D.
User Avatar
Member
9143 posts
Joined: 7月 2007
Offline
try this
...
    ordered_menu_parm = hou.MenuParmTemplate(
        name="select_texture_node",
        label="Select Texture Node",
        menu_items=menu_items,
        script_callback="print('pressed')",    
        script_callback_language=hou.scriptLanguage.Python
        )
...

or alternatively if you want to use your mentioned setScriptCallback() method you do it after you instanciatee the MenuParmTemplate
...
    ordered_menu_parm = hou.MenuParmTemplate(
        name="select_texture_node",
        label="Select Texture Node",
        menu_items=menu_items
        )
    ordered_menu_parm.setScriptCallback( "print('pressed')" )
    ordered_menu_parm.setScriptCallbackLanguage( hou.scriptLanguage.Python )
...
Edited by tamte - 2025年4月8日 22:01:44
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
16 posts
Joined: 7月 2020
Offline
Thanks Tomas, the first and the most obvious solution works. That was actually the first thing I tried and it was giving me error. I didn't realize the default script language is Hscript so it was needed to specifically set it to Python. Stupid but easy
Thanks.
  • Quick Links