Skip to content

Ndgt/Mobu_PluginBase_Python

Repository files navigation

Mobu-PluginBase-Python

Set the standard of arranging original Python Tools/Scripts


Concepts

In the Menubar, add new menu to invoke original Scripts/Tools

alt text

set the following directories in Users/<username>/Documents/MB/<version>/config/PythonStartup folder.

  • Tools : Python Tools registered in FBTools
  • Scripts : Python Scripts which can be used as module

Usage of Example

Put data under src folder into C:/Users/<username>/Documents/MB/<version>/config/PythonStartup directory

alt text


Rules

In the Scripts

define main() function to execute all functions in the module

In the Tools

In the main file, define ActivateTool() function below

def ActivateTool():
    # define the Tool name 
    toolName = "<tool name>"

    # check the Tool already created
    if toolName in FBToolList:
        ShowToolByName(toolName)

    else:
        # declare tool
        tool = <Original Tool Class declare>
        FBAddTool(tool)
        ShowToolByName(toolName)

Mechanism

Get Access to MenuBar

pyfbsdk.FBGetmainWindow() can return the MotionBuilder main window.
One of the child widget contains MenuBar, to which add new menu.

def getMenubar() -> QMenuBar:
    # get Main Window
    pMainW = FBGetMainWindow()
    if pMainW:
        #Convert pointer to any non-inappropriate type
        MainW = wrapInstance(pMainW, QMainWindow)
        menubar = MainW.menuWidget().children()[1]
        return menubar

Add Menu

PluginBase.py extracts module name from Tools/Scripts path.

mbar  = getMenubar()
tmenu = mbar.addMenu("Tools")
smenu = mbar.addMenu("Scripts")

# if Tools
for file in os.listdir(<toolpath>):
    if file.endswith(".py"):
        # extract module name to import
        module_name = file[:-3]
        module = importlib.import_module(module_name)
        
        # add action to invoke tool function
        t = tmenu.addAction(module_name)
        t.triggered.connect(module.ActivateTool)

# if Scripts
for file in os.listdir(<scriptpath>):
    if file.endswith(".py"):
        # extract module name to import
        module_name = file[:-3]
        module = importlib.import_module(module_name)

        # add action to invoke tool function
        s = smenu.addAction(module_name)
        s.triggered.connect(module.main)

About

Standard Python Scripts Distribution

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages