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.