running imported modules functions from a socket command

running imported modules functions from a socket command

Rourker27
Contributor Contributor
1,391 Views
6 Replies
Message 1 of 7

running imported modules functions from a socket command

Rourker27
Contributor
Contributor

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.

Accepted solutions (1)
1,392 Views
6 Replies
Replies (6)
Message 2 of 7

Rourker27
Contributor
Contributor

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.

0 Likes
Message 3 of 7

Rourker27
Contributor
Contributor

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.

0 Likes
Message 4 of 7

zewt
Collaborator
Collaborator

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.

 

0 Likes
Message 5 of 7

Rourker27
Contributor
Contributor

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

0 Likes
Message 6 of 7

Rourker27
Contributor
Contributor

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.

0 Likes
Message 7 of 7

Rourker27
Contributor
Contributor
Accepted solution

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.

0 Likes