Maya Python script is compiled into pyc binary file

Compile python into a pyc binary file to facilitate the distribution of scripts and prevent ordinary people from modifying it at will. Here, use Maya's own python to compile to prevent it from not running normally.

ready

Divide the script into two files, one is the Core file, and the other is the script initialization related files

Reason: If you directly compile the source file into a pyc file, Maya will not be able to recognize it, prompting there is no plug-in and other error messages

Script initialization file

from Core import *

class MyPlugin(OpenMaya.MPxCommand):
    def __init__(self):
        super(MyPlugin, self).__init__()

    def doIt(self, args):
        print(u'doIt')
        #code 
        #UI.show()

### plugin initialization
def cmdCreator():
    ''' Creates an instance of the scripted command. '''
    return MyPlugin()

def initializePlugin(mobject):
    ''' Initializes the plug-in.'''
    mplugin = OpenMaya.MFnPlugin(mobject)
    try:
        mplugin.registerCommand(kPluginCmdName, cmdCreator)
    except:
        sys.stderr.write("Failed to register command: %s\n" % kPluginCmdName)

def uninitializePlugin(mobject):
    ''' Uninitializes the plug-in '''
    mplugin = OpenMaya.MFnPlugin(mobject)
    try:
        mplugin.deregisterCommand(kPluginCmdName)
    except:
        sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
        pass
    pass

Core file

This file is our code

Compile file

Call C:\Program Files\Autodesk\Maya2019\bin\mayapy.exe to run the following file

import py_compile

py_compile.compile("Core.py")

At last

Generate the Core.pyc file after compilation, copy and distribute the Core.pyc file and the initialization script file together, and directly call the initialization script file when the script is called, and it can be used normally

https://blog.csdn.net/shaynerain/article/details/106426953

Guess you like

Origin blog.csdn.net/shaynerain/article/details/106426953