Where to add custom G-code in Bambu Studio
Custom G-code in Bambu Studio goes into the Machine Start G-code or Machine End G-code fields, found under Print Settings. You reach these by opening a print profile, clicking the gear icon next to your printer name, then selecting "Machine G-code" from the left sidebar. The Start G-code runs before your print begins; the End G-code runs after it finishes. You can also insert G-code between layers using the Change Filament feature or by editing the sliced file directly in a text editor before sending it to the printer.
Most users add custom commands to the Start G-code section to override bed leveling, adjust nozzle temperature mid-print, or run a custom purge line. The End G-code typically contains commands to park the nozzle, turn off the bed heater, or retract filament. Bambu Studio preserves your custom commands even when you update the software, so you only need to enter them once per printer profile.
Key Takeaways
- Custom G-code fields are in Machine G-code settings, accessible through the gear icon next to your printer name in the print profile.
- Start G-code runs before printing begins, and End G-code runs after the print finishes — use each for the commands that belong at those moments.
- You can insert G-code between layers by using the Change Filament feature or by editing the .gcode file directly in a text editor.
- Bambu Studio does not validate G-code syntax, so incorrect commands will send to the printer unchanged and may cause errors or unexpected behavior.
Accessing the Machine G-code settings
Open Bambu Studio and load or create a print profile. In the right panel under "Printer", locate your printer name with a small gear icon next to it. Click that gear icon to open printer settings. In the left sidebar, scroll down and select "Machine G-code". You will see two large text fields: one labeled "Start G-code" and one labeled "End G-code".
If you do not see the gear icon, make sure you have selected a printer profile (not just the generic printer type). The Machine G-code section only appears when a specific printer is active. Your changes save automatically when you close the settings panel or switch to another section.
What goes in Start G-code versus End G-code
Start G-code executes immediately after the printer homes its axes and heats the bed and nozzle. Use this section for commands that prepare the printer for printing: custom bed leveling routines, nozzle priming moves, purge lines, or temperature adjustments. For example, if you want to run a custom mesh bed leveling before every print, that command goes here.
End G-code runs after the final layer is printed and the nozzle retracts. This is where you place commands to cool the bed, move the nozzle to a safe position, disable motors, or trigger a filament unload. Many users add a command to move the nozzle to the front-left corner so they can easily remove the print without the nozzle blocking access.
Inserting G-code between layers
Bambu Studio does not have a built-in interface for inserting G-code at specific layers, but you have two workarounds. The first is to use the Change Filament feature: right-click on a layer in the preview, select "Change Filament", and Bambu Studio will pause the print at that layer. You can then add custom G-code in the dialog that appears. This approach works well for pausing to insert inserts, change colors, or run a custom command mid-print.
The second method is to export the sliced file as G-code, open it in a text editor like Notepad or VS Code, and manually insert G-code commands at the line numbers where you want them to execute. Find the layer you want to target by searching for comments like `;LAYER_CHANGE` or `;Z:` followed by the height. Insert your command on a new line, save the file, and send it to the printer. This method requires familiarity with G-code structure but gives you precise control.
Common G-code commands for Bambu printers
Bambu printers use standard RepRap G-code syntax. Common commands include G28 (home all axes), G29 (bed leveling), M104 S[temp] (set nozzle temperature), M140 S[temp] (set bed temperature), G1 Z[height] (move nozzle to a specific height), and M109 S[temp] (set nozzle temperature and wait). For example, `M104 S210` sets the nozzle to 210°C without waiting, while `M109 S210` sets it and pauses until the temperature is reached.
Retraction commands like G10 (retract) and G11 (unretract) are also supported, as are fan commands like M106 S[speed] (set part cooling fan speed, 0–255) and M107 (turn off part cooling fan). Always check your printer's manual or Bambu's documentation for any printer-specific variations, as some commands may behave differently on X1 versus P1 models.
Testing and troubleshooting custom G-code
Bambu Studio does not validate G-code before sending it to the printer, so syntax errors or unsupported commands will be sent unchanged. Test new G-code on a small, low-risk print first — not on a multi-hour job. If the printer behaves unexpectedly, check the printer's display or log for error messages. Most errors will cause the printer to pause or stop rather than damage itself, but it is always safer to test incrementally.
If your custom G-code does not seem to run, verify that it is in the correct field (Start or End), that there are no trailing spaces or hidden characters, and that the syntax matches your printer model's documentation. You can also export the final G-code file and open it in a text editor to confirm your custom commands are present in the output. If they are missing, they may have been overwritten by a profile update or removed by a filter in Bambu Studio's slicer engine.
Saving and reusing custom G-code profiles
Once you have entered custom G-code, save your printer profile by clicking the save icon or using File > Save. Bambu Studio stores profiles locally on your computer, so your custom G-code persists across sessions and software updates. If you want to use the same G-code on multiple computers or share it with others, export your profile: go to File > Export Profiles, select your printer, and save the .json file. You can then import it on another computer or share it with a colleague.
If you update Bambu Studio and your custom G-code disappears, check whether the software reset your profiles to defaults. You can restore a backup by re-importing your exported profile or by manually re-entering the commands into the Machine G-code fields. Keeping a text file with your custom commands is a good practice for this reason.
Frequently Asked Questions
Can I use different G-code for different filament types?
Yes. Create separate printer profiles for each filament type or material, and set different Start G-code in each profile. For example, you might have a "PLA" profile with a nozzle temperature of 210°C and a "PETG" profile with 240°C. When you load a print, select the profile that matches your filament, and Bambu Studio will use the corresponding G-code.
What happens if I enter invalid G-code?
Bambu Studio sends it to the printer unchanged. The printer will either ignore the command, pause with an error, or behave unexpectedly depending on what the invalid command is. Always test new G-code on a small print first. If the printer pauses, you can usually resume or cancel from the printer's display without damage.
Can I see the final G-code that Bambu Studio generates?
Yes. After slicing, click "Export G-code" and save the file to your computer. Open it in any text editor to see the complete G-code, including your custom Start and End commands. This is useful for debugging or understanding exactly what the printer will execute.
Do I need to include homing or bed leveling in Start G-code?
No. Bambu Studio includes default homing and leveling commands before your Start G-code runs. If you want to override or skip these, you can add your own commands, but most users leave the defaults in place and only add custom commands for specific needs like a purge line or temperature adjustment.
Will my custom G-code work on both X1 and P1 printers?
Most standard G-code commands work on both, but some printer-specific features may differ. Check the documentation for your specific model before using advanced commands. When in doubt, test on a small print or consult Bambu's support forum to confirm compatibility.