Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

running imported modules functions from a socket command

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Rourker27
805 Views, 6 Replies

running imported modules functions from a socket command

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.

Labels (3)
6 REPLIES 6
Message 2 of 7
Rourker27
in reply to: Rourker27

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.

Message 3 of 7
Rourker27
in reply to: Rourker27

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.

Message 4 of 7
zewt
in reply to: Rourker27

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.

 

Message 5 of 7
Rourker27
in reply to: zewt

Oh, that makes sense. Yeah I'll have to double check that works later. Thanks @zewt 

Message 6 of 7
Rourker27
in reply to: zewt

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.

Message 7 of 7
Rourker27
in reply to: Rourker27

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.

Post to forums  

Autodesk Design & Make Report