Jump to content
taconite

If statement in Postprocessing, Layer height

Recommended Posts

Posted · If statement in Postprocessing, Layer height

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
Posted · If statement in Postprocessing, Layer height

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
Posted · If statement in Postprocessing, Layer height

Thank you for your reply

 

I already did that but couldn't find a switch-case style or if-statement but anyhow because it is just python I will figure it out (I hope so).

 

But does anyone know how the variable for the layer height is called?

Share this post


Link to post
Share on other sites
Posted · If statement in Postprocessing, Layer height

Python does not have a switch-case construct, but it does have if/elif/else statements.

  • Like 1

Share this post


Link to post
Share on other sites
Posted · If statement in Postprocessing, Layer height

Thank you for you replies.

 

Does nobody know how the variable for layer height is called in cura?

Share this post


Link to post
Share on other sites
Posted · If statement in Postprocessing, Layer height

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
Posted · If statement in Postprocessing, Layer height
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
Posted · If statement in Postprocessing, Layer height
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
Posted · If statement in Postprocessing, Layer height

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
Posted · If statement in Postprocessing, Layer height

Check cura.log in Help -> Show configuration folder. Search for the id (filename without .py) of your script, and it should give you a hint about why it is not loading. If not, I'm going to have to see the entire script.

Share this post


Link to post
Share on other sites
Posted · If statement in Postprocessing, Layer height
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
Posted · If statement in Postprocessing, Layer height

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

×
×
  • Create New...

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!