Install shelf from folder drag and drop

Hello! I would like to know if there’s a way in python to install self buttons in maya from the installation folder to maya directly.

So could install my shelf from whatever folder i use on the disk.
I think advanced skeleton does that, we have a ‘intall.mel’ that we can drag and drop in maya then it intalls all the shelves needed.

Is anyone can help ?

Cheers!

Yes, I do this with python! Let me whip up an example…

import os
import logging
import maya.cmds as cmds

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


def onMayaDroppedPythonFile(obj):

    mod_directory = os.path.dirname(__file__)
    mod_file_path = os.path.join(mod_directory, "myMod.mod")

    with open(mod_file_path, "w") as file_object:

        file_object.write("+ MyMod 1.0 {0}".format(mod_directory))
        logger.info("Created mod file {0}".format(mod_file_path))

    # Generate Shelf

    window = cmds.window()
    shelf = cmds.shelfLayout()

    icon_directory = os.path.join(mod_directory, "icons")

    buttons = [

        {
            "label": "Do Stuff",
            "annotation": "It does something...",
            "image1": os.path.join(icon_directory, "do_stuff.png"),
            "command": "from do_stuff import do\ndo()",
        },
    ]

    for btn in buttons:
        cmds.shelfButton(**btn)

    cmds.saveShelf(shelf, os.path.join(mod_directory, "shelf_MyMod"))

    logger.info("Created shelf file {0}".format(
        os.path.join(mod_directory, "shelf_MyMod.mel")))

This basically generates a mod and shelf file in the folder i dragged the script from. The onMayaDroppedPythonFile is a hard coded Maya thing you need for the drag drop mechanic to work. I then put the mod/shelf files in the appropriate location. You could however have it save it in the right path, or edit the env file etc… lots of ways to do it. For me this was the cleanest way i found - generate the mod file but allow the user to place it in the right location.

3 Likes

Hello! Thank you soo much for the exemple!! Great help. I was a bit confused about not seing the shelf first but i understood why! I can see the effect now!! This is exactly what i needed to know. Strange thing is that i struggle to find usefull info on this! Should pin this topic ^^!

Cheers!

I tried different approach and found it work without problem!
suggestions are welcome

import os
import shutil
import maya.cmds as cmds
import maya.mel as mel

def onMayaDroppedPythonFile(*args):
    try:
        # Script directory for where file come from, script_file_path for full address
        # Make sure this code is on the same location with the script
        installer_directory = os.path.dirname(__file__)
        script_file_path = os.path.join(installer_directory, "file_name.py)
        
        # raise runtime error if script not found
        if not os.path.exists(script_file_path):
                raise RuntimeError("Unable to find '"file_name.py"' relative to this installer file")
        
        # Get default script directory for suggestion
        prefs_dir = os.path.dirname(cmds.about(preferences=True))
        scripts_dir = os.path.normpath(os.path.join(prefs_dir, "scripts"))
        
        # copy script to maya default directory
        shutil.copy(script_file_path,scripts_dir)

        # Generate Shelf for current active shelf
        current_shelf = mel.eval("string $currentShelf = `tabLayout -query -selectTab $gShelfTopLevel`;")
        cmds.setParent(current_shelf)
        cmds.shelfButton(command="import script_name; script_name.execute()",ann="script_name",
        label="script_name",image="custom.png",sourceType="python",iol="SCRIPT_NAME")

        cmds.confirmDialog(message="Script successfully installed ""<br>""<br>""{0}".format(scripts_dir),
        title="Confirmation dialog")
    
    except Exception as e:
        cmds.confirmDialog(message="Script failed to installed: {0}".format(e),icon = "warning",title="ERROR")