Context Options Editor - Read Name of Menu Item?

   1460   5   3
User Avatar
Member
447 posts
Joined: April 2018
Online
Hi, when using the Contex Options Editor, and an Int Menu, is there a way to read the name of the menu item?

For example, I have a menu called shot:
Medium   0
Far      1
Wide     2

I can read the value of the menu easily enough with @shot.

What I also want is to read the name of each item, so when I'm writing a file name, I want it to be $HIP/render/`@shot`.exr, but with the name, not the number.

I have the reverse problem when using a String Menu. With that, I can obviously get the string value, but I also want to get the index of the selected menu item, for the Switch node.
Edited by eikonoklastes - July 20, 2023 06:02:12
User Avatar
Staff
593 posts
Joined: June 2020
Offline
In Python you can use hou.contextOptionConfigfor this.

For example, something like this could address your first goal:
import json
menu_items = json.loads(hou.contextOptionConfig('shot'))['menu_items']
shot_idx = int(hou.contextOption('shot'))
shot_name = [item[0] for item in menu_items if int(item[1]) == shot_idx][0]
Edited by robp_sidefx - July 20, 2023 08:02:43
User Avatar
Member
447 posts
Joined: April 2018
Online
robp_sidefx
In Python you can use hou.contextOptionConfigfor this.

For example, something like this could address your first goal:
import json
menu_items = json.loads(hou.contextOptionConfig('shot'))['menu_items']
shot_idx = hou.contextOption('shot')
shot_name = [item[0] for item in menu if item[1] == shot_idx][0]
Thank you! A fair bit more convoluted than I was hoping for, but it does work.

Thanks again

Side note: In the last line, the name of the array should be menu_items, not menu.
Edited by eikonoklastes - July 20, 2023 06:57:07
User Avatar
Member
447 posts
Joined: April 2018
Online
Follow up on this:
This does not actually work for an Int Menu. I had set up a String Menu with explicit values of 0, 1, 2, 3 and that's why it worked for me.

For an Int Menu, hou.contextOption returns a float, that needs to be converted to an integer.
Furthermore, this line:
shot_name = [item[0] for item in menu_items if item[1] == shot_idx][0]
produces a list index out of range error.

I replaced it with this, and it seems to work now:
shot_name = menu_items[shot_idx][0]

Here is the updated code:
import json
menu_items = json.loads(hou.contextOptionConfig('still_shot'))['menu_items']
shot_idx = int(hou.contextOption('still_shot'))
shot_name = menu_items[shot_idx][0]
return '$HIP/render/still_render_' + shot_name + '.exr'
User Avatar
Staff
593 posts
Joined: June 2020
Offline
Sorry, there was a typo in the code initially. I've updated it. Your approach will also work, if your int menu has values 0,1,2,... but may fail if the values are 1,3,2,9,552,...
User Avatar
Member
447 posts
Joined: April 2018
Online
robp_sidefx
Sorry, there was a typo in the code initially. I've updated it. Your approach will also work, if your int menu has values 0,1,2,... but may fail if the values are 1,3,2,9,552,...
Brilliant, thank you! This does work now for the Int Menu.
  • Quick Links