Skip to content

Instantly share code, notes, and snippets.

@tpoveda
Created May 6, 2021 12:57
Show Gist options
  • Save tpoveda/8ee8cb29d0e830682fdfc826a5be447d to your computer and use it in GitHub Desktop.
Save tpoveda/8ee8cb29d0e830682fdfc826a5be447d to your computer and use it in GitHub Desktop.
MotionBuilder Python startup "Deferred" Qt script
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
"Hacky" example that shows how to use OnUIIdle callback to defer a function call until MotionBuilder UI is properly
"""
from __future__ import print_function, division, absolute_import
try:
from PySide.QtWidgets import QApplication, QMenuBar
except ImportError:
from PySide2.QtWidgets import QApplication, QMenuBar
def get_main_qt_window():
parent = QApplication.activeWindow()
grand_parent = parent
while grand_parent is not None:
parent = grand_parent
grand_parent = parent.parent()
return parent
def _on_uiidle_fn(*args, **kwargs):
main_window = get_main_qt_window()
if not main_window:
return
# accessing QMenuBar using main_window.menuBar() can cause MotionBuilder to crash
parent_menubar = main_window.findChild(QMenuBar)
if not parent_menubar:
return
init_cpg()
def init():
# this will be called once Qt is fully loaded and you will be able to access and interact with Qt main window menubar without
# problems.
# CODE_HERE
fbsys.OnUIIdle.Remove(_on_uiidle_fn)
fbsys.OnUIIdle.Add(_on_uiidle_fn)
@Liquid-Martin
Copy link

Thanks a lot for sharing this. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment