[Maya] Running existing functions in mayapy

My usual workflow with mayapy has been to just write a script that is designed to be called with mayapy.exe. This works fine when that is the intention from the start, however now I would like to run functions from existing libraries/modules using mayapy and I am not quite sure how to proceed. If I go the write-a-script route then I will end up creating a lot of scripts just to run existing functions in a way that I can pass them arguments via the command line. My other thought was to create a script that accepts a module name, function name, and all the arguments to pass. While that would work I figure there may be a better way.

How do you guys handle situations like this? Is this a good case for RPC?

Also, the mayapy instances that would be running these scripts will be on artists’ machines, and will be created (and killed) for each script that gets run. I’m not trying to set up persistent mayapy sessions (at the moment).

when i did it, i would use the command port. So startup headless and then send commands to it. I would suggest the RPC/command port route

That’s what I figured. I’ll got the RPC route, thanks.

EDIT:
If I’m running this on machines that are connected to the internet, do I need to secure the connection? I haven’t done any networking in python so I’m not to what extent I need to protect my connections. The server and client will be on the same machines, if that matters.

we have a Batch Utility GUI that has a text input field. I can type in a series of python commands to run, imports, run functions, etc. That text is captured and sent to the mayapy process that then executes the python code on each file.

If you want to remote control a standalone interactively, You can do lightweight stuff for yourself with something like this: https://github.com/theodox/standaloneRPC

That will listen for remote calls packaged up as JSON. It’s not fancy and not secure(!) but it’s a good proof of concept. For fancier stuff you can try something like http://zeromq.org/ or https://rpyc.readthedocs.org/en/latest/ to remotely control a standalone.

If you don’t need to interact with the standalone, you can just use subprocess and fire off the standalone with the -c flag, passing in a set of commands as a string using -c to run a script from disk. If you go that route, https://gist.github.com/theodox/2c712a91155c7e1c4c15 does some of the scaffolding you might need (particularly for environment vars, etc)

I’ve been experimenting with rpyc and enjoying the simplicity of it. I’m not sure what to do about security and have been finding it difficult to find good beginner resources on it. Do you have any pointers or suggestions on good practices/approaches for securing an rpc connection? Basic HTTP auth? SSL?

RPYC will let you expose services on a case-by-case basis (instead of using ‘classic mode’ where the remote client is effectively running the local host as if it were local. http://rpyc.readthedocs.org/en/latest/docs/services.html I have never bothered with security beyond that because my use-cases are all in house. But that’s better than nothing. In a ‘real’ environment you’d want to have a separate authentication service, maybe using OAuth2. That basically means you have send a request to a separate server which returns a token and then that token is the guarantee of what rights the user has. It’s not trivial to do, alas. Here’s an example of using OAuth to get to twitter:

http://popdevelop.com/2010/07/an-example-on-how-to-use-oauth-and-python-to-connect-to-twitter/

to give the flavor.

Can I not just restrict my service to connections from localhost and not worry about authenticating? I only need to communicate with an instance on the same machine.

I think RPYC will let you restrict it that way, yes, but it’s been a while since I tried.