Would Like Help With Using Start Code Variables

Since starting work on my new IDEX printer, I have had to deal with some, admittedly minor, issues with nozzle & bed heating, primarily with getting commands into the gcode file that will cause the nozzle & bed to heat the way I want.

Of course, I thought about how I could use the Start Code feature of Cura to my advantage. Then, being a very long time programmer, my mind naturally turned to how I might access the variables I just knew had to be used to store values that are set in the Print Settings window. What if I could include variables in the Start Code that Cura would fill in during the slicing process? I searched the Internet & I did find articles that strongly suggest I should indeed be able to do what I hoped I could. Unfortunately, I seem to be doing something wrong, cause it’s not working. By that, I mean the gcode produced by Cura does not include command lines that are filled with resolved variables. Instead, Cura simply copies the commands I placed in the Start Code verbatim to the gcode file.

I found this article: https://cnc.qbytesworld.com/3d-print…es-3d-printer/ which basically told me I should be able to do exactly what I wanted.

I also found a forum post that told me I could reference a file called fdmprinter.def.json. I searched for that file in the folder on my computer where Cura was installed, and found it. There are many references in the file to variables that Cura uses for user configurable parameters.

So, my obvious question is, can anyone save me some time & point me in the right direction?

Unfortunately, the name “gcode” promises more than it delivers. 3D printers are profoundly stupid and only understand simple commands. Most run on processors with a fraction of the power of even low-power desktops. Any smarts are in the slicer. Variables are commonly referred to as placeholders in slicers, no doubt for some obscure historical reason. This means every slicer implementation has its own dialect of placeholders.

In Cura gcode, you can use placeholder names (variables) by enclosing them in curly braces ({}). Here are some common examples:
[TABLE=“border: 1, cellpadding: 1, width: 500”]
[TR]
[TD]Placeholder[/TD]
[TD]Setting[/TD]
[/TR]
[TR]
[TD]{material_bed_temperature_layer_0}[/TD]
[TD]1st layer bed temperature[/TD]
[/TR]
[TR]
[TD]{material_print_temperature_layer_0}[/TD]
[TD]1st layer nozzle temperature[/TD]
[/TR]
[TR]
[TD]{line_width}[/TD]
[TD]Extrusion width[/TD]
[/TR]
[TR]
[TD]{layer_height}[/TD]
[TD]Layer height[/TD]
[/TR]
[TR]
[TD]{material_diameter}[/TD]
[TD]Filament diameter[/TD]
[/TR]
[TR]
[TD]{machine_height}[/TD]
[TD]Maximum z height[/TD]
[/TR]
[/TABLE]

Most slicers can’t do any assignment of variables or calculations. PrusaSlicer has a very limited set of calculations (min, max, basic math). It’s important to keep in mind that any such calculations are done at slice time, not at print time. The slicer does the math and substitutes values for placeholders.

If you’re interested, I’ve documented my Cura gcode here. The notes are not complete but should give you an idea of what’s possible. There’s a complete list of Cura placeholders here.

Ender5r, Try looking at this. https://github.com/Ultimaker/Cura/bl…aryOfDoom.json
I used it to shorten the preheating cycle while it is developing the 5x5 mesh for BLTouch bed height compensation.

Here is an example of what I added to my Start G-Code:
M190 S{material_bed_temperature}; Wait for bed to heat up

I find this very interesting. I really want to learn how to play with my g code without causing major problems and getting good results. Have they written a ‘g code for Dummies yet’ ?

@roon4660 - I haven’t seen a good tutorial at the “dummies” level yet. I’ve put together some notes based on what I’ve encountered with different slicers, but they’re far from complete. A good place to start is the Marlin reference, assuming your printer is based on Marlin. Not every printer is, and individual implementations vary in how complete they are. Unfortunately, it’s often hard to get information from the manufacturer on exactly what they support.

It’s pretty common to modify the start gcode routine used by the printer to home, heat the nozzle & bed, and prepare for printing. For example, on early Prusa Mk3 printers the bed leveling sensor was sensitive to temperature, so a routine to warm the sensor was a good starting point. Then modifying the prime line to reduce stinging. Some people had fun getting the prime line to draw little patterns, though the novelty of waiting for every print quickly wore off.

