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.
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.