Skip to content

bambuprinter

BambuPrinter

BambuPrinter is the main class within bambu-printer-manager for interacting with and managing your Bambu Lab 3d printer. It provides an object oriented abstraction layer between your project and the mqtt and ftps based mechanisms in place for communicating with your printer.

__init__(config=BambuConfig())

Sets up all internal storage attributes for BambuPrinter and bootstraps the logging engine.

Parameters
  • config : Optional[BambuConfig] = BambuConfig()
Attributes
  • _mqtt_client_thread: PRIVATE Thread handle for the mqtt client thread
  • _watchdog_thread: PRIVATE Thread handle for the watchdog thread
  • _internalExcepton: READ ONLY Returns the underlying Exception object if a failure occurred.
  • _lastMessageTime: READ ONLY Epoch timestamp (in seconds) for the last time an update was received from the printer.
  • _recent_update: READ ONLY Indicates that a message from the printer has been recently processed.
  • _config: READ/WRITE bambuconfig.BambuConfig Configuration object associated with this instance.
  • _state: READ/WRITE bambutools.PrinterState enum reports on health / status of the connection to the printer.
  • _client: READ ONLY Provides access to the underlying paho.mqtt.client library.
  • _on_update: READ/WRITE Callback used for pushing updates. Includes a self reference to BambuPrinter as an argument.
  • _bed_temp: READ ONLY The current printer bed temperature.
  • _bed_temp_target: READ/WRITE The target bed temperature for the printer.
  • _bed_temp_target_time: READ ONLY Epoch timetamp for when target bed temperature was last set.
  • _tool_temp: READ ONLY The current printer tool temperature.
  • _tool_temp_target: READ/WRITE The target tool temperature for the printer.
  • _tool_temp_target_time: READ ONLY Epoch timetamp for when target tool temperature was last set.
  • _chamber_temp READ/WRITE Not currently integrated but can be used as a stub for external chambers.
  • _chamber_temp_target READ/WRITE Not currently integrated but can be used as a stub for external chambers.
  • _chamber_temp_target_time: READ ONLY Epoch timetamp for when target chamber temperature was last set.
  • _fan_gear READ ONLY Combined fan(s) reporting value. Can be bit shifted for individual speeds.
  • _heat_break_fan_speed READ_ONLY The heatbreak (heater block) fan speed in percent.
  • _fan_speed READ ONLY The parts cooling fan speed in percent.
  • _fan_speed_target READ/WRITE The parts cooling fan target speed in percent.
  • _fan_speed_target_time: READ ONLY Epoch timetamp for when target fan speed was last set.
  • _light_state READ/WRITE Boolean value indicating the state of the work light.
  • _wifi_signal READ ONLY The current Wi-Fi signal strength of the printer.
  • _speed_level READ/WRITE System Print Speed (1=Quiet, 2=Standard, 3=Sport, 4=Ludicrous).
  • _gcode_state READ ONLY State reported for job status (FAILED/RUNNING/PAUSE/IDLE/FINISH).
  • _gcode_file READ ONLY The name of the current or last printed gcode file.
  • _3mf_file READ ONLY The name of the 3mf file currently being printed.
  • _plate_num READ ONLY The selected plate # for the current 3mf file.
  • _subtask_name READ ONLY The name of the active subtask.
  • _print_type READ ONLY Not entirely sure. Reports "idle" when no job is active.
  • _percent_complete READ ONLY Percentage complete for the current active job.
  • _time_remaining READ ONLY The number of estimated minutes remaining for the active job.
  • _start_time READ ONLY The start time of the last (or current) active job in epoch minutes.
  • _elapsed_time READ ONLY The number of elapsed minutes for the last (or current) active job.
  • _layer_count READ ONLY The total number of layers for the current active job.
  • _current_layer READ ONLY The current layer being printed for the current active job.
  • _current_stage READ ONLY Maps to bambutools.parseStage.
  • _current_stage_text READ ONLY Parsed current_stage value.
  • _spools READ ONLY A Tuple of all loaded spools. Can contain up to 5 BambuSpool objects.
  • _target_spool READ_ONLY The spool # the printer is transitioning to (0-3=AMS, 254=External, 255=None).
  • _active_spool READ_ONLY The spool # the printer is using right now (0-3=AMS, 254=External, 255=None).
  • _spool_state READ ONLY Indicates whether the spool is Loaded, Loading, Unloaded, or Unloading.
  • _ams_status READ ONLY Bitwise encoded status of the AMS (not currently used).
  • _ams_exists READ ONLY Boolean value represents the detected presense of an AMS.
  • _ams_rfid_status READ ONLY Bitwise encoded status of the AMS RFID reader (not currently used).
  • _sdcard_contents READ ONLY dict (json) value of all files on the SDCard (requires get_sdcard_contents be called first).
  • _sdcard_3mf_files READ ONLY dict (json) value of all .3mf files on the SDCard (requires get_sdcard_3mf_files be called first).
  • _hms_data READ ONLY dict (json) value of any active hms codes with descriptions attached if they are known codes.
  • _hms_message READ ONLY all hms_data desc fields concatinated into a single string for ease of use.
  • _print_type READ ONLY can be cloud or local
  • _skipped_objects READ ONLY array of objects that have been skipped / cancelled

The attributes (where appropriate) are included whenever the class is serialized using its toJson() method.

When accessing the class level attributes, use their associated properties as the class level attributes are marked private.

delete_sdcard_file(file)

Delete the specified file on the printer's SDCard and returns an updated dict of all files on the printer

