chris debski
chris debski

Reputation: 1

Transferring a mesh from one process to another in PYTHON

I've been cracking my head over this but nothing comes to my mind yet. I want my script to execute a .py file inside of another already started process. I have a maya process opened, and inside for example modo I want to start file hello.py (print 'hello!') inside that exact Maya.

I already got the PID of that maya process, but don't know how to actually send a command to execute.

is theres some attribute/flag in subprocess or signal modules I could be missing? or is it done even another way?

import os

openedMaya = []
r = os.popen('tasklist /v').read().strip().split('\n')
for i in range(len(r)):
    s = r[i]
    if 'maya.exe' in s and ': untitled' in s:
        openedMaya.append(s)
mayaPID = openedMaya.split('maya.exe')[1].split('Console')[0]

I need a command that could execute hello.py in that maya process.

Upvotes: 0

Views: 177

Answers (3)

chris debski
chris debski

Reputation: 1

Thanks for the advices, at the end I found a solution by opening the port of maya, by starting a mel command (at the startup):

commandPort -n ":<some_port>";

and connecting from modo to that port through socket:

HOST = '127.0.0.1'
PORT = <some_port>
ADDR=(HOST,PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
client.send(<message_that_you_want_to_send)
data = client.recv(1024)
client.close()

and i'm able to do whatever I want inside that opened maya, as long as I send mel commands.

Thanks for the help though!

Upvotes: 0

ababak
ababak

Reputation: 1793

Maybe an easier way would be to transfer your mesh by using an intermediate file. One process creates the file, another process (running inside the host app) reads it in.

Upvotes: 0

Green Cell
Green Cell

Reputation: 4777

You could use RPyC to act as a bridge so that you can communicate from one software to another. The idea is that you use RPyC to run an idle server in Maya, where the PYTHONPATH is also pointing to your hello.py script. This server stays active in the session, but the user shouldn't notice it exists.

Then in your other software you use RPyC to broadcast a message using the same port as the server so that it triggers it in Maya. This would then run your command.

It's slightly more overhead, but I have been able to use this successfully for stand-alone tools to trigger events in Maya. As far as using subprocess, you can use it to run a command in a new Maya session, but I don't think there's a way to use it for an existing one.

Hope that nudges you in the right direction.

Upvotes: 1

Related Questions