为什么一个软件通过UDP(python)从另一个软件接收数据时会变慢?

时间:2016-12-20 18:00:06

标签: python sockets udp cinema-4d

我经常通过UDP从Houdini发送相机的位置矩阵并将其设置在Cinema4d中。两者都是3-D软件程序。数据发送正常,但Cinema4d冻结,从houdini更新矩阵时速度很慢。为什么会这样?

这是我从Houdini发送的python代码:

import socket

UDP_IP = '192.168.1.8'
UDP_PORT = 7864

cam = hou.selectedNodes()
camerac4d =  hou.node('/obj/obj_andcamera/cam1')
xform = camerac4d.worldTransform() #get the camera matrix
data_string = str(xform)

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
sock.sendto(data_string, (UDP_IP, UDP_PORT))

Cinema 4d中的UDP接收器:

import socket

def main():
    operateon = doc.SearchObject('Camera') #get cinema 4d camera

    UDP_IP = '192.168.1.8'
    UDP_PORT = 7864

    sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    sock.bind((UDP_IP,UDP_PORT))
    data_string,addr = sock.recvfrom(1024)


    data_string = ast.literal_eval(data_string) #converts string list


    #set houdini matrix to cinema 4d camera

    off = v(newlist[3][0],newlist[3][1], -newlist[3][2]) 
    v1 = v(-newlist[0][0],newlist[0][1], newlist[0][2])
    v2 = v(-newlist[1][0], -newlist[1][1], -newlist[1][2])
    v3 = v(-newlist[2][0], -newlist[2][1], newlist[2][2])

    mat = c4d.Matrix(off,v1*-1,v2*-1,v3)

    newpos = operateon.SetMg(mat)

1 个答案:

答案 0 :(得分:1)

所以我想出来,我只需要将矩阵设置粘贴在5次尝试后死亡的while循环中,这样cinema4d就不会崩溃,然后必须使用刷新视口的代码行更新影院中的视口每次矩阵最后更新

def main():

operateon = doc.SearchObject('Camera') #find and set the cinema camera

##UDP receive transformation matrix from houdini##

UDP_IP = 'localhost'
UDP_PORT = 7864

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

sock.bind((UDP_IP,UDP_PORT))
##kill the connection after 5 viewport updates/works but stupid/but it stops cinema from freezing on an endless while loop
die = 0
while die < 5:
    die += 1
    data_string, addr = sock.recvfrom(1024)

# set each vector component 
    data_string = ast.literal_eval(data_string) #converts string to list 

    #a hou matrix is 4x4, cinema is a 3x4 matrix consisting of :off=position data, v1,v2,v3 stores the scale,rotation and shear
    index = 3  #Delete column 3 
    newlist = [ (x[0:index] + x[index+1:])  for x in data_string]
#print newlist
#---------------------set vectors for matrix---------------------------------#
    off = v(newlist[3][0],newlist[3][1], -newlist[3][2]) 
    v1 = v(-newlist[0][0],newlist[0][1], newlist[0][2])
    v2 = v(-newlist[1][0], -newlist[1][1], -newlist[1][2])
    v3 = v(-newlist[2][0], -newlist[2][1], newlist[2][2])

#---------------------create a matrix and set it---------------------------------#

    mat = c4d.Matrix(off,v1*-1,v2*-1,v3)
    newpos = operateon.SetMg(mat)


    #update the viewport with the new matrix 
    c4d.EventAdd()
    c4d.DrawViews(c4d.DRAWFLAGS_ONLY_ACTIVE_VIEW|c4d.DRAWFLAGS_NO_THREAD|c4d.DRAWFLAGS_NO_REDUCTION|c4d.DRAWFLAGS_STATICBREAK)