Skip to content

Instantly share code, notes, and snippets.

@mattcox
Created August 13, 2018 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattcox/c96b10f2403965a014710c04f214dba2 to your computer and use it in GitHub Desktop.
Save mattcox/c96b10f2403965a014710c04f214dba2 to your computer and use it in GitHub Desktop.
This sample script demonstrates the basics for connecting to the preview socket, setting the resolution, and the pixel format, and reading back a floating point buffer of the image.
'''
This sample script demonstrates the basics for connecting to the preview
socket, setting the resolution, and the pixel format, and reading back a
floating point buffer of the image.
To test it, launch modo, and execute the command:
telnet.listen port:5678 raw:true
Then run the following in an external Python client.
'''
import socket
import struct
def testPreviewSocket(address, port):
preview = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
preview.connect((address, port))
preview.send(struct.pack("<I", 0x10000000|0x00001000)) # LXiPREVIEWMSG_REQ_SET_PROTOCOL
preview.send(struct.pack("<B", 0)) # 0
result = struct.unpack("<I", preview.recv(4))
if result[0] != (0x20000000|0x00001000): # LXiPREVIEWMSG_ACK_PROTOCOL_SET
print "LXiPREVIEWMSG_REQ_SET_FORMAT failed"
return
# Set the resolution.
preview.send(struct.pack("<I", 0x10000000|0x00002000)) # LXiPREVIEWMSG_REQ_SET_RES
preview.send(struct.pack("<I", 100)) # 100
preview.send(struct.pack("<I", 100)) # 100
result = struct.unpack("<I", preview.recv(4)) # Should be LXiPREVIEWMSG_ACK_RES_SET
if result[0] != (0x20000000|0x00002000):
print "LXiPREVIEWMSG_REQ_SET_RES failed"
return
# Set the format.
preview.send(struct.pack("<I", 0x10000000|0x00002001)) # LXiPREVIEWMSG_REQ_SET_FORMAT
preview.send(struct.pack("<I", 0x08)) # LXiIMD_FLOAT - floating point RGBA
result = struct.unpack("<I", preview.recv(4))
if result[0] != (0x20000000|0x00002001): # LXiPREVIEWMSG_ACK_FORMAT_SET
print "LXiPREVIEWMSG_REQ_SET_FORMAT failed"
return
# Rad the RGBA pixel buffer back.
preview.send(struct.pack("<I", 0x10000000|0x00003000)) # LXiPREVIEWMSG_REQ_SEND_FULL_FRAME
result = struct.unpack("<I", preview.recv(4))
if result[0] & (0x20000000|0x00003000|0x00000002): # LXiPREVIEWMSG_ACK_SEND_FULL_FRAME_RGBAFP
width = (struct.unpack("<I", preview.recv(4)))[0]
height = (struct.unpack("<I", preview.recv(4)))[0]
buffer = preview.recv((width*16)*height)
format = "<" + ("f" * ((width*4)*height))
pixels = struct.unpack(format, buffer)
print pixels
else
print "LXiPREVIEWMSG_REQ_SEND_FULL_FRAME failed"
return
telnet = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
telnet.connect(("127.0.0.1", 5678))
telnet.sendall("previewSocket.listen port:1234 open:true\0")
testPreviewSocket("127.0.0.1", 1234)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment