Jump to content

Updating menu items with python

Recommended Posts



I'm trying to find a way to read a config when a button is pressed, and update the available items in a menu parameter.


I've tried a couple of methods, but haven't managed to get anything to work yet. :(


First Attempt:

target_menu_items = ["a", "b", "c"]

menu_parm = node.parm("menu_parm")
template = menu_parm.parmTemplate()

The interface remains unchanged. Reading the docs that is understandable:


Note that ParmTemplate objects are just data containers, and they have no live relationship with objects in Houdini. For example, a hou.Node object corresponds directly to a node in Houdini, and if the node is renamed in Houdini hou.Node.name will return the new name. ParmTemplates, on the other hand, do not have a live relationship. Creating a ParmTemplate based on a spare parameter or asset definition parameter will simply copy the information into the ParmTemplate object, and modifying the object will not affect the spare parameter/asset parameter.




So I tried what the docs recommended without any luck:

ptg = node.parmTemplateGroup()

menu_parm = node.parm("menu_parm")
template = menu_parm.parmTemplate()

ptg.replace("menu_parm", template)

I had some success by adding a line, but this isn't really a solution.


I assume it's failing to replace the template if the name of the replacement template matches that of the existing one. I don't understand why that would be a problem though.


I've had a thorough Google and can't find any examples of this... so I'm worried I'm approaching it the wrong way. Can anyone explain where I'm going wrong?


Many many thanks in advance,


Edited by DannioMaundinio
  • Like 1

Share this post

Link to post
Share on other sites

Without a .hip file to look at I'm not entirely sure how you are going about using your script, so this is just a stab in the dark.




target_menu_items = ["a", "a", "b", "b", "c",  "c"]


Some menus require matching pairs to populate, Im not entirely sure why, maybe someone else can explain it.

Edited by 3iart

Share this post

Link to post
Share on other sites

Thanks for your reply.


I believe that paired list solution applies to the "Menu Script" for the parameter, as far as I can tell the .setMenuItems() function in python sets both the labels and item values unless labels are separately set with .setMenuLabels().


My problem was more to do with rebuilding menus to have different entries depending on the choice made in a separate parm. I tried to build a demo scene at home, but my machine wasn't playing ball. :(


I did some more reading of the docs and found this on the ParmTemplateGroup page:




You can get an asset’s parm templates with hou.HDADefinition.parmTemplateGroup and set them with hou.HDADefinition.setParmTemplateGroup. Similarly, you can get an individual node’s parameter definition with hou.Node.parmTemplateGroup and set it with hou.Node.setParmTemplateGroup. When setting a node’s parameter interface, any parms that are not in the node’s type’s set of parms will be added as spare parameters. Similarly, if parameters in the node’s type are not present in the group, they will be automatically added at the end and made invisible. Also, if they exist but the types, labels, looks, naming schemes, etc are different, Houdini will discard all changes you attempt to make to these reserved parameters except for visibility settings.


So I guess I was approaching it in the wrong way. My method is now to build the menu parms as spare parms, rather than attempting to modify an existing asset parm, and that seems to be a happy solution... if a bit messy.




Share this post

Link to post
Share on other sites

I'm doing a similar thing, my code is working on a subnet but for some reason when I convert it to a digital asset it doesn't seem to be able to update the menu... Here's my code:

import os

dir = hou.hipFile.path().rsplit('/',1)[0]

folder = hou.pwd().evalParm('folder')

check = dir + '/' + folder + '/'

cacheList = os.listdir(check)

ptg = hou.pwd().parmTemplateGroup()

menuParm = hou.pwd().parm('name')
template = menuParm.parmTemplate()

ptg.replace("name", template)

print cacheList

Share this post

Link to post
Share on other sites

thanks for that

Edited by tasiek

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now