This will use the callback hou.ui.waitUntil to check if the desktop has changed to add the shelf set to the user’s shelfbar. having it exit was hard but hou._isExiting is a thing!
from functools import cache
from typing import Callable
import hou
import os
import time
import logging
log = logging.getLogger('my.houdini.uiready')
@cache
def initial_desktop_name():
return hou.ui.curDesktop().name()
def throttle(callbacks : list[Callable], delay=0.5):
"""pulled from hou.ui docs. only run callback every 0.5 seconds"""
last_check = [0.0]
def wrapper():
now = time.time()
if hou._isExiting():
return True
if now < last_check[0] + delay:
return False
last_check[0] = now
return any(callback() for callback in callbacks)
return wrapper
def has_desktop_changed():
"""Check if stored value has changed."""
return hou.ui.curDesktop().name() != initial_desktop_name()
def update_events():
"""Will cycle changes any time user changes their desktop opened."""
while wait((has_desktop_changed,)):
if has_desktop_changed():
initial_desktop_name.cache_clear()
add_shelf()
def add_shelf():
shelf_name = "my_shelf_name"
cmd = f'shelfdock add {shelf_name}'
hou.hscript(cmd)
def wait(func):
hou.ui.waitUntil(throttle(func, delay=0.5))
return not hou._isExiting()
def main():
try:
add_shelf()
update_events()
except Exception as e:
log.exception(e)
if __name__ == '__main__':
main()