Jump to content

Updating menu items with python


DannioMaundinio

Recommended Posts

Hi!

 

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()
template.setMenuItems(target_menu_items)

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()
template.setMenuItems(target_menu_items)

ptg.replace("menu_parm", template)
node.setParmTemplateGroup(ptg)

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

template.setName("bahhumbug")

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,

Dan

Edited by DannioMaundinio
  • Like 1
Link to comment
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.

 

try:

 

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
Link to comment
Share on other sites

  • 2 weeks later...

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.

 

Thanks!

Dan

Link to comment
Share on other sites

  • 1 year later...

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()
template.setMenuLabels(cacheList)

ptg.replace("name", template)
hou.pwd().setParmTemplateGroup(ptg)

print cacheList

Link to comment
Share on other sites

  • 7 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...