Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BigRoy/03f2a12f9dcaf0cc4c2283306201d0ed to your computer and use it in GitHub Desktop.

Select an option

Save BigRoy/03f2a12f9dcaf0cc4c2283306201d0ed to your computer and use it in GitHub Desktop.

Revisions

  1. BigRoy revised this gist Jul 21, 2025. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions hou_solaris_get_context_options_menu_items.py
    Original file line number Diff line number Diff line change
    @@ -48,13 +48,13 @@ def get_context_option_menu_values(name: str) -> list[tuple[str, str]]:
    if not hou.hasContextOption(name):
    raise ValueError(f"Config option '{name}' does not exist.")

    config = hou.contextOptionConfig(name)
    config: str = hou.contextOptionConfig(name)
    if not config:
    raise ValueError(f"Unable to get context option config for option '{name}'")

    data = json.loads(config)
    context_type = data["type"]
    supported_types = {"int_menu", "string_menu", "py_menu"}
    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. "
    @@ -65,8 +65,8 @@ def get_context_option_menu_values(name: str) -> list[tuple[str, str]]:
    if context_type == "py_menu":
    return exec_as_function(data["menu_source"])

    menu_items = data["menu_items"]
    auto_values = data["autovalues"]
    menu_items: list[str, str] = data["menu_items"]
    auto_values: bool = data["autovalues"]
    result = []
    for name, value in menu_items:
    if auto_values:
  2. BigRoy created this gist Jul 21, 2025.
    81 changes: 81 additions & 0 deletions hou_solaris_get_context_options_menu_items.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    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 = hou.contextOptionConfig(name)
    if not config:
    raise ValueError(f"Unable to get context option config for option '{name}'")

    data = json.loads(config)
    context_type = data["type"]
    supported_types = {"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 = data["menu_items"]
    auto_values = 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)