Examples
Basic
To get started - make sure your printer is on the same network as your target machine. Make sure that ports 8883, 6000 and 990 are accessible from your machine.
import time
import bambulabs_api as bl
IP = '192.168.1.200'
SERIAL = 'AC12309BH109'
ACCESS_CODE = '12347890'
if __name__ == '__main__':
print('Starting bambulabs_api example')
print('Connecting to Bambulabs 3D printer')
print(f'IP: {IP}')
print(f'Serial: {SERIAL}')
print(f'Access Code: {ACCESS_CODE}')
# Create a new instance of the API
printer = bl.Printer(IP, ACCESS_CODE, SERIAL)
# Connect to the Bambulabs 3D printer
printer.connect()
time.sleep(2)
# Get the printer status
status = printer.get_state()
print(f'Printer status: {status}')
# Turn the light off
printer.turn_light_off()
time.sleep(2)
# Turn the light on
printer.turn_light_on()
# Disconnect from the Bambulabs 3D printer
printer.disconnect()
Basic No Camera
If using the api without camera connection follow the below example. Note that camera on X1 machines is not supported on the API for versions less than 2.7.0
import time
import bambulabs_api as bl
IP = '192.168.1.200'
SERIAL = 'AC12309BH109'
ACCESS_CODE = '12347890'
if __name__ == '__main__':
print('Starting bambulabs_api example')
print('Connecting to Bambulabs 3D printer')
print(f'IP: {IP}')
print(f'Serial: {SERIAL}')
print(f'Access Code: {ACCESS_CODE}')
# Create a new instance of the API
printer = bl.Printer(IP, ACCESS_CODE, SERIAL)
# Connect to the Bambulabs 3D printer without connecting to the camera
printer.mqtt_start()
time.sleep(2)
# Get the printer status
status = printer.get_state()
print(f'Printer status: {status}')
# Turn the light off
printer.turn_light_off()
time.sleep(2)
# Turn the light on
printer.turn_light_on()
# Disconnect the mqtt client
printer.mqtt_stop()
Basic Subscription
A simple looping subscription script to get you started once you’ve been able to connect the printer up to the api.
import time
import bambulabs_api as bl
import os
import datetime
IP = '192.168.1.200'
SERIAL = 'AC12309BH109'
ACCESS_CODE = '12347890'
debug = os.getenv("debug", False)
if __name__ == '__main__':
print('Starting bambulabs_api example')
print('Connecting to Bambulabs 3D printer')
print(f'IP: {IP}')
print(f'Serial: {SERIAL}')
print(f'Access Code: {ACCESS_CODE}')
# Create a new instance of the API
printer = bl.Printer(IP, ACCESS_CODE, SERIAL)
# Connect to the Bambulabs 3D printer
printer.connect()
try:
while True:
time.sleep(5)
# Get the printer status
status = printer.get_state()
percentage = printer.get_percentage()
layer_num = printer.current_layer_num()
total_layer_num = printer.total_layer_num()
bed_temperature = printer.get_bed_temperature()
nozzle_temperature = printer.get_nozzle_temperature()
remaining_time = printer.get_time()
if remaining_time is not None:
finish_time = datetime.datetime.now() + datetime.timedelta(
minutes=int(remaining_time))
finish_time_format = finish_time.strftime("%Y-%m-%d %H:%M:%S")
else:
finish_time_format = "NA"
print(
f'''Printer status: {status}
Layers: {layer_num}/{total_layer_num}
percentage: {percentage}%
Bed temp: {bed_temperature} ºC
Nozzle temp: {nozzle_temperature} ºC
Remaining time: {remaining_time}m
Finish time: {finish_time_format}
'''
)
if debug:
import json
print("=" * 100)
print("Printer MQTT Dump")
print(json.dumps(
printer.mqtt_dump(),
sort_keys=True,
indent=2
))
print("=" * 100)
finally:
# Disconnect from the Bambulabs 3D printer
printer.disconnect()
Get a Camera Frame
Access the Camera of a P1 printer:
import time
import bambulabs_api as bl
import os
IP = '192.168.1.200'
SERIAL = 'AC12309BH109'
ACCESS_CODE = '12347890'
env = os.getenv("env", "debug")
if __name__ == '__main__':
print('Starting bambulabs_api example')
print('Connecting to Bambulabs 3D printer')
print(f'IP: {IP}')
print(f'Serial: {SERIAL}')
print(f'Access Code: {ACCESS_CODE}')
# Create a new instance of the API
printer = bl.Printer(IP, ACCESS_CODE, SERIAL)
# Connect to the Bambulabs 3D printer
printer.connect()
time.sleep(5)
image = printer.get_camera_image()
image.save("example.png")
Start a Print
Start a print using the api given a valid gcode file:
from io import BytesIO
import time
import zipfile
import bambulabs_api as bl
import os
IP = '192.168.1.200'
SERIAL = 'AC12309BH109'
ACCESS_CODE = '12347890'
# ============================================================
INPUT_FILE_PATH = 'bambulab_api_example.gcode'
UPLOAD_FILE_NAME = 'bambulab_api_example.3mf'
# ============================================================
env = os.getenv("env", "debug")
plate = os.getenv("plate", "true").lower() == "true"
def create_zip_archive_in_memory(
text_content: str,
text_file_name: str = 'file.txt') -> BytesIO:
"""
Create a zip archive in memory
Args:
text_content (str): content of the text file
text_file_name (str, optional): location of the text file.
Defaults to 'file.txt'.
Returns:
io.BytesIO: zip archive in memory
"""
zip_buffer = BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
zipf.writestr(text_file_name, text_content)
zip_buffer.seek(0)
return zip_buffer
if __name__ == '__main__':
print('Starting bambulabs_api example')
print('Connecting to Bambulabs 3D printer')
print(f'IP: {IP}')
print(f'Serial: {SERIAL}')
print(f'Access Code: {ACCESS_CODE}')
# Create a new instance of the API
printer = bl.Printer(IP, ACCESS_CODE, SERIAL)
# Connect to the Bambulabs 3D printer
printer.connect()
time.sleep(5)
with open(INPUT_FILE_PATH, "r") as file:
gcode = file.read()
if plate:
gcode_location = "Metadata/plate_1.gcode"
io_file = create_zip_archive_in_memory(gcode, gcode_location)
if file:
result = printer.upload_file(io_file, UPLOAD_FILE_NAME)
if "226" not in result:
print("Error Uploading File to Printer")
else:
print("Done Uploading/Sending Start Print Command")
printer.start_print(UPLOAD_FILE_NAME, 1)
print("Start Print Command Sent")
else:
gcode_location = INPUT_FILE_PATH
io_file = create_zip_archive_in_memory(gcode, gcode_location)
if file:
result = printer.upload_file(io_file, UPLOAD_FILE_NAME)
if "226" not in result:
print("Error Uploading File to Printer")
else:
print("Done Uploading/Sending Start Print Command")
printer.start_print(UPLOAD_FILE_NAME, gcode_location)
print("Start Print Command Sent")