Parameters
  • file : str - the full path filename to be deleted

download_sdcard_file(src, dest)

Downloads a file from the printer

Parameters
  • src : str - the full path filename on the printer to be downloaded to the host
  • dest : str - the full path filename on the host to store the downloaded file

get_sdcard_3mf_files()

Returns a dict (json document) of all .3mf files on the printer's SD card. The private class level _sdcard_3mf_files attribute is also populated.

Usage

The return value of this method is very useful for binding to things like a clientside TreeView

get_sdcard_contents()

Returns a dict (json document) of ALL files on the printer's SD card. The private class level _sdcard_contents attribute is also populated.

Usage

The return value of this method is very useful for binding to things like a clientside TreeView

jsonSerializer(obj)

Helper method used by toJson() to serialize this object.

load_filament(slot)

Requests the printer to load filament into the extruder using the requested spool (slot #)

Parameters

slot : int

  • 0 - AMS Spool #1
  • 1 - AMS Spool #2
  • 2 - AMS Spool #3
  • 3 - AMS Spool #4
  • 254 - External Spool

make_sdcard_directory(dir)

Creates the specified directory on the printer and returns an updated dict of all files on the printer

Parameters
  • dir : str - the full path directory name to be created

pause_printing()

Pauses the current print job if one is running.

pause_session()

Pauses the BambuPrinter session is it is active. Under the covers this method unsubscribes from the /report topic, essentially disabling all printer data refreshes.

print_3mf_file(name, plate, bed, use_ams, ams_mapping='', bedlevel=True, flow=True, timelapse=False)

Submits a request to execute the name 3mf file on the printer's SDCard.

Parameters
  • name : str - path, filename, and extension to execute
  • plate : int - the plate # from your slicer to use (usually 1)
  • bed : PlateType - the bambutools.PlateType to use
  • use_ams : bool - Use the AMS for this print
  • ams_mapping : Optional[str] - an AMS Mapping that specifies which AMS spools to use (external spool is used if blank)
  • bedlevel : Optional[bool] = True - boolean value indicates whether or not the printer should auto-level the bed
  • flow : Optional[bool] = True - boolean value indicates if the printer should perform an extrusion flow calibration
  • timelapse : Optional[bool] = False - boolean value indicates if printer should take timelapse photos during the job
Example
  • print_3mf_file("/jobs/my_project.3mf", 1, PlateType.HOT_PLATE, False, "") - Print the my_project.3mf file in the SDCard /jobs directory using the external spool with bed leveling and extrusion flow calibration enabled and timelapse disabled
  • print_3mf_file("/jobs/my_project.gcode.3mf", 1, PlateType.HOT_PLATE, True, "[-1,-1,2,-1]") - Same as above but use AMS spool #3
AMS Mapping
  • [0,-1,-1,-1] - use AMS spool #1 only
  • [-1,1,-1,-1] - use AMS spool #2 only
  • [0,-1,-1,3] - use AMS spools #1 and #4
  • [0,1,2,3] - use all 4 AMS spools

quit()

Shuts down all threads. Your BambuPrinter instance should probably be considered dead after making this call although you may be able to restart a session with start_session().

refresh()

Triggers a full data refresh from the printer (if it is connected). You should use this method sparingly as resorting to it indicates something is not working properly.

rename_sdcard_file(src, dest)

Renames the specified file on the printer and returns an updated dict of all files on the printer

Parameters
  • src : str - the full path name to be renamed
  • dest : str - the full path name to be renamed to

resume_printing()

Resumes the current print job if one is paused.

resume_session()

Resumes a previously paused session by re-subscribing to the /report topic.

send_ams_control_command(ams_control_cmd)

Send an AMS Control Command - will pause, resume, or reset the AMS.

send_gcode(gcode)

Submit one, or more, gcode commands to the printer. To submit multiple gcode commands, separate them with a newline (\n) character.

Parameters

gcode : str

Examples
  • send_gcode("G91\nG0 X0\nG0 X50") - queues 3 gcode commands on the printer for processing
  • send_gcode("G28") - queues 1 gcode command on the printer for processing

set_ams_user_setting(setting, enabled, ams_id=0)

Enable or disable one of the AMSUserSetting options

set_print_option(option, enabled)

Enable or disable one of the PrintOption options

set_spool_details(tray_id, tray_info_idx, tray_id_name='', tray_type='', tray_color='', nozzle_temp_min=-1, nozzle_temp_max=-1)

Sets spool / tray details such as filament type, color, and nozzle min/max temperature.

set_spool_k_factor(tray_id, k_value, n_coef=1.399999976158142, nozzle_temp=-1, bed_temp=-1, max_volumetric_speed=-1)

Sets the linear advance k factor for a specific spool / tray

skip_objects(objects)

skip a list of objects extracted from the 3mf's plate_x.json file

Parameters

objects : list

start_session()

Initiates a connection to the Bambu Lab printer and provides a stateful session, with built-in recovery in the event BambuPrinter becomes disconnected from the machine.

This method is required to be called before any commands or data collection / callbacks can take place with the machine.

stop_printing()

Requests the printer to stop printing if a job is currently running.

toJson()

Returns a dict (json document) representing this object's private class level attributes that are serializable (most are).

unload_filament()

Requests the printer to unload whatever filament / spool may be currently loaded.

upload_sdcard_file(src, dest)

Uploads the local filesystem file to the printer and returns an updated dict of all files on the printer

Parameters
  • src : str - the full path filename on the host to be uploaded to the printer
  • dest : str - the full path filename on the printer to upload to