socket.recv为空,但结果在Maya中显示

时间:2019-04-21 02:43:26

标签: python sockets maya recv

我正在尝试获取Maya中关节旋转到外部IDE中的值。我已经能够成功发送命令,并且Maya可以正确响应,但是我无法理解如何在IDE中接收结果。

我尝试使用socket.recv和socket.recvmsg,但是都出现错误。

在PyCharm中运行的代码

import socket
host = 'localhost'
port = 7720

code1 = ("servo_1 = [cmds.getAttr('joint2.rotateY')]\\n"
         "print servo_1")
try:
   # Connect to Maya Command Port
   maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   maya.connect( (host,port) )
   # Send Command Through Socket --> Can Only Send MEL Commands
   message = 'python("{}")'.format(code1)
   print message
   maya.send(message)
   reply = maya.recv(4096)
   print reply
except:
   raise Exception, 'Connection Failed To : %s:%s' % (host, port)

finally:
   #Close Socket Connection
   maya.close()

Maya中的代码以打开commandPort

import maya.cmds as cmds
if not cmds.commandPort(':7720', q=True, echoOutput=True, noreturn=False):
    cmds.commandPort(name=':7720')

如Maya所示,预期输出为[43.74],但实际输出为PyCharm

1 个答案:

答案 0 :(得分:0)

我通过建立从Maya到PyCharm的客户端套接字连接来制作python套接字服务器,并将Maya设置为客户端,并使用pickle模块发送列表来使其工作。

Python服务器

import socket
import pickle

HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(4096)
    if not data: break
    conn.send(data)
    data2 = pickle.loads(data)
    print 'servo_1 = %s' % (data2)
conn.close()

Maya客户

import socket, pickle
import maya.cmds as cmds

HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

max = int(cmds.playbackOptions( q=True, aet=True))
servo_1 = [cmds.getAttr("joint2.rotateY")]

data_string = pickle.dumps(servo_1)
s.send(data_string)

data = s.recv(4096)
data_arr = pickle.loads(data)
s.close()
print 'Received', repr(data_arr)