I have a problem and I am not sure what is going on, I have created a 'userSetup.py' file and placed it in the script folder (C:\Users\*user*\Documents\maya\2022\scripts)
This is my userSetup.py file:
def importPy():
### import modules and open a command port ###
exec("import my_module", globals())
exec("cmds.commandPort(name=':1234', sourceType = 'python')")
exec("print('FINISHED')")
def tryImport():
"""
execute importPy when Maya is idle
"""
from maya.utils import executeDeferred
executeDeferred("importPy()")
tryImport()
now I have created a file 'my_module.py' and placed it in the same script folder
this is the my_module.py file:
print("my_module loaded")
def function_print(statement: str):
print(statement)
I know this is working on startup as the console in Maya reads:
WORKING!
my_module loaded
Now I have a successfully sent over simple commands like:
cmds.polyCube()
through the connection, it works fine even returns fine which I found odd since I am not echoing the output, so the socket server is working as intended. But if I send:
my_module.function_print("please print this")
it doesn't work, but it will work if I write that exact same line in the python command line within Maya.
This doesn't really make sense to me, I am fairly new to python so I may be missing something obvious here. But all I can think is that the socket commands somehow don't have access to the global scope.
If anyone knows what's up with this, it would be much appreciated.
Thanks,
Brendan
P.S. the names to my modules and functions have been made up for this example. If that wasn't obvious.
Also a side note, I don't think the Maya socketserver.py module has not been properly updated to python 3. But again, I barely know what I am talking about.
Solved! Go to Solution.
Solved by Rourker27. Go to Solution.
it doesn't print
WORKING!
my_module loaded
it does print
my_module loaded
FINISHED
I was doing some other tests when I copied that into the post, sorry if there was confusion there.
So a work around for this that I did find is to import my_module on every send so if I send:
import my_module; my_module.function_print("please print this")
it works fine, I just wish I didn't have to do that.
You're telling executeDeferred to compile and run the string "importPy()", but that won't make it run it in the global context of your module.
You can just say "executeDeferred(importPy)" to hand it the function to call, instead of giving it a source string.
hmm, so this still isn't working. When I send the the command it is stating that my_module is not defined. though it is clearly importing it on start-up. And it still works if I write it directly in the console within Maya.
Hello, so I have figured out the problem. I was correct in my assumption that commandPort calls don't have access to the global scope. I found a google groups thread that was discussing this problem (https://groups.google.com/g/python_inside_maya/c/ct5I0Q4Vs94?pli=1), turns out the commandPort has it's own local scope which is created anew with each call. However if you append 'import __main__; __main__.' before each call this will give you access to Maya's global scope and you will be able to import custom modules.
Can't find what you're looking for? Ask the community or share your knowledge.