Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BigRoy/03f2a12f9dcaf0cc4c2283306201d0ed to your computer and use it in GitHub Desktop.
Save BigRoy/03f2a12f9dcaf0cc4c2283306201d0ed to your computer and use it in GitHub Desktop.
Houdini Python script to get the menu items for configured Solaris Context Options
from typing import Union
import hou
import json
import textwrap
def exec_as_function(code):
"""Execute code similar to Houdini
- Single line evaluates like expression
- Multi-line evaluate like body of a function.
"""
if code.count("\n") == 0:
# Houdini evualates single lines as expressions
return eval(code)
indented_code = textwrap.indent(code, prefix=" ")
function_code = f"""
def main():
{indented_code}
value = main()
"""
loc = {}
exec(function_code, {}, loc)
return loc['value']
def get_context_option_menu_values(name: str) -> list[tuple[str, str]]:
"""Return houdini scene context option menu values.
Note that for an `int_menu` the resulting values are still `str` because
Houdini does not differentiate the value fields to a different type and
they may very well still hold string values that are not actual digits.
For a `py_menu` the code is executed similar to how Houdini would
execute it - and the return value is just directly passed along so
it may not necessarily match the function return type exactly.
Arguments:
name (str): The context option name.
Returns:
list[tuple[str, str]]: The list of name, value pairs.
"""
if not hou.hasContextOption(name):
raise ValueError(f"Config option '{name}' does not exist.")
config: str = hou.contextOptionConfig(name)
if not config:
raise ValueError(f"Unable to get context option config for option '{name}'")
data: dict = json.loads(config)
context_type: str = data["type"]
supported_types: set[str] = {"int_menu", "string_menu", "py_menu"}
if context_type not in supported_types:
raise ValueError(
f"Context option type '{context_type}' not supported. "
f"Supported values are: {', '.join(supported_types)}"
)
# Python menu is special
if context_type == "py_menu":
return exec_as_function(data["menu_source"])
menu_items: list[str, str] = data["menu_items"]
auto_values: bool = data["autovalues"]
result = []
for name, value in menu_items:
if auto_values:
result.append((name, name))
else:
result.append((name, value))
return result
menu_items = get_context_option_values("shot")
print(menu_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment