Skip to content

Instantly share code, notes, and snippets.

@rondreas
Created January 6, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondreas/0484337fce0045d0d70c15b0ade884c4 to your computer and use it in GitHub Desktop.
Save rondreas/0484337fce0045d0d70c15b0ade884c4 to your computer and use it in GitHub Desktop.
functions for gettings the currently active tools in modo
import lx
def get_command(name: str) -> lx.object.Command:
""" Spawn and return the command of given name """
cmd_svc = lx.service.Command()
try:
tag = cmd_svc.Lookup(name)
return cmd_svc.Spawn(tag, name)
except RuntimeError as e:
# bad result: CMD_UNKNOWN_COMMAND
pass
def get_value_hints(command: str, argument: int, internal: bool = True) -> List[str]:
""" Given a command name and index for argument to get ui value hints
from. Returns list of the value hint names. """
result = list()
cmd = get_command(command)
attrs = lx.object.AttributesUI(cmd)
value_hints = attrs.UIValueHints(argument) # type: lx.object.UIValueHints
for index in range(value_hints.PopCount()):
result.append(value_hints.PopInternalName(index) if internal else value_hints.PopUserName(index))
return result
def list_available_tools():
""" Return a list of all registered tools and tool presets,
Example::
>>> tools = list_available_tools()
>>> "xfrm.move" in tools # test that xfrm.move is in list of tools
True
>>> "TransformMove" in tools # test that the preset TransformMove is in list of tools also
True
"""
return get_value_hints("tool.set", 0)
def get_active_tools():
""" Tools states can be either of: on | off | clear | remove | add | flush
we're looking for any tools that are 'on' with this function.
"""
active_tools = []
for tool in list_available_tools():
try:
query_results = lx.eval(f"tool.set {{{tool}}} ?")
if query_results == "on":
active_tools.append(tool)
except RuntimeError:
# Will raise runtime error when the command is disabled, like "tool.set ik.pose" won't be enabled
# when users don't have any hierarchy selected etc. Enable state might differ between others but
# we can safely assume the tool won't be active unless the command is enabled.
pass
return active_tools
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment