Jump to content

Create a new Postprocessing Script


Cuq

Recommended Posts

Posted (edited) · Create a new Postprocessing Script

Hello,

 

I'm trying to create my own postprocessing script. The goal of the script is to add at the beginning of the GCode file different comments to keep the print profile.  For the print parameter I don't have anny problem to get the values , but I don"t know how to get the Name of the profile and the initial material name.  I'have made a test with a command line like :

GetValStr = Application.getInstance().getGlobalContainerStack().material.getMetaData().get("material", "")

but this line return an empty value.  hereafter the actual sample code 

import re #To perform the search and replace.

from ..Script import Script
from UM.Application import Application #To get the current printer's settings.
##  Performs a search-and-replace on all g-code.
#
#   Due to technical limitations, the search can't cross the border between layers.
class Documentation(Script):
    def getSettingDataString(self):
        return """{
            "name": "Documentation parametres",
            "key": "Documentation",
            "metadata": {},
            "version": 2,
            "settings":
            {
                "search":
                {
                    "label": "Search",
                    "description": "All occurrences of this text will get replaced by the parameter list.",
                    "type": "str",
                    "default_value": ";FLAVOR:Marlin"
                }
            }
        }"""

    def execute(self, data):
        search_string = self.getSettingValueByKey("search")
        search_regex = re.compile(search_string)

        replace_string = search_string
        replace_string = replace_string + "\n;==============================================\n;  Documentation\n;=============================================="   
        #   machine_nozzle_size
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("machine_nozzle_size", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("machine_nozzle_size", "label")
        replace_string = replace_string + "\n; " + GetLabel + "              : {Val} mm".format(Val = GetVal)
        #   material_guid
        GetValStr = Application.getInstance().getGlobalContainerStack().material.getMetaData().get("material", "")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("material_guid", "label")
        replace_string = replace_string + "\n; " + GetLabel + "     : " + GetValStr
        #   layer_height
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("layer_height", "label")
        replace_string = replace_string + "\n; " + GetLabel + "                 : {Val} mm".format(Val = GetVal)        
        #   line_width 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("line_width", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("line_width", "label")
        replace_string = replace_string + "\n; " + GetLabel + "                   : {Val} mm".format(Val = GetVal)
        #   material_print_temperature 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("material_print_temperature", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("material_print_temperature", "label")
        replace_string = replace_string + "\n; " + GetLabel + "         : {Val}°C".format(Val = GetVal)
        #   speed_print 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("speed_print", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("speed_print", "label")
        replace_string = replace_string + "\n; " + GetLabel + "                  : {Val} mm/s".format(Val = GetVal)
        #   speed_infill 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("speed_infill", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("speed_infill", "label")
        replace_string = replace_string + "\n; " + GetLabel + "                 : {Val} mm/s".format(Val = GetVal)        
        #   material_flow 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("material_flow", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("material_flow", "label")
        replace_string = replace_string + "\n; " + GetLabel + "                         : {Val} %".format(Val = GetVal)
        #   retraction_enable 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("retraction_enable", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("retraction_enable", "label")
        replace_string = replace_string + "\n; " + GetLabel + "            : [{Val}]".format(Val = GetVal)
        #   retraction_speed 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("retraction_speed", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("retraction_speed", "label")
        replace_string = replace_string + "\n; " + GetLabel + "             : {Val}".format(Val = GetVal)        
        #   support_enable 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("support_enable", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("support_enable", "label")
        replace_string = replace_string + "\n; " + GetLabel + "             : [{Val}]".format(Val = GetVal)
        #   support_type
        GetValStr = Application.getInstance().getGlobalContainerStack().getProperty("support_type", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("support_type", "label")
        replace_string = replace_string + "\n; " + GetLabel + "            : " + GetValStr        
        #   infill_sparse_density 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("infill_sparse_density", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("infill_sparse_density", "label")
        replace_string = replace_string + "\n; " + GetLabel + "               : {Val} %".format(Val = GetVal)
        #   infill_pattern
        GetValStr = Application.getInstance().getGlobalContainerStack().getProperty("infill_pattern", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("infill_pattern", "label")
        replace_string = replace_string + "\n; " + GetLabel + "               : " + GetValStr
        #   material_diameter 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("material_diameter", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("material_diameter", "label")
        replace_string = replace_string + "\n; " + GetLabel + "                     : {Val} mm".format(Val = GetVal)
        #   ironing_enabled 
        GetVal = Application.getInstance().getGlobalContainerStack().getProperty("ironing_enabled", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("ironing_enabled", "label")
        replace_string = replace_string + "\n; " + GetLabel + "               : [{Val}]".format(Val = GetVal)
        #   adhesion_type
        GetValStr = Application.getInstance().getGlobalContainerStack().getProperty("adhesion_type", "value")
        GetLabel = Application.getInstance().getGlobalContainerStack().getProperty("adhesion_type", "label")
        replace_string = replace_string + "\n; " + GetLabel + "    : " + GetValStr          
        #   Fin de commentaire 
        replace_string = replace_string + "\n;=============================================="
        
        for layer_number, layer in enumerate(data):
            data[layer_number] = re.sub(search_regex, replace_string, layer) #Replace all.

        return data

 

Edited by Cuq
change title
  • Link to post
    Share on other sites

    Posted · Create a new Postprocessing Script

    A printer has one global stack that has common settings, but it typically has no materials. A printer also has one or more Extruder Stacks. You need to get the material from the extruder stack. This might work (but I have not tested it):

    GetValStr = Application.getInstance().getGlobalContainerStack().extruders[0].material.getMetaData().get("material", "")

    There are other settings that you should be getting from the extruder stack too, like material_diameter, material_flow, retraction+*, material_print_temperature, machine_nozzle_size, infill_*, speed_*

     

    There's a lot of code duplication in your script. You don't need to get the global stack over and over again; store it in a variable.

    • Like 1
    • Thanks 1
    Link to post
    Share on other sites

    Posted · Create a new Postprocessing Script
    1 hour ago, gr5 said:

    This is beyond my knowledge but maybe @burtoogle or @ctbeke or @ahoeben can help or at least know who can.

     

    54 minutes ago, ahoeben said:

    A printer has one global stack that has common settings, but it typically has no materials. A printer also has one or more Extruder Stacks. You need to get the material from the extruder stack. This might work (but I have not tested it):

    
    GetValStr = Application.getInstance().getGlobalContainerStack().extruders[0].material.getMetaData().get("material", "")

    There are other settings that you should be getting from the extruder stack too, like material_diameter, material_flow, retraction+*, material_print_temperature, machine_nozzle_size, infill_*, speed_*

     

     

    There's a lot of code duplication in your script. You don't need to get the global stack over and over again; store it in a variable.

     

    For the the majority of these parameters ( material_print_temperature, machine_nozzle_size , material_diameter etc ) . GetProperty function retrun the right values.

    GetVal = Application.getInstance().getGlobalContainerStack().getProperty("machine_nozzle_size", "value")

    But I don't find any parameter for the profil name  and the material base. 

     

    GetValStr = Application.getInstance().getGlobalContainerStack().extruders[0].material.getMetaData().get("material", "")

    Doesn't seems to be a valide solution

  • Link to post
    Share on other sites

    Posted (edited) · Create a new Postprocessing Script

    Ok step by step I'm closer to my goal   to get the Material name I'm using this code :

     

            # add extruder specific data to slice info
            extruders = list(Application.getInstance().getGlobalContainerStack().extruders.values())
            GetValStr = extruders[0].material.getMetaData().get("material", "")

     

    Does anyone of you knows how to get the Profil name ? 

    Edited by Cuq
  • Link to post
    Share on other sites

    Posted (edited) · Create a new Postprocessing Script
    42 minutes ago, Cuq said:

    Doesn't seems to be a valide solution

     

    Sorry, GlobalStack.extruders returns a dict, not a list. You could try

    ...getGlobalContainerStack().extruders["0"].material.getMetaData().get("material", "")

    For the profile, try this direction:

    ...getGlobalContainerStack().extruders["0"].qualityChanges.getMetaData().get("name", "")

    AND

    ...getGlobalContainerStack().extruders["0"].quality.getMetaData().get("name", "")

    Use the quality one if the qualityChanges one returns empty.

    Edited by ahoeben
    • Like 1
    Link to post
    Share on other sites

    Posted · Create a new Postprocessing Script

    Thank you @ahoeben !!!!   That's OK :

    • Profile name
    • Material name
    • Quality
            # add extruder specific data to slice info
            extruders = list(Application.getInstance().getGlobalContainerStack().extruders.values())
            #   Profile
            GetValStr = extruders[0].qualityChanges.getMetaData().get("name", "")
            GetLabel = "Profile"
            replace_string = replace_string + "\n; " + GetLabel + "                      : " + GetValStr        
            #   Material
            GetValStr = extruders[0].material.getMetaData().get("material", "")
            GetLabel = "Material"
            replace_string = replace_string + "\n; " + GetLabel + "                     : " + GetValStr
            #   Quality
            GetValStr = extruders[0].quality.getMetaData().get("name", "")
            GetLabel = "Quality"
            replace_string = replace_string + "\n; " + GetLabel + "                      : " + GetValStr

     

  • 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 the UltiMaker Factor 4
        We are happy to announce the next evolution in the UltiMaker 3D printer lineup: the UltiMaker Factor 4 industrial-grade 3D printer, designed to take manufacturing to new levels of efficiency and reliability. Factor 4 is an end-to-end 3D printing solution for light industrial applications
          • Thanks
          • Like
        • 3 replies
      • UltiMaker Cura 5.7 stable released
        Cura 5.7 is here and it brings a handy new workflow improvement when using Thingiverse and Cura together, as well as additional capabilities for Method series printers, and a powerful way of sharing print settings using new printer-agnostic project files! Read on to find out about all of these improvements and more. 
         
          • Like
        • 26 replies
    ×
    ×
    • Create New...