cleanup mana python plugin

This commit is contained in:
Florian Lüke 2024-12-27 18:49:49 +01:00
parent a9bfcf151c
commit 47926ce5e1
2 changed files with 21 additions and 17 deletions

View file

@ -21,7 +21,8 @@ struct Context
py::module usercode;
py::object userobject;
py::object py_begin_run;
py::object py_event_data;
py::object py_process_event;
py::object py_process_system_event;
py::object py_end_run;
std::vector<std::vector<py::object>> eventBuffers;
};
@ -29,7 +30,7 @@ struct Context
MANA_DEFINE_PLUGIN_INIT(init)
{
log_set_level(LOG_DEBUG);
log_set_level(LOG_INFO);
static Context g_ctx;
auto ctx = &g_ctx;
log_debug("init: ctx=%p", ctx);
@ -39,21 +40,18 @@ MANA_DEFINE_PLUGIN_INIT(init)
return ctx;
}
MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown)
{
(void)context;
log_debug("shutdown");
}
MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown) { log_info("shutdown: ctx=%p", context); }
MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run)
{
log_debug("begin_run: context=%p, descriptor_json=%s", context, descriptor_json);
log_info("begin_run: ctx=%p", context);
auto jRun = nlohmann::json::parse(descriptor_json);
auto ctx = reinterpret_cast<Context *>(context);
ctx->usercode.reload();
// TODO: check if the retrieved attributes are callable
ctx->py_begin_run = ctx->usercode.attr("begin_run");
ctx->py_event_data = ctx->usercode.attr("event_data");
ctx->py_process_event = ctx->usercode.attr("process_event");
ctx->py_process_system_event = ctx->usercode.attr("process_system_event");
ctx->py_end_run = ctx->usercode.attr("end_run");
ctx->py_begin_run(descriptor_json);
ctx->eventBuffers.clear();
@ -68,7 +66,7 @@ MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run)
MANA_DEFINE_PLUGIN_END_RUN(end_run)
{
log_debug("end: context=%p, descriptor_json=%s", context, descriptor_json);
log_info("end: ctx=%p", context);
auto ctx = reinterpret_cast<Context *>(context);
ctx->py_end_run(descriptor_json);
}
@ -77,6 +75,7 @@ MANA_DEFINE_PLUGIN_EVENT_DATA(process_event)
{
log_trace("event: ctx=%p, eventIndex=%d, arrayCount=%zu, totalBytes=%zu", context, eventIndex,
arrayCount, totalBytes);
auto ctx = reinterpret_cast<Context *>(context);
auto &buffers = ctx->eventBuffers.at(eventIndex);
assert(buffers.size() == arrayCount);
@ -86,13 +85,15 @@ MANA_DEFINE_PLUGIN_EVENT_DATA(process_event)
ctx->eventBuffers[eventIndex][ai] =
py::memoryview::from_memory(arraySpan.data(), arraySpan.size() * sizeof(float));
}
ctx->py_event_data(eventIndex, ctx->eventBuffers);
ctx->py_process_event(eventIndex, ctx->eventBuffers);
}
MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event)
{
log_trace("system_event: ctx=%p, size=%zu", context, size);
log_debug("system_event: ctx=%p, size=%zu", context, size);
auto ctx = reinterpret_cast<Context *>(context);
auto view = py::memoryview::from_memory(data, size * sizeof(uint32_t));
ctx->py_process_system_event(view);
}
extern "C" MANA_C_SINK_PLUGIN()

View file

@ -1,13 +1,16 @@
import json
import py_mana
import sys
def begin_run(runDescription: str):
print("begin_run")
print(f"python: begin_run - python version: {sys.version}")
def end_run(runDescription: str):
print("end_run")
print(f"python: end_run")
def event_data(eventIndex: int, dataArrays):
def process_event(eventIndex: int, dataArrays):
return
print(f"event[{eventIndex}]: {dataArrays}")
def process_system_event(data):
return
print(f"python: system_event: size={len(data)}")