A bit of custom code is also handy for things like modifying settings (temperature, linear advance settings) during a print for things like test towers where changing values throughout the print based on height is wanted.

Thanks @rsonnicksen. The problem is, when I put a line like that in my Start Code, then slice the model, the resulting g-code contains exactly the same line as what I entered. IOW, it does not get resolved to the temperature I entered in Print Settings. I assume this is not what’s supposed to happen.

OK, I just sliced a recent model after inserting M104 S{material_print_temperature} into the Start Code. This time (I have zero idea why) it did resolve the variables. To double check, I changed the value in Print Settings to 333. It again resolved.

So, I’m puzzled. Why did it refuse to resolve variables before & now it does. Truly weird.

Thank you Bobstro I will follow this up later. For now I am drinking a glass of wine and smoking a substance that used to be illegal but I can get same day delivery of it now from the Canadian government or however the new deal works. I order on the internet and get it the same day. Oh how times do change! I must admit that I have not enjoyed the lock-down since it ruined my vacation in the Dominican Republic. I go on youtube to see how things are going in Sosua Dom. Rep. and I can see that a third world dictatorship is dealing with it better than the Western Powers as they wish to be known.
Sosua one on youtube.com is a good resource.
My family that lives in the West Indies can’t come as they had planned to see my new grandchild. My son and my grandson were supposed to come and live for awhile in montreal while my grandson went to school. I am more than a little pissed with the politics involved in this Plandemic.

Maybe the variables are only used for the internal gcode generator and not in start and end stuff. Or they are only for the post processing stuff.

How do you add “custom code” that affects settings during the middle of a print? Is there a place where I can execute Gcode at each layer, or some interval during the print?

@Ender5r - Unfortunately, every slicer has different names for these settings. If you are using a slicer that is not Cura, be sure to use the appropriate dialect. Placeholder names and syntax you find for another slicer are unlikely to work. I’ve found that spacing is also important. Be aware that {material_print_temperature} is the temperature used for the bulk of the print. For your start gcode, you probably want {material_bed_temperature_layer_0} which is the 1st layer filament temperature if you are using a different setting for adhesion. More details on Cura placeholders can be found here.

@Geit - The placeholders are evaluated by the slicer at slice time. When the code is written, it contains only text. It will replace each placeholder with its respective value in your settings. There is no interpretation of placeholders in the actual printer firmware. The start gcode inserted into your gcode file by the slicer is sent verbatim to the printer.

It is indeed strange seeing a dispensary being built just up the road in my little town. Talk about a cultural shift!

[…] I must admit that I have not enjoyed the lock-down since it ruined my vacation in the Dominican Republic.
It has definitely been a strange year, but we do seem to be seeing light at the end of the tunnel. I just hope we can take care of the rest of the world, particularly since this has only been a taste of what could happen in a worst case scenario. Not taking lessons from all this fumbling would be a tragedy!

They always say correctly, take care of yourself first do that you can take care of others.

Except the command was inserted into the Cura Start Code section, & this time it worked.

There is an ChangeAtZ extension to Cura that will let you change a number of settings at either a specific height or specific layer.

If it was planned, it was the most piss poor job of planning I think I’ve ever seen or heard of.

1 thing is good, at least so far. Last time (1919), the global population was less than 2 billion & estimates are that 50 to 150 million died in the pandemic. This time, with a global population of nearly 8 billion, there have been roughly 3 million deaths. And that’s despite the astronomically greater levels of world travel today. There is still time for the virus to mutate into something truly awful but, so far, science is doing fairly well against it. My wife & I got our 1st shots last week.

I do not want to be a white rat. But everyone must be able to do what they feel. I have an idea that the Plandemic is for communist control of the world.

The issue with that idea is that a virus is simply too difficult/impossible to control, so it makes a really poor weapon.

I have run into the most controversial COVID discussions on 3D printing forums for some reason, and they never seem to end well. Going to declare this one OT for myself. ?

I agree. It is most atypical for me to comment on such matters, except to say I like to preserve the forum for the topics it’s intended to cover.