Python API¶
Automatically generated from the source docstrings with
mkdocstrings. Run mkdocs serve to render
this page locally.
Plugin¶
The OctoPrint plugin implementation: mixins, settings, webcam provider and the simple API.
BambucamPlugin ¶
BambucamPlugin()
Bases: TimelapseOpsMixin, AutoSyncMixin, IpcamSyncMixin, RawFilesOpsMixin, AutoLedMixin, StartupPlugin, ShutdownPlugin, SettingsPlugin, TemplatePlugin, AssetPlugin, SimpleApiPlugin, EventHandlerPlugin, WebcamProviderPlugin
Manage the BambuCam webcamd daemon and expose it to OctoPrint.
Note: several OctoPrint mixin hooks (the SimpleApiPlugin methods,
is_template_autoescaped) are annotated with overly narrow return types
in OctoPrint's base classes (-> None / Literal[False]) even though
the real plugin API expects Flask responses / dicts / bools. We return the
correct values here; reportIncompatibleMethodOverride is disabled in
pyrightconfig.json so those accurate overrides do not get flagged.
Source code in octoprint_bambucam/__init__.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
get_additional_backup_excludes ¶
get_additional_backup_excludes(excludes, *args, **kwargs)
Exclude bulky render dirs from OctoPrint backups.
Chunks are re-harvestable from the printer's /ipcam folder and
the work/trash dirs are transient, so backing them up only bloats
the archive. When the user excludes "timelapse" from the backup the
rendered videos are gone anyway, so the whole render tree (incl.
thumbs/metadata) is dropped for consistency.
Source code in octoprint_bambucam/__init__.py
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 | |
get_timelapse_extensions ¶
get_timelapse_extensions()
Teach OctoPrint's native Timelapse tab to also list .avi files.
Bambu P1/A1 printers record timelapses as .avi (alongside .mp4
on some firmware). OctoPrint's built-in tab only recognizes
mpg/mpeg/mp4/m4v/mkv (see octoprint.timelapse._extensions), so a
copied .avi would land in the folder but never appear. Registering
this octoprint.timelapse.extensions hook adds avi to the
allow-list. .mp4 is already covered upstream.
Source code in octoprint_bambucam/__init__.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 | |
get_update_information ¶
get_update_information()
Software-update hook config consumed by OctoPrint's updater.
Source code in octoprint_bambucam/__init__.py
980 981 982 983 984 985 986 987 988 989 990 991 992 993 | |
on_event ¶
on_event(event, payload) -> None
Fan a printer event out to the post-print pipelines.
AutoSyncMixin.on_event drives the render-gate flags and the serial
post-print pipeline; on_ipcam_event snapshots the /ipcam
baseline at PrintStarted for pipeline stage 3; on_auto_led_event
switches the chamber light on/off around the job.
Called explicitly rather than through super() because the mixins
each own the hook name; adding a consumer means adding a call here.
Source code in octoprint_bambucam/__init__.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
on_settings_migrate ¶
on_settings_migrate(target, current)
Drop settings that no longer back anything (OctoPrint hook).
current is None for a config written before this plugin had a
settings version — which is every install carrying the dead keys.
Source code in octoprint_bambucam/__init__.py
293 294 295 296 297 298 299 300 301 302 303 304 305 | |
options: show_root_heading: true members_order: source
Daemon supervisor¶
The supervised process manager for the vendored MJPEG daemon.
WebcamdManager ¶
WebcamdManager(logger, on_state_change=None, http_logger=None)
Supervise the vendored webcamd child process.
Handles start/stop/restart, pumps the child's output into the plugin logger, and runs a generation-tracked watchdog that restarts the daemon with exponential backoff after unexpected exits while guaranteeing only one daemon ever runs at a time.
:param logger: plugin logger; child output is forwarded here :param on_state_change: optional callable(state: str, detail: dict) called on "started", "stopped", "crashed", "gave_up" :param http_logger: optional logger for the daemon's --loghttp output; those lines are routed here instead of the main plugin log
Source code in octoprint_bambucam/daemon.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
fetch_info ¶
fetch_info()
GET /?info from the daemon (always via loopback).
Returns a dict or None. The vendored daemon already redacts the access code, but strip it again here so the frontend can never receive it even if the vendor patch is lost in an upstream update.
Source code in octoprint_bambucam/daemon.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
is_running ¶
is_running()
Return True while the daemon process is alive.
Source code in octoprint_bambucam/daemon.py
132 133 134 135 | |
port_in_use
staticmethod
¶
port_in_use(port, bind_address='127.0.0.1')
Return True if the given port is already bound on the host.
Source code in octoprint_bambucam/daemon.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
restart ¶
restart(config=None)
Stop and start again, reusing the last config unless one is given.
Returns (ok, error_message).
Source code in octoprint_bambucam/daemon.py
123 124 125 126 127 128 129 130 | |
start ¶
start(config)
Start the daemon with the given config dict (see plugin settings).
Returns (ok, error_message).
Source code in octoprint_bambucam/daemon.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
status ¶
status()
Return a snapshot dict of daemon state for the API/frontend.
Source code in octoprint_bambucam/daemon.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
stop ¶
stop()
Terminate the running daemon (if any) and supersede its watchdog.
Source code in octoprint_bambucam/daemon.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
test_connection
staticmethod
¶
test_connection(hostname, access_code, timeout=10.0)
Probe the printer camera port using the §2.2 handshake.
Returns (ok, reason) with reason in ("ok", "unreachable", "auth_failed", "timeout", "error").
Source code in octoprint_bambucam/daemon.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
options: show_root_heading: true members_order: source
Timelapse operations¶
Copy/move/delete batches and the .avi → .mp4 transcode, mixed into the
plugin.
TimelapseOpsMixin ¶
Timelapse transfer/convert machinery for BambucamPlugin.
options: show_root_heading: true members_order: source
Auto-sync¶
Pulls the new timelapse after a print, once the printer and OctoPrint are idle.
AutoSyncMixin ¶
Trigger an automatic timelapse pull when a print finishes.
handle_pipeline_status ¶
handle_pipeline_status() -> flask.Response
Return the pipeline busy flag + harvest progress (UI polling).
Source code in octoprint_bambucam/autosync.py
167 168 169 | |
note_own_movie ¶
note_own_movie(movie_path: str) -> None
Record a movie path we are about to fire MovieDone for, so the
event handler ignores it (otherwise our own copy would look like an
OctoPrint render and flip the gate flag).
Source code in octoprint_bambucam/autosync.py
115 116 117 118 119 120 | |
on_event ¶
on_event(event, payload) -> None
Track OctoPrint's render state and trigger auto-sync on print end.
Source code in octoprint_bambucam/autosync.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
options: show_root_heading: true members_order: source
FTPS client¶
Implicit-TLS FTPS client for the printer's SD-card timelapses.
BambuTimelapseFtp ¶
BambuTimelapseFtp(logger, hostname: str, access_code: str, *, timeout: int = CONNECT_TIMEOUT)
Lockable, context-managed FTPS service for the printer's timelapses.
One instance manages one connection (the printer allows ~1 at a time).
Open it as a context manager (or via open() / close()) and call
list_timelapses / download / delete on the open session.
All operations are serialized by a per-instance lock.
Source code in octoprint_bambucam/ftp.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
close ¶
close() -> None
Best-effort quit() then close().
Source code in octoprint_bambucam/ftp.py
184 185 186 187 188 189 190 191 192 | |
delete ¶
delete(name: str) -> None
Issue DELE <name> on the open session.
Rejects unsafe names first. Raises on FTP error so the caller can classify/report. The caller (not this method) enforces the §5.9 guards (print-active, confirmation, copy-verified-before-delete).
Source code in octoprint_bambucam/ftp.py
340 341 342 343 344 345 346 347 348 349 350 | |
download ¶
download(name: str, dest_path: str, *, progress_cb: Optional[Callable[[int, Optional[int]], None]] = None) -> int
Stream RETR <name> to dest_path atomically; return bytes.
Writes to a temp file in the same folder as dest_path (so the
final rename is an atomic same-device move), opened with
O_EXCL|O_NOFOLLOW so a planted symlink can't redirect the write and
an existing file is never clobbered. On any failure the partial temp
file is removed. The caller is responsible for building a safe,
contained dest_path (see __init__.py / plan §5.7).
progress_cb(transferred, total) is invoked from the chunk callback.
Source code in octoprint_bambucam/ftp.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
fetch_thumbnail ¶
fetch_thumbnail(name: str) -> Optional[bytes]
Return the JPEG preview bytes for video name, or None.
The printer stores /timelapse/thumbnail/<videostem>.jpg. Streams it
into memory (capped at :data:MAX_THUMBNAIL_BYTES). A missing thumbnail
is a normal None result, not an error. Rejects unsafe names first.
Source code in octoprint_bambucam/ftp.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
list_timelapses ¶
list_timelapses() -> list[dict]
Return [{"name", "size", "date"}] for the timelapse videos.
Tries MLSD (size + modify time) first, falls back to NLST +
size(). An empty or missing folder is a valid empty result, not an
error. Filters to :data:VIDEO_EXTENSIONS. Lists LIST_DIR so
subclasses (BambuIpcamFtp) can point at another folder.
Source code in octoprint_bambucam/ftp.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
open ¶
open() -> None
Connect and log in. Idempotent within a batch.
Source code in octoprint_bambucam/ftp.py
145 146 147 148 | |
remote_size ¶
remote_size(name: str) -> Optional[int]
Return ftp.size(name) for the disk-space pre-check / progress.
Source code in octoprint_bambucam/ftp.py
264 265 266 267 268 269 270 271 | |
options: show_root_heading: true members_order: source
MQTT client¶
One-shot MQTT client for the printer's light control.
BambuMqttClient ¶
BambuMqttClient(logger, hostname: str, access_code: str, serial: str, *, timeout: int = CONNECT_TIMEOUT)
One-shot MQTT control client for a single Bambu printer.
Build it with the printer's host/access_code/serial, then call
:meth:set_chamber_light. Each call opens its own short-lived connection
(the printer tolerates few concurrent clients), serialized per instance.
Source code in octoprint_bambucam/mqtt.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
set_chamber_light ¶
set_chamber_light(on: bool) -> None
Turn the printer's LED on or off. Raises :class:MqttError.
Source code in octoprint_bambucam/mqtt.py
140 141 142 | |
options: show_root_heading: true members_order: source
MQTT monitor¶
Standing MQTT connection that tracks the printer's real light state and publishes commands over the same socket. Used only as a fallback when BambuConnector is not the active connection.
BambuMqttMonitor ¶
BambuMqttMonitor(logger, hostname: str, access_code: str, serial: str, on_change: Callable[[Optional[bool]], None])
Long-lived MQTT connection that tracks the printer's light state.
Subscribes to device/{serial}/report, parses the chamber_light
state out of each report, and invokes on_change(bool) whenever it
changes. Also publishes ledctrl commands over the same connection
(:meth:set_chamber_light), so only one socket is ever open.
The connection runs on paho's background loop with automatic reconnect.
Start it with :meth:start and always pair it with :meth:stop. The last
known state is available via :meth:current_state (None until the
first report arrives).
Source code in octoprint_bambucam/mqtt.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
current_state ¶
current_state() -> Optional[bool]
Return the last known chamber-light state, or None if unknown.
Source code in octoprint_bambucam/mqtt.py
230 231 232 233 | |
set_chamber_light ¶
set_chamber_light(on: bool) -> None
Publish a ledctrl command over the live connection.
Source code in octoprint_bambucam/mqtt.py
291 292 293 294 295 296 297 298 299 300 301 | |
start ¶
start() -> None
Connect, subscribe to reports, and request a full state dump.
Raises :class:MqttError if the printer is unreachable, the serial is
missing, or the access code is rejected.
Source code in octoprint_bambucam/mqtt.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
stop ¶
stop() -> None
Disconnect and tear down the background loop. Idempotent.
Source code in octoprint_bambucam/mqtt.py
303 304 305 306 307 308 | |
options: show_root_heading: true members_order: source
Connector light path¶
Drives the light over OctoPrint-BambuConnector's already-open connection (its
bpm.BambuPrinter.light_state), avoiding a second socket to the printer's
slot-limited broker. The preferred path for light control + state.
connector_led ¶
Drive the printer light through OctoPrint-BambuConnector's connection.
The Bambu A1 mini's local MQTT broker tolerates only ~1-2 connections, and
OctoPrint-BambuConnector keeps one open permanently to drive the printer.
Opening a second connection from BambuCam therefore fails intermittently with
a TLS handshake timeout — which is what the standalone
:mod:octoprint_bambucam.mqtt client hits.
When BambuConnector is the active connection we instead drive the light over its
already-open connection: it wraps the printer with bambu_printer_manager
(bpm), whose BambuPrinter object exposes a light_state property
(getter = real state, setter publishes the toggle over the open socket). We
reach it through OctoPrint 2.0 internals
(printer.current_connection / printer._connection →
ConnectedBambuPrinter._client → bpm.BambuPrinter).
Every access is best-effort and defensive: any missing attribute or wrong type means "not available" rather than a crash, so the caller can fall back to the standalone MQTT client.
available ¶
available(printer, logger=None) -> bool
True when the light can be driven via BambuConnector's connection.
Source code in octoprint_bambucam/connector_led.py
110 111 112 | |
current_state ¶
current_state(printer, logger=None) -> Optional[bool]
Return the real light state from BambuConnector, or None.
Reads bpm.BambuPrinter.light_state (a bool). Any missing attribute or
error yields None (state unknown).
Source code in octoprint_bambucam/connector_led.py
134 135 136 137 138 139 140 141 142 143 144 145 146 | |
set_chamber_light ¶
set_chamber_light(printer, on: bool, logger=None) -> bool
Set the printer light over BambuConnector's connection.
Returns True on success, False when the connector path is
unavailable (so the caller can fall back to the standalone client). Never
raises.
Source code in octoprint_bambucam/connector_led.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
options: show_root_heading: true members_order: source filters: ["!^_"]
Transcoder¶
Wraps OctoPrint's ffmpeg for the .avi → .mp4 re-encode.
TimelapseTranscoder ¶
TimelapseTranscoder(logger, *, ffmpeg_path: Optional[str], videocodec: str = 'libx264', bitrate: str = '10000k', threads: int = 1, thumbnail_commandline: Optional[str] = None, runner=None)
Re-encode an .avi to .mp4 using OctoPrint's ffmpeg settings.
Construct with the resolved ffmpeg parameters (so the plugin pulls them
from self._settings and this class stays unit-testable with a fake
runner). ffmpeg_path of None/empty means ffmpeg is not configured
and :meth:available is False.
Source code in octoprint_bambucam/transcode.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
available ¶
available() -> bool
True when ffmpeg is configured (path set in OctoPrint webcam).
Source code in octoprint_bambucam/transcode.py
62 63 64 | |
create_gcode_thumbnail ¶
create_gcode_thumbnail(src_png: str, thumb_path: str) -> bool
Write thumb_path from a gcode preview PNG; return success.
Scales the slicer plate preview down to a list-friendly size. Best- effort — a failure returns False so the caller can fall back to a video-frame thumbnail.
Source code in octoprint_bambucam/transcode.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
create_thumbnail ¶
create_thumbnail(mp4_path: str, thumb_path: str) -> bool
Best-effort <mp4>.thumb.jpg from the last frame.
A timelapse's first frame is an empty bed (a blank white tile in the
native Timelapse list); the last frame shows the finished print, which
makes a far more useful thumbnail. We therefore grab a frame near the
end with our own ffmpeg command (-sseof), independent of whether
OctoPrint has an ffmpegThumbnailCommandline configured — on recent
OctoPrint that template is often unset, so relying on it produced no
thumbnail at all. Returns True on success; a failure is non-fatal.
Source code in octoprint_bambucam/transcode.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
status ¶
status() -> dict
Report ffmpeg availability for the settings indicator.
configured is True when OctoPrint has an ffmpeg path set;
executable additionally checks that the file exists and is runnable
(a stale/typo'd path is configured-but-not-executable). path is the
configured path (or "") so the UI can show it.
Source code in octoprint_bambucam/transcode.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
transcode ¶
transcode(avi_path: str, mp4_path: str, *, progress_cb: Optional[Callable[[int], None]] = None) -> None
Re-encode avi_path → mp4_path (H.264), atomically.
Writes to a temp .part next to mp4_path then renames, so a
failed/interrupted run never leaves a half .mp4 behind. Raises
:class:TranscodeError on any failure (caller keeps the .avi and
reports a warning). progress_cb(percent) is invoked as ffmpeg
reports progress (0-100, whole percents).
Source code in octoprint_bambucam/transcode.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
options: show_root_heading: true members_order: source
Bambu Connector probe¶
Best-effort discovery of connection data from OctoPrint-BambuConnector.
ConnectorInfo ¶
ConnectorInfo(*, installed: bool = False, hostname: Optional[str] = None, access_code: Optional[str] = None, serial: Optional[str] = None)
What we could discover about the printer from BambuConnector.
available is True only when the plugin is installed and we found at
least a host + access code we can reuse. hostname/access_code/
serial may be None individually.
Source code in octoprint_bambucam/bambu_connector.py
31 32 33 34 35 36 37 38 39 40 41 42 | |
as_dict ¶
as_dict() -> dict
Serialize for the frontend. The access code is never sent back to the browser as plaintext — only whether one is present.
Source code in octoprint_bambucam/bambu_connector.py
49 50 51 52 53 54 55 56 57 58 | |
options: show_root_heading: true members_order: source