我希望/需要使此代码作为脚本在 OBS 中运行。我把这个OSC Sender for OBS 看做它的名字所说的:它发送 OSC 消息。但是,在代码内部,服务器部分被注释了,因为它不起作用......开箱即用。运行 OBS 需要LD_PRELOAD=/path/libpython3.7.so
.
我修改了代码,但是(未注释的行)但是,当服务器打开时,OBS 保持不可用,被阻止。所以,我尝试了python-osc的“异步服务器”-“并发模式”。我从它的 wiki中获取示例(它在控制台中工作)并与其他脚本混合。我做了一个很好的代码......但它让一些奇怪的东西。这是代码:
"""
"""
# osc data
import argparse
import random
import time
import math
import asyncio
from pythonosc import osc_message_builder
from pythonosc import udp_client
from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import AsyncIOOSCUDPServer
client = None
server = None
#obs !
import obspython as obs
pleaseLog = False
host = None
serverPort = None
# transport = None
async def loop():
"""Example main loop that only runs for 10 iterations before finishing"""
for i in range(10):
print(f"Loop {i}")
await asyncio.sleep(.5)
async def init_main():
# global transport
server = AsyncIOOSCUDPServer((host, serverPort), dispatcher, asyncio.get_event_loop())
transport, protocol = await server.create_serve_endpoint() # Create datagram endpoint and start serving
await loop() # Enter main loop of program
transport.close() # Clean up serve endpoint
def handleOSC(*args):
for i in args:
print(i)
# defines script description
def script_description():
return '''Send OSC data when source is activated if source name begins with /'''
# defines user properties
def script_properties():
global props
props = obs.obs_properties_create()
obs.obs_properties_add_text(props, "host", "Host IP", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_int(props, "clientPort", "Host port", 1, 400000, 1)
obs.obs_properties_add_bool(props, "logOscOutput", "Log OSC output")
obs.obs_properties_add_int(props, "serverPort", "Listen port", 1, 400000, 1)
return props
def script_defaults(settings):
obs.obs_data_set_default_string(settings, "host", "127.0.0.1")
obs.obs_data_set_default_int(settings, "clientPort", 10000)
obs.obs_data_set_default_int(settings, "serverPort", 10001)
def source_activated(cd):
global pleaseLog
source = obs.calldata_source(cd, "source")
if source is not None:
name = obs.obs_source_get_name(source)
if name[0] == "/":
client.send_message(name, 1)
if (pleaseLog):
print("send " + name)
def script_load(settings):
global dispatcher
global host
sh = obs.obs_get_signal_handler()
obs.signal_handler_connect(sh, "source_activate", source_activated)
dispatcher = Dispatcher()
dispatcher.map("/*", handleOSC)
# def script_unload():
# global transport
# print(f'script_unload(settings)')
# transport.close() # Clean up serve endpoint
def script_update(settings):
global host
global client
global server
global clientPort
global serverPort
global pleaseLog
pleaseLog = obs.obs_data_get_bool(settings, "logOscOutput")
host = obs.obs_data_get_string(settings, "host")
clientPort = obs.obs_data_get_int(settings, "clientPort")
serverPort = obs.obs_data_get_int(settings, "serverPort")
# Client
client = udp_client.SimpleUDPClient(host, clientPort)
print("target set to "+host+":"+str(clientPort)+"")
# Server
print("serving in "+host+":"+str(serverPort)+"")
asyncio.run(init_main())
我不是程序员,所以 asyncio 对我来说是一团糟。我不明白。但我知道它的作用以及我希望它做什么。
加载脚本时(在 中script_update(settings)
),它会打开服务器并运行loop()
函数。这个函数是一个 5 秒的循环,打印一些文本。如果我从 PureData 连接到正确的端口并发送一些 OSC 消息,这些消息会到达 OBS ......但是当循环完成时,所有这些消息都会一起下降。同时,什么都没有,OBS 被阻止并且脚本日志中没有任何内容。
如果我在控制台中运行 python-osc asyncio 示例代码,while 循环正在执行,每个Loop 0
,Loop 1
等,并且每条消息都到达并且所有消息都在正确的时间打印。
我应该如何让这段代码工作?我需要打开该服务器并同时运行更多代码。我正在像游戏引擎一样使用 OBS。