Maya2023如何使用maya_standalone?

Hello everyone, I have an example script for maya_standalone that works in Maya2022 but doesn’t work in Maya2023.
The startup method I used was “mayapy.exe” in their installation directory.
Are there any operational differences between these two versions of Maya?
Source code from

import time
def initialize():
    try:
        import maya.standalone
        maya.standalone.initialize(name="python")
        print("maya.standalone.initialize success!")
    except: 			
        pass

def uninitialize():
    try: 			
        import maya.standalone
        maya.standalone.uninitialize() 	
        print("maya.standalone.uninitialize success!")
    except: 			
        pass

def createModel():
    # import mayapy
    import maya.cmds as cmds

    # create sphere and move it
    sphereName = cmds.polySphere(name="mySphere")
    cmds.move( -2, 1, 0, sphereName, absolute=True )

    # create cube and move it
    cubeName = cmds.polyCube(name="myCube")
    cmds.move( 0, 0.5, 0, cubeName, absolute=True )

    # create cylinder and move it
    cylinderName = cmds.polyCylinder(name="myCylinder")
    cmds.move( 2, 1, 0, cylinderName, absolute=True )

    print("createModel success!")

def saveFile():
    # import mayapy
    import maya.cmds as cmds

    # save .ma file
    cmds.file(rename=r"D:\Maya_script\autoCreateMod.ma")
    cmds.file(save=True, type="mayaAscii")

    print("saveFile success!")

if __name__ == '__main__':
    tic = time.time()
    initialize()

    createModel()

    saveFile()

    uninitialize()
    toc = time.time()
    print("run time cost: {}".format(toc-tic))

What is the error you get?

The error I got was: the CMD command line interface appeared for a few seconds, and then the page closed automatically.

You don’t want to have bare try except blocks - it is best practice to catch a specific exception type and you also want to always report the error so users can see a problem has occurred

1 Like

That is the behaviour - what is the error or stack trace?
Your bare try excepts are suppressing useful information to help you debug the issue

Ok, thank you for your help, I will try to catch the error, if there is progress I will continue to discuss, I am still a novice, please forgive my ignorance.

That’s ok - we have all abused try excepts in our time and come to regret it later.
Good luck hope you get it figured out

Hello, I got it.Only one line

DLL load failed while importing standalone: The dynamic link library (DLL) initialization routine failed.

This is an initialization error, which was discussed a long time ago(In the link below).
https://discourse.techart.online/t/import-maya-standalone-problem/1437
But the situation has changed. The Maya2023 installation directory has a lot less things than Maya2022(Some of the reduced folders are for python),I don’t know if these changes have anything to do with initialization errors
.

I don’t see any reason for the dll error.
Normally that happens when you try to import a compiled module that is incompatible with the current interpreter version

Can you try put this in your except blocks:

import traceback
traceback.print_exc()

OK, this should be a complete error.

Traceback (most recent call last):
File "D:\Maya_script\import_maya.py", line 54, in <module>
initialize()
File "D:\Maya_script\import_maya.py", line 6, in initialize
import maya.standalone
DLL load failed while importing standalone: The dynamic link library (DLL) initialization routine failed.

I have no idea why that would fail.
The only thing I can think is to remove name="python" from your initialise call. The docs don’t mention it: Help

Otherwise run this in your initialise function:

import maya
print(maya)

This will show you where maya is being imported from which could be a clue as to the problem

I put “import maya print(maya)” into the script, and then run it with Maya2023 and Maya2022 respectively., I got the following picture,They are similar.I have no clue about this.

Adding name=“python” to the initialization is the approach of this article.(In the link below).
https://forums.autodesk.com/t5/maya-programming/standalone-initialize-qt-error/td-p/12101213
Regardless of adding name=“python” or not, Maya2023 cannot run the script, but Maya2022 can.That shouldn’t be the point.
In the above article, “import maya.standalone as standalone” is normal. His Maya version is 2023.0. I am using 2023.3.I will try to switch to 2023.0.

Do you have any custom or 3rd party plugins set to auto load on my start up?

Yes, I have some plugins like this.
I will check their effect on Maya.

You likely have a plugin that is only compatible with Maya 2022 that is trying to be loaded by 2023.

Because you’ve cracked the Maya2023 by *replacing .dlls.