Hi, Last week I managed to get a connection from Blender to max.msp working with the following function in my script. (it basically sends out RGB data and an attached name)
def sendout(myinfo, red = 0, green = 0, bleu= 0):
host = "127.0.0.1"
port = 7001
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)#not sure what this does, probably opens the socket
channel = 32
data = "/color %d " %(channel) + myinfo + " %d %d %d" %(red, green, bleu)
UDPSock.close# probably closes the socket
I’m having a very hard time getting data into blender from an external program. I’ve been looking at OSC examples but cant seem to get it right. It would be great to have a basic list thats updated with values continuously. It must be possible to do this in a similar way , with a socket. but apparently multi treading is involved…
I’m gonna keep struggling but it wold be fantastic if someone already solved this puzzle and would be able to help me out.
the socket() function creates a socket. It doesn’t actually listen or try to receive or sent, just tells the operating system to reserve that socket for you (there’s a finite number of sockets available, although its a large number). It also tells the os what type of socket.
In your example above, you create a DATAGRAM socket (also known as UDP). UDP sockets (unlike TCP/IP sockets) are connectionless, and do not have listen or connect type statements. UDP sockets can be thought of as ‘always’ listening for any noise coming in from anywhere.
you can use the recvfrom function (as opposed to sendto) to read data from a udp socket. Bear in mind only external programs sending in UDP mode to that particular port will get through. TCP/IP connections (SOCK_STREAM as opposed to SOCK_DGRAM) will not work.
If you need to receive data sent by another program thats talking using UDP, recvfrom should work for you Otherwise, if the other program is using TCP/IP, you’ll have to change the socket type to SOCK_STREAM instead of SOCK_DGRAM, and have your program call the listen function on the socket to get it to start listening for connections, followed by the “accept” function to have it wait for, and then accept a TCP connection.
Once you have a TCP connection accepted, you should be able to use the “recv” function to receive data from the other side (as opposed to the UDP recvfrom function).
You don’t actually need multithreading - its just that the recvfrom and recv and accept and listen functions block until something happens (like data coming through or a connection on the other side), so blender will ‘freeze’ until the data comes through, while it waits for the connection. You can probably Ctrl+C it to break the freeze.