Document the whole orca Python API and search every catalog from api_describe

This commit is contained in:
SoftFever committed 2026-11-03 23:10:10 +08:00
1 parent ee0314dee2
commit f776208c94
24 files changed
+2032 -770

No files matched your search

+192 -74
View File
@@ -432,95 +432,197 @@ void bind_python_api(pybind11::module_& m)
.value("Unknown", PluginCapabilityType::Unknown)
.export_values();
py::enum_<PluginResult>(m, "PluginResult", "Execution summary code")
py::enum_<PluginResult>(m, "PluginResult",
"Status of an ExecutionResult. What the host does with each value depends on the\n"
"method that returns it; see the execute() method of each capability base.")
.value("Success", PluginResult::Success)
.value("Skipped", PluginResult::Skipped)
.value("RecoverableError", PluginResult::RecoverableError)
.value("FatalError", PluginResult::FatalError)
.export_values();
py::enum_<LifecycleEvent>(m, "LifecycleEvent", "Application lifecycle moment passed to on_lifecycle_event")
.value("NewProject", LifecycleEvent::NewProject)
.value("ProjectOpened", LifecycleEvent::ProjectOpened)
.value("ProjectBeforeSave", LifecycleEvent::ProjectBeforeSave)
.value("ProjectAfterSave", LifecycleEvent::ProjectAfterSave)
.value("ProjectClosed", LifecycleEvent::ProjectClosed)
.value("ProjectDirtyChanged", LifecycleEvent::ProjectDirtyChanged)
.value("SliceStarted", LifecycleEvent::SliceStarted)
.value("SliceGeometryFinished", LifecycleEvent::SliceGeometryFinished)
.value("GCodeExportStarted", LifecycleEvent::GCodeExportStarted)
.value("GCodeExportFinished", LifecycleEvent::GCodeExportFinished)
.value("SlicingJobComplete", LifecycleEvent::SlicingJobComplete)
.value("ObjectAdded", LifecycleEvent::ObjectAdded)
.value("ObjectDeleted", LifecycleEvent::ObjectDeleted)
.value("ObjectTransformed", LifecycleEvent::ObjectTransformed)
.value("ObjectChanged", LifecycleEvent::ObjectChanged)
.value("ObjectRenamed", LifecycleEvent::ObjectRenamed)
.value("PlateCreated", LifecycleEvent::PlateCreated)
.value("PlateDeleted", LifecycleEvent::PlateDeleted)
.value("PlateSelected", LifecycleEvent::PlateSelected)
.value("PlateRenamed", LifecycleEvent::PlateRenamed)
.value("PresetSelected", LifecycleEvent::PresetSelected)
.value("PresetSaved", LifecycleEvent::PresetSaved)
.value("PrintStateChanged", LifecycleEvent::PrintStateChanged)
.value("DeviceOnline", LifecycleEvent::DeviceOnline)
.value("DeviceOffline", LifecycleEvent::DeviceOffline)
.value("DeviceDiscovered", LifecycleEvent::DeviceDiscovered)
.value("DeviceSelected", LifecycleEvent::DeviceSelected)
.value("UploadStarted", LifecycleEvent::UploadStarted)
.value("UploadFinished", LifecycleEvent::UploadFinished)
.value("PrintJobStarted", LifecycleEvent::PrintJobStarted)
.value("PrintJobFinished", LifecycleEvent::PrintJobFinished)
.value("SendJobStarted", LifecycleEvent::SendJobStarted)
.value("SendJobFinished", LifecycleEvent::SendJobFinished)
py::enum_<LifecycleEvent>(m, "LifecycleEvent",
"Application lifecycle moment passed to on_lifecycle_event(). The LifecycleEventContext\n"
"fields describe which events fill them.")
.value("NewProject", LifecycleEvent::NewProject,
"A new project is being started. It fires before the current project is reset, so a\n"
"ProjectClosed event for that project, when it had a file or objects, follows it.")
.value("ProjectOpened", LifecycleEvent::ProjectOpened, "A project was opened; name is its file path.")
.value("ProjectBeforeSave", LifecycleEvent::ProjectBeforeSave,
"The project is about to be saved; name is the target file path.")
.value("ProjectAfterSave", LifecycleEvent::ProjectAfterSave, "The project was saved; name is its file path.")
.value("ProjectClosed", LifecycleEvent::ProjectClosed,
"The current project, when it had a file or objects, was closed or reset; name is its file path\n"
"without the extension, or empty if it was never saved.")
.value("ProjectDirtyChanged", LifecycleEvent::ProjectDirtyChanged,
"The project gained or lost unsaved changes; see dirty and source.")
.value("SliceStarted", LifecycleEvent::SliceStarted,
"Slicing started, on the slicing thread; id and name identify the model.")
.value("SliceGeometryFinished", LifecycleEvent::SliceGeometryFinished,
"The slicing steps finished, before G-code export, on the slicing thread; id and name identify the\n"
"model.")
.value("GCodeExportStarted", LifecycleEvent::GCodeExportStarted,
"G-code export started, on the slicing thread; msg is the output path.")
.value("GCodeExportFinished", LifecycleEvent::GCodeExportFinished,
"G-code export ended; code is Ok or Error, and msg is the output path, followed by the error on\n"
"failure.")
.value("SlicingJobComplete", LifecycleEvent::SlicingJobComplete,
"The background slicing job ended, on the UI thread; code is Ok, Warn when cancelled, or Error with\n"
"the error in msg.")
.value("ObjectAdded", LifecycleEvent::ObjectAdded, "An object was added; name is its name.")
.value("ObjectDeleted", LifecycleEvent::ObjectDeleted, "An object was deleted; name is its name.")
.value("ObjectTransformed", LifecycleEvent::ObjectTransformed,
"Objects were moved, rotated, scaled, arranged or auto-oriented; msg says which, and name is the\n"
"object's name when exactly one object was moved, rotated or scaled.")
.value("ObjectChanged", LifecycleEvent::ObjectChanged,
"An object's geometry changed; name, id and, when known, index identify it.")
.value("ObjectRenamed", LifecycleEvent::ObjectRenamed,
"An object or one of its parts was renamed; name, previous_name, id, index and source (\"object\" or\n"
"\"volume\") describe it.")
.value("PlateCreated", LifecycleEvent::PlateCreated, "A plate was added; name and index identify it.")
.value("PlateDeleted", LifecycleEvent::PlateDeleted, "A plate was deleted; name and index identify it.")
.value("PlateSelected", LifecycleEvent::PlateSelected,
"Another plate became the current plate; name and index identify it.")
.value("PlateRenamed", LifecycleEvent::PlateRenamed,
"A plate was renamed; name, previous_name and index describe it.")
.value("PresetSelected", LifecycleEvent::PresetSelected,
"A preset was selected; name is the preset name and msg its type: \"print\", \"filament\",\n"
"\"printer\", \"sla_print\" or \"sla_material\".")
.value("PresetSaved", LifecycleEvent::PresetSaved,
"A preset was saved; name is the preset name and msg is \"new\" or \"overwrite\".")
.value("PrintStateChanged", LifecycleEvent::PrintStateChanged,
"A printer's print status changed; name is the device id and msg the new status.")
.value("DeviceOnline", LifecycleEvent::DeviceOnline, "A printer came online; name is its device id.")
.value("DeviceOffline", LifecycleEvent::DeviceOffline, "A printer went offline; name is its device id.")
.value("DeviceDiscovered", LifecycleEvent::DeviceDiscovered,
"A printer not known before was discovered on the network; name is its device id.")
.value("DeviceSelected", LifecycleEvent::DeviceSelected,
"The user selected a printer; name is its device id, or empty when the selection was cleared.")
.value("UploadStarted", LifecycleEvent::UploadStarted,
"An upload to a print host such as OctoPrint started; name is the uploaded file's name.")
.value("UploadFinished", LifecycleEvent::UploadFinished,
"A print host upload ended; code is Ok or Error, with the error in msg.")
.value("PrintJobStarted", LifecycleEvent::PrintJobStarted,
"Sending a print job to a printer started; name is the project name, device_id the printer and\n"
"source the sender.")
.value("PrintJobFinished", LifecycleEvent::PrintJobFinished,
"Sending a print job ended; code is Ok, Warn when cancelled, or Error, and msg is \"\", \"cancelled\",\n"
"\"failed\" or \"exception\".")
.value("SendJobStarted", LifecycleEvent::SendJobStarted,
"Sending a file to a printer's storage started; name is the project name and device_id the printer.")
.value("SendJobFinished", LifecycleEvent::SendJobFinished,
"Sending a file to a printer's storage ended; code and msg as for PrintJobFinished.")
.export_values();
py::enum_<LifecycleEvtCode>(m, "LifecycleEvtCode", "Outcome code accompanying a LifecycleEventContext")
.value("Ok", LifecycleEvtCode::Ok)
.value("Error", LifecycleEvtCode::Error)
.value("Warn", LifecycleEvtCode::Warn)
.value("Ok", LifecycleEvtCode::Ok, "The event happened, or the operation it reports succeeded.")
.value("Error", LifecycleEvtCode::Error, "The operation failed; msg holds the detail.")
.value("Warn", LifecycleEvtCode::Warn, "The operation was cancelled.")
.export_values();
py::class_<LifecycleEventContext>(m, "LifecycleEventContext", "Payload accompanying a LifecycleEvent")
py::class_<LifecycleEventContext>(m, "LifecycleEventContext",
"Payload accompanying a LifecycleEvent. Each field says which events fill it; a\n"
"field the event does not use is empty, -1 or False.")
.def(py::init<>())
.def_readonly("name", &LifecycleEventContext::name)
.def_readonly("code", &LifecycleEventContext::code)
.def_readonly("msg", &LifecycleEventContext::msg)
.def_readonly("id", &LifecycleEventContext::id)
.def_readonly("previous_name", &LifecycleEventContext::previous_name)
.def_readonly("device_id", &LifecycleEventContext::device_id)
.def_readonly("job_id", &LifecycleEventContext::job_id)
.def_readonly("source", &LifecycleEventContext::source)
.def_readonly("index", &LifecycleEventContext::index)
.def_readonly("dirty", &LifecycleEventContext::dirty);
.def_readonly("name", &LifecycleEventContext::name,
"The event's subject: a file path for Project* events (see ProjectClosed), the model name for\n"
"SliceStarted, SliceGeometryFinished, GCodeExport* and SlicingJobComplete, the object, plate or\n"
"preset name for Object*, Plate* and Preset* events, the device id for PrintStateChanged and\n"
"Device* events, the uploaded file name for Upload* events, and the project name for PrintJob*\n"
"and SendJob* events. Empty for NewProject and ProjectDirtyChanged.")
.def_readonly("code", &LifecycleEventContext::code,
"The LifecycleEvtCode. Only GCodeExportFinished, SlicingJobComplete, UploadFinished,\n"
"PrintJobFinished and SendJobFinished report anything but Ok: Error on failure, Warn when\n"
"cancelled.")
.def_readonly("msg", &LifecycleEventContext::msg,
"Detail text: the output path for GCodeExport* events, followed by the error on failure;\n"
"the error or \"cancelled\" for SlicingJobComplete; the error for UploadFinished; \"\",\n"
"\"cancelled\", \"failed\" or \"exception\" for PrintJobFinished and SendJobFinished; \"moved\",\n"
"\"rotated\", \"scaled\", \"arranged\" or \"auto_oriented\" for ObjectTransformed; the preset\n"
"type for PresetSelected; \"new\" or \"overwrite\" for PresetSaved; the new status for\n"
"PrintStateChanged; \"online\" or \"offline\" for DeviceOnline and DeviceOffline. Error text\n"
"is for people, not for parsing.")
.def_readonly("id", &LifecycleEventContext::id,
"A numeric id as a decimal string: the model's id for SliceStarted, SliceGeometryFinished,\n"
"GCodeExport* and SlicingJobComplete, the object's or part's id for ObjectChanged and\n"
"ObjectRenamed.")
.def_readonly("previous_name", &LifecycleEventContext::previous_name,
"The old name for ObjectRenamed and PlateRenamed.")
.def_readonly("device_id", &LifecycleEventContext::device_id,
"The target printer's device id for PrintJob* and SendJob* events. PrintStateChanged and\n"
"Device* events carry the device id in name instead.")
.def_readonly("job_id", &LifecycleEventContext::job_id,
"The task id for PrintJob* events sent through the multi-printer task queue (source\n"
"\"task_manager\").")
.def_readonly("source", &LifecycleEventContext::source,
"Where the event came from: \"object\" or \"volume\" for ObjectRenamed, \"geometry\" for\n"
"ObjectChanged, \"print_job\" or \"task_manager\" for PrintJob* events, \"send_job\" for\n"
"SendJob* events, and for ProjectDirtyChanged the change that flipped the state: \"plater\",\n"
"\"presets\", \"undo_redo\", \"save\" or \"batch\".")
.def_readonly("index", &LifecycleEventContext::index,
"A 0-based index: the plate for Plate* events, the object for ObjectChanged (when known) and\n"
"object renames, and the part within its object for part renames.")
.def_readonly("dirty", &LifecycleEventContext::dirty,
"For ProjectDirtyChanged, True when the project now has unsaved changes.");
py::class_<PluginContext>(m, "PluginContext", "Context shared with plugin entry points")
py::class_<PluginContext>(m, "PluginContext",
"Holds the OrcaSlicer version. No OrcaSlicer call passes one to a plugin; slicing steps\n"
"read the version from SlicingPipelineContext.orca_version.")
.def(py::init<>())
.def_readwrite("orca_version", &PluginContext::orca_version);
.def_readwrite("orca_version", &PluginContext::orca_version, "The OrcaSlicer version string.");
py::class_<ExecutionResult>(m, "ExecutionResult", "Structured execution outcome")
.def(py::init<>())
.def(py::init<PluginResult, std::string, std::string>())
.def_readwrite("status", &ExecutionResult::status)
.def_readwrite("message", &ExecutionResult::message)
.def_readwrite("data", &ExecutionResult::data)
.def_static("success", &ExecutionResult::success, py::arg("message") = std::string(), py::arg("data") = std::string())
.def_static("skipped", &ExecutionResult::skipped, py::arg("message") = std::string())
.def_static("failure", &ExecutionResult::failure, py::arg("status"), py::arg("message"), py::arg("data") = std::string());
py::class_<ExecutionResult>(m, "ExecutionResult",
"Outcome a capability's execute() returns: a PluginResult status, a message for the\n"
"user or the log, and optional data. Build one with success(), skipped() or failure().")
.def(py::init<>(), "A Success result with no message or data.")
.def(py::init<PluginResult, std::string, std::string>(), py::arg("status"), py::arg("message"),
py::arg("data"), "A result with the given status, message and data.")
.def_readwrite("status", &ExecutionResult::status, "The PluginResult status.")
.def_readwrite("message", &ExecutionResult::message,
"Text shown to the user or logged, depending on the capability; may be empty.")
.def_readwrite("data", &ExecutionResult::data,
"Extra result text for the caller, such as a JSON string. The Plugins dialog logs it; the host\n"
"does not otherwise read it.")
.def_static("success", &ExecutionResult::success, py::arg("message") = std::string(),
py::arg("data") = std::string(), "Return a Success result with an optional message and data.")
.def_static("skipped", &ExecutionResult::skipped, py::arg("message") = std::string(),
"Return a Skipped result, for a run that had nothing to do, with an optional message.")
.def_static("failure", &ExecutionResult::failure, py::arg("status"), py::arg("message"),
py::arg("data") = std::string(),
"Return a failed result: status is PluginResult.RecoverableError or PluginResult.FatalError,\n"
"message says what went wrong, and data is optional.");
// Config lives at the capability level, not as a global orca.* function: the host reads the
// owning (plugin_key, capability) straight off the instance the call arrived on, so a
// capability can only ever address its own config and never has to name itself.
// Registered on the base, so every capability type (script/gcode/printer-agent) inherits it.
py::class_<PluginCapabilityInterface, PyPluginInterfaceTrampoline, std::shared_ptr<PluginCapabilityInterface>>(m, "PythonPluginBase")
py::class_<PluginCapabilityInterface, PyPluginInterfaceTrampoline, std::shared_ptr<PluginCapabilityInterface>>(
m, "PythonPluginBase",
"Base of every capability class, holding what all capabilities share: the required get_name(), the\n"
"on_load(), on_unload() and on_lifecycle_event() hooks, and the capability's config. Subclass one of\n"
"the typed bases rather than this class: orca.script.ScriptPluginCapabilityBase,\n"
"orca.pages.PagesPluginCapabilityBase, orca.printer_agent.PrinterAgentBase,\n"
"orca.slicing.SlicingPipelineCapabilityBase or orca.resource_texture.ResourceTexturePluginCapabilityBase.")
.def(py::init<>())
.def("get_name", &PluginCapabilityInterface::get_name)
.def("get_type", &PluginCapabilityInterface::get_type)
.def("on_load", &PluginCapabilityInterface::on_load)
.def("on_unload", &PluginCapabilityInterface::on_unload)
.def("on_lifecycle_event", &PluginCapabilityInterface::on_lifecycle_event,
"Override to react to an application lifecycle moment (LifecycleEvent) and its\n"
"LifecycleEventContext payload. Available on every capability type.")
.def("get_name", &PluginCapabilityInterface::get_name,
"Required. Return this capability's name. The host reads it once when the plugin loads, shows it\n"
"in the UI and uses it to refer to the capability in presets and configs, so it must stay the same.\n"
"It must not contain ';' and must be unique among loaded capabilities of the same type.")
.def("get_type", &PluginCapabilityInterface::get_type,
"Return this capability's PluginType. Each typed capability base already returns its own type, and\n"
"the host reads it once when the plugin loads.")
.def("on_load", &PluginCapabilityInterface::on_load,
"Called once when the plugin loads, after all of its capabilities have been created. Raising an\n"
"exception fails the plugin load. The default does nothing.")
.def("on_unload", &PluginCapabilityInterface::on_unload,
"Called once when the plugin unloads (disabled, removed, reloaded or at exit), and after its own\n"
"on_load() raised. Exceptions are logged and ignored. The default does nothing.")
.def("on_lifecycle_event", &PluginCapabilityInterface::on_lifecycle_event, py::arg("event"), py::arg("ctx"),
"Called for every application lifecycle moment, on every loaded and enabled capability whatever\n"
"its type. event is a LifecycleEvent and ctx the LifecycleEventContext describing it. It runs on\n"
"the thread that raised the event, a background thread for slicing, export, upload and print-job\n"
"events. The return value is ignored, and exceptions are logged and ignored. The default does\n"
"nothing.")
.def("has_config_ui", &PluginCapabilityInterface::has_config_ui,
"Override to return True to replace the host's default JSON editor with your own HTML\n"
"UI, returned by get_config_ui(). Every capability is configurable and appears in the\n"
@@ -569,11 +671,17 @@ void bind_python_api(pybind11::module_& m)
"The plugin key, capability name and version are supplied by the host. Returns False if\n"
"the string is not valid JSON, or if the config file could not be written.");
// Expose the package marker base as orca.base. @orca.plugin later verifies that the
// decorated class derives from this exact pybind-registered C++ type.
py::class_<PyPluginPackage, PyPluginPackageTrampoline>(m, "base")
// @orca.plugin verifies that the decorated class derives from this exact pybind-registered C++ type.
py::class_<PyPluginPackage, PyPluginPackageTrampoline>(
m, "base",
"Base of a plugin's package class. Subclass it once per plugin, decorate the subclass with\n"
"@orca.plugin, and override register_capabilities().")
.def(py::init<>())
.def("register_capabilities", &PyPluginPackage::register_capabilities);
.def("register_capabilities", &PyPluginPackage::register_capabilities,
"Called once while OrcaSlicer loads the plugin, after its module is imported. Call\n"
"orca.register_capability() for each capability class, and orca.request_permissions() and\n"
"orca.register_operation() as needed. Raising an exception fails the plugin load. The default\n"
"registers nothing.");
BOOST_LOG_TRIVIAL(debug) << "Registering embedded Python plugin type bindings";
@@ -607,7 +715,13 @@ void bind_python_api(pybind11::module_& m)
g_pending_capabilities[g_active_plugin_key].push_back(std::move(plugin_cls));
BOOST_LOG_TRIVIAL(debug) << "Registered Python plugin capability class for key " << g_active_plugin_key;
},
R"pbdoc(Register a PluginCapability subclass while OrcaSlicer loads your module.)pbdoc");
py::arg("capability_class"),
R"pbdoc(Register a capability class while OrcaSlicer loads your module, normally from register_capabilities().
Pass the class, not an instance: OrcaSlicer creates one instance with no arguments after
register_capabilities() returns. The class must derive from a capability base such as
orca.script.ScriptPluginCapabilityBase. Raises ValueError outside plugin loading or for a class that is
not a capability.)pbdoc");
m.def(
"register_operation",
@@ -765,7 +879,11 @@ instance is created. Denying the request aborts the plugin load.)pbdoc");
slot = cls;
}
return cls; // decorator returns the class unchanged
}, R"pbdoc(Mark the single plugin package class (the orca.base subclass) for this file.)pbdoc");
}, py::arg("cls"),
R"pbdoc(Decorator marking the plugin's package class, its orca.base subclass, and returning it unchanged.
Use it on exactly one class per plugin. Raises ValueError outside plugin loading, on something that is
not a class, on a class that does not derive from orca.base, or on a second package class.)pbdoc");
}