TouchDesigner 中处理tuio多点触控消息代码
以下是一个处理 Tuio 多点触控消息的 TouchDesigner 脚本示例:
# 导入Tuio模块
import tuio
# 设置Tuio客户端
client = tuio.Client('127.0.0.1', 3333)
# 创建一个字典来存储每个触点的信息
touchPoints = {}
# 定义一个处理Tuio消息的函数
def processTuioMessage(msg):
if msg.is_alive():
# 如果是一个新触点,创建一个新的字典条目
if msg.session_id not in touchPoints:
touchPoints[msg.session_id] = {}
# 更新触点的位置和速度
touchPoints[msg.session_id]['x'] = msg.xpos
touchPoints[msg.session_id]['y'] = msg.ypos
touchPoints[msg.session_id]['vx'] = msg.xspeed
touchPoints[msg.session_id]['vy'] = msg.yspeed
elif msg.is_fseq():
# 如果是一个帧序号消息,更新所有触点的速度
for id in touchPoints:
touchPoints[id]['vx'] = touchPoints[id]['vx'] * 0.7
touchPoints[id]['vy'] = touchPoints[id]['vy'] * 0.7
elif msg.is_dead():
# 如果是一个触点离开消息,删除字典条目
if msg.session_id in touchPoints:
del touchPoints[msg.session_id]
# 主循环
while True:
# 循环读取Tuio消息
for msg in client.get_messages():
processTuioMessage(msg)
# 在TouchDesigner中使用触点信息来做一些事情
for id in touchPoints:
# 例如,将每个触点的x坐标转换为0到1的范围,并将其作为参数传递给一个CHOP
value = touchPoints[id]['x'] / 1920.0
op('chop').par.value0 = value
此脚本将连接到本地 Tuio 服务器(IP 地址为 127.0.0.1,端口为 3333),并从服务器接收 Tuio 消息。它将使用一个字典来存储每个触点的位置和速度信息,并在 TouchDesigner 中使用这些信息来做一些事情(例如,将触点位置传递给一个 CHOP)
原文地址: https://www.cveoy.top/t/topic/dojr 著作权归作者所有。请勿转载和采集!