hannesdelbeke / maya-plugin-template

maya plugin template
5 stars 1 forks source link

Maya Python plugin template

A template to quickly make a Python plugin for Maya. (For modules, see Maya module template

Features

Overview

handle dependencies automatically

A pip install auto handles dependencies, removing the need for vendoring dependencies.
Without pip install you need to manually install the dependencies.
This template includes a pyproject.toml to support a pip install to a Maya plugin folder.
The below command triggers a pip install from this repo:

pip install https://github.com/hannesdelbeke/maya-plugin-template/archive/refs/heads/main.zip --target "C:/Users/%username%/Documents/Maya/plug-ins"
Read this if the above command fails _1. if the target folder doesn't exist, this command creates a `Maya/plug-ins` folder in your documents , which requires admin access._ _2. When a user has been renamed on Windows, `%username%` will return the current name. But the folder path will use the old name, resulting in this demo command failing._

Maya plugins don't support Python packages, they only support a single .py file.
To include a package in your plugin, I recommend to use pip dependencies.

the drag and drop installer uses requirements.txt to install dependencies. and installs them to documents/maya/scripts.

Instructions

Extras

see command info You can add commands to you Maya plugin. Included this in the template but I never use this. Feel free to just delete all code related to commands. Adding a command to your plugin is optional. (I never had the need for it) In Maya Python scripting, MPxCommand is a base class for creating custom commands. Below is a simple example of creating a custom command using MPxCommand. This example demonstrates a command that creates a cube. ```python import maya.api.OpenMaya as om import maya.cmds as cmds class CreateCubeCommand(om.MPxCommand): commandName = "createCube" def __init__(self): super(CreateCubeCommand, self).__init__() def doIt(self, args): # Parse the arguments if needed (not used in this example) # Create a cube cube = cmds.polyCube()[0] # Set the result to the name of the created cube self.setResult(cube) # Creator function def createCubeCommand(): return om.asMPxPtr(CreateCubeCommand()) # Initialize the plugin def initializePlugin(plugin): pluginFn = om.MFnPlugin(plugin) try: pluginFn.registerCommand( CreateCubeCommand.commandName, createCubeCommand ) except: om.MGlobal.displayError( "Failed to register command: {}".format( CreateCubeCommand.commandName ) ) # Uninitialize the plugin def uninitializePlugin(plugin): pluginFn = om.MFnPlugin(plugin) try: pluginFn.deregisterCommand(CreateCubeCommand.commandName) except: om.MGlobal.displayError( "Failed to unregister command: {}".format( CreateCubeCommand.commandName ) ) # Usage: # 1. Save this script as "createCubeCmd.py" # 2. Load the script in Maya using the following commands: # ``` # import maya.cmds as cmds # cmds.loadPlugin("path/to/createCubeCmd.py") # ``` # 3. Run the custom command: # ``` # cmds.createCube() # ``` ```
sample repos using this template create a PR to add your repo below 😊 - https://github.com/hannesdelbeke/maya-pip-qt - https://github.com/plugget/plugget-qt-maya-plugin - https://github.com/hannesdelbeke/pyblish-maya-plugin - https://github.com/hannesdelbeke/maya-plugin-duplicate-obj-along-curve - https://github.com/hannesdelbeke/maya-plugin-snap-to-closest-UV

references