Jump to content
Ultimaker Community of 3D Printing Experts
taconite

If statement in Postprocessing, Layer height

Recommended Posts

Good evening fellow 3D-printing enthusiasts,

 

I am new to this forum and have a problem - I couldn't find a topic with the search function. I have no idea if this is the correct forum for my inquiry - maybe a moderator could help me out.

I would like to add some code to the g-code with postprocessing. I would like to add different code dependent on the layer height like:

 

if(layer height==0.3)

{

// some g-code

}

elseif(layer height == 0.1)

{

// some other g-code

}

else

{

// default g-code

}

 

is this possible with the post-processing plug-in (I will use the search and replace function)?

what is the variable for the layer height? layer_height ... ?

 

I am using Cura 3.4

 

Thank you for your help

 

Best Regards

 

 

Share this post


Link to post
Share on other sites

Read about post processing plugins.  There are bout 6 of them that come with cura.  Look at the code in them.  They use python.  Python is a general purpose language so you can do anything in a post processing script that you can do in any language.

 

Maybe start by looking at the names of the scripts (go to extensions, post-processing, modify g-code, then click add-a-script.  Try to find the python files that go with those script names using grep.  They'll be in the same folder tree structure as cura.

Share this post


Link to post
Share on other sites

What do you mean by "variable"? A python variable? There is not global python variable for the layer height in Cura.

 

There is a setting called "layer_height". To get its value, you would call the method getPropertyValue on the "global stack".

 

global_stack = Application.getInstance().getGlobalContainerStack()
layer_height = global_stack.getProperty("layer_height", "value")

 

  • Like 2

Share this post


Link to post
Share on other sites
16 hours ago, ahoeben said:

What do you mean by "variable"? A python variable? There is not global python variable for the layer height in Cura.

 

There is a setting called "layer_height". To get its value, you would call the method getPropertyValue on the "global stack".

 


global_stack = Application.getInstance().getGlobalContainerStack()
layer_height = global_stack.getProperty("layer_height", "value")

 

 

 

Thanks, I could definitely use that at some point!

What would the spelling look like for "material_standby_temperature"?

Global_stack.getProperty (material_standby_temperature, value) returns only the default value of fdmprinter.def.json.

It would be best if extruders were also possible dependent!

Share this post


Link to post
Share on other sites
26 minutes ago, zerspaner_gerd said:

Global_stack.getProperty (material_standby_temperature, value) returns only the default value of fdmprinter.def.json.

 

That is because material_standby_temperature is set per extruder, so instead of the global stack you need to look at one of the extruder stacks.

 

For example, this gets the standby temperature set for the first extruder:

first_extruder_stack = ExtruderManager.getInstance().getActiveExtruderStacks()[0]
material_standby_temperature = first_extruder_stack.getProperty("material_standby_temperature", "value")

 

  • Like 2

Share this post


Link to post
Share on other sites

Excuse me @ahoeben ?,

 

has changed a bit with Cura 3.5.1, it does not work anymore.

t0_extruder_stack = ExtruderManager.getInstance().getActiveExtruderStacks()[0]


The post processing plugin is not displayed at all, if I hide it it will appear again for selection.

 

I tried a lot, without success. I do not know where to read up something.

Share this post


Link to post
Share on other sites
2018-10-25 23:29:12,563 - ERROR - [MainThread] UM.Logger.logException [84]: Exception: Exception occurred while loading post processing plugin: list index out of range
2018-10-25 23:29:12,565 - ERROR - [MainThread] UM.Logger.logException [88]: Traceback (most recent call last):
2018-10-25 23:29:12,566 - ERROR - [MainThread] UM.Logger.logException [88]:   File "C:\Program Files\Ultimaker Cura 3.5\plugins\PostProcessingPlugin\PostProcessingPlugin.py", line 155, in loadScripts
2018-10-25 23:29:12,567 - ERROR - [MainThread] UM.Logger.logException [88]:     temp_object = loaded_class()
2018-10-25 23:29:12,568 - ERROR - [MainThread] UM.Logger.logException [88]:   File "C:\Program Files\Ultimaker Cura 3.5\plugins\PostProcessingPlugin\scripts\test2_settings_cura.py", line 22, in __init__
2018-10-25 23:29:12,569 - ERROR - [MainThread] UM.Logger.logException [88]:     self.t0_extruder_stack = ExtruderManager.getInstance().getActiveExtruderStacks()[0]
2018-10-25 23:29:12,570 - ERROR - [MainThread] UM.Logger.logException [88]: IndexError: list index out of range

It had worked with Cura 3.4.
Have now changed the order, now it seems to work again!


That was my previous one sequence:

from ..Script import Script
from UM.Application import Application
from cura.Settings.ExtruderManager import ExtruderManager
import re

class test2_settings_cura(Script):
    version = "1.0.0"
    def __init__(self):
        super().__init__()
        ########################################################################################################################
        self.global_stack = Application.getInstance().getGlobalContainerStack()
        self.t0_extruder_stack = ExtruderManager.getInstance().getActiveExtruderStacks()[0]
        self.t1_extruder_stack = ExtruderManager.getInstance().getActiveExtruderStacks()[1]
        ########################################################################################################################

    def getSettingDataString(self):
        return """{
            "name":"Test Settings Cura """ + self.version + """",
            "key": "test2_settings_cura",
            "metadata": {},
            "version": 2,
            "settings":
            {

            }
        }"""

    def execute(self, data):
        layers_started = False

        ########################################################################################################################


        layer_height = self.global_stack.getProperty("layer_height", "value")
        Turm = self.global_stack.getProperty("prime_tower_position_x", "value")
        flover = self.global_stack.getProperty("machine_gcode_flavor", "value")


        material_standby_temperature_T0 = self.t0_extruder_stack.getProperty("material_standby_temperature", "value")
        material_standby_temperature_T1 = self.t1_extruder_stack.getProperty("material_standby_temperature", "value")


        ########################################################################################################################


        for layer in data:
            modified_layer = ""

            index = data.index(layer)
            lines = layer.split("\n")
            for line in lines:

                if ";Generated with Cura_SteamEngine " in line:
                    modified_layer += ";With Dual Script RepRap Duet3D V%s\n" % self.version + line +"\n\n"

                    modified_layer += ";TES TESTE Layer:%f\n\n" % layer_height
                    modified_layer += ";TES TESTE tover pos.x:%f\n\n" % Turm
                    modified_layer += ";TES TESTE flover:%s\n\n" % flover
                    modified_layer += ";TES TESTE Standby_T0:%s\n\n" % material_standby_temperature_T0
                    modified_layer += ";TES TESTE Standby_T1:%s\n\n" % material_standby_temperature_T1

                    layers_started = True
                    continue

                if not layers_started:
                    #insert original line
                    modified_layer += line + "\n"
                    continue


                else:
                    #insert original line
                    if line == "":
                        modified_layer += line
                    else:
                        modified_layer += line + "\n"

            #replace the layer with the modified data
            data[index] = modified_layer
        return data

 

No matter you, now it works again
Thanks

Share this post


Link to post
Share on other sites

Without looking too much into the rest of the code, you are looking up the stacks in the __init__ method. That method gets called when the PostProcessing script discovers the available scripts, which might be before Cura has finished loading the printers from your configuration. Even if it worked, the information would also be incorrect once you changed to another printer in Cura.

 

Don't do postprocessing things in __init__, but do them in execute.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Our picks

    • Introducing Ultimaker Cura 3.6 | Beta
      Ultimaker Cura 3.6 | Beta is available. It comes with new features, bug fixes, and UX improvements. We would really like to have your feedback on it to make our stable release as good as it can be. As always, you can download the beta for free from our website, for Windows, MacOS, and Linux.
        • Like
      • 92 replies
    • Print Core CC | Red for Ruby
      Q: For some users, abrasive materials may be a new subject matter. Can you explain what it is that makes a material abrasive when you are not sure which print core to use?
      A: Materials which are hard in a solid piece (like metals, ceramics and carbon fibers) will generally also wear down the nozzle. In general one should assume...
        • Like
      • 30 replies
    • "Back To The Future" using Generative Design & Investment Casting
      Designing for light-weight parts is becoming more important, and I’m a firm believer in the need to produce lighter weight, less over-engineered parts for the future. This is for sustainability reasons because we need to be using less raw materials and, in things like transportation, it impacts the energy usage of the product during it’s service life.
        • Like
      • 12 replies
×

Important Information

Welcome to the Ultimaker Community of 3D printing experts. Visit the following links to read more about our Terms of Use or our Privacy Policy. Thank you!