When prototyping easily, you can easily create it by leaving complicated processing to Python and drawing to Processing. When I was looking for a good way to cooperate, I could simply make it with socket communication. Please let me know if there is a better way.
Below is a sample code that only sends a text message unilaterally from Python to Processing. If you start the Processing side first and then execute the Python side, a message will be output to the standard output of Processing.
conenctWithPython.pde
import processing.net.*;
int port = 10001; //Set an appropriate port number
Server server;
void setup() {
server = new Server(this, port);
println("server address: " + server.ip()); //Output IP address
}
void draw() {
Client client = server.available();
if (client !=null) {
String whatClientSaid = client.readString();
if (whatClientSaid != null) {
println(whatClientSaid); //Output message from Python
}
}
}
toProcessing.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
host = "127.0.0.1" #IP address of the server launched by Processing
port = 10001 #Port number set in Processing
if __name__ == '__main__':
socket_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Creating an object
socket_client.connect((host, port)) #Connect to server
#socket_client.send('Message to send') #Send data Python2
socket_client.send('Message to send'.encode('utf-8')) #Send data Python3
Recommended Posts