# Edit G-code to change print order of multiple one-at-a-time print jobs

SUMMARY: I use a Creality Ender 3 and Cura 4.8 slicer. I need to print multiple objects 'one-at-a-time' (Cura allows this, that is fine) and I must specify the order in which each model is printed but Cura does not have this facility. My question is: how can I change the order of the printed objects by editing the G-code?

DETAIL: Sometimes it is essential or useful to specify the order of printing multiple objects in a 'one-at-a-time' batch print to allow the printing of some objects that are taller than the X-gantry clearance together with other objects that are shorter than the gantry because if you can control the order then you can print the small objects first and then the tall objects (because the gantry won't get in the way of the already-printed small objects but if the tall objects are printed first then the gantry will crash into them). Specifically I am talking about two or more objects printed at roughly the same Y-level on the print bed (I know you can 'get-away' with it if you separate them in Y but I want to print multiple rows of such objects). Without control over print order this cannot be done thereby limiting productivity (number of models per print run).

WHAT HAVE I DONE TO RESOLVE THIS ALREADY: I can control the print order of multiple identical objects in Cura (they are printed in reverse order to their creation as duplicates of an initial model and if there are problems you can sometimes control the order by moving objects around) but this does not work for multiple different objects. I did several Google searches and found some conversations that deal with this issue including on the official GitHub page of the Cura slicer (see for example this Cura GitHub Discussion). The conclusion given by the Cura developers is that they understand the limitation, but it is not an issue for Ultimaker printers so they have no plans to make user-customisable print order a feature in Cura because it would be too much work for them to do the changes and maintain them. One person mentioned a python script to allow post editing of G-code to change print order but I could find no links to it. Another person described a 'workaround' in Cura to do with grouping and ungrouping models in a specific sequence but this does not work for me (?maybe it only worked in older versions of Cura). Perhaps someone on this forum has G-coding knowledge and could shed some light on how it may be done? [PS: I am only interested in G-code editing solutions for this question, or a Cura workaround that works in v4.8 - I don't use Octoprint/Rasperry Pi controllers].

Thanks.

[EDIT] Thanks to the contributors in the accepted answer I managed to do this with 'cut-and-paste' (via a small C program - you could do it manually but it is tedious and there is more risk of making a mistake if you have many models). I used the comments in Cura's G-code and a few other G-code cues to calculate the correct splice points.

It works provided you take care to ensure the new extruder code is either altered according to the new model positions or use the 'relative extrusion' mode when saving the original G-code from Cura (I used the latter method). Also you must splice the G-code at the right points to ensure pre-model start and post-model finish G-code commands are properly carried around and the final model in the original G-code needs a terminal G0 command to be constructed if it is no longer terminal in the new order so that it can properly position the nozzle to go to the next model in the chain. Also you need to ensure M107 is included prior to each model's first layer. There is danger in doing any of this - the new G-code might damage your printer if you get anything wrong - but it can work (at least it has for me).

I made several manual checks on the new re-arranged G-code as well as loading it into Cura and running Cura's nozzle simulator on it to ensure it does what I expected of it prior to risking running it in my printer, but after that it printed OK.

• This would suggest a useful feature request to forward to the Cura development team, assuming it isn't already in 4.9... May 25 at 16:42
• @ZeissIkon The Cura developers know that people want this feature but they don't want to do it. It is potentially dangerous but then messing about with Cura settings (overriding machine defaults for your printer, etc.) is similarly dangerous. As long as people know that and accept the risk then that's fine I think. If there is enough demand then perhaps if I can get my code into a form others can easily use I will post it on GitHub - with a big health warning! May 25 at 17:01
• Seems obvious that you'd want/need to make the shorter parts first, even if they're spread to the corners of the bed. If they're spread enough, on the other hand, it doesn't matter as long as none of them are high enough to interfere with the gantry and they're far enough apart not to hit the hot end fan cases etc. May 25 at 17:33

You can open the G-code file in any text editor and copy-paste the relevant sections of G-code to reorder them. It will be fairly obvious where one object ends and the next one starts because the printhead will move back down to Z=0; you can do a simple search to find the commands that move to Z=0 (in my version of Cura, it inserts a comment ;LAYER:0 at the start of each object).
To ensure that the extruder starts in the correct position, look back at the previous print (in the original order) to see where the extruder finished, then insert (at the start of the new print) G92 Exx.xx where xx.xx is the previous position of the extruder. If looking back to the previous print is too tedious, you can also just use the very first extruder position in the print itself and use that. It will just skip printing the very first section of brim/skirt, which doesn't make a big difference.
• @WildCat The G92 command really does fix that issue. You don't need to (programatically) change all of the E position commands. Using G92 tells the printer that it should "virtually" move the extruder, that is, not physically move it but only update the printer's memory to set the extruder to that position. For example (if the starting position is zero) issuing G92 E10 followed by G1 E20 results in 10mm of filament being extruded, the same as if we had just issued G1 E10. May 22 at 12:30