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

      • UltiMaker Cura 5.3 stable released
        In this stable release, Cura 5.3 achieves yet another huge leap forward in 3D printing thanks to material interlocking! As well as introducing an expanded recommended print settings menu and lots of print quality improvements. Not to mention, a whole bunch of new printer profiles for non-UltiMaker printers!
          • Thanks
          • Like
        • 56 replies
      • Here it is. The new UltiMaker S7
        The UltiMaker S7 is built on the success of the UltiMaker S5 and its design decisions were heavily based on feedback from customers.
         
         
        So what’s new?
        The obvious change is the S7’s height. It now includes an integrated Air Manager. This filters the exhaust air of every print and also improves build temperature stability. To further enclose the build chamber the S7 only has one magnetically latched door.
         
        The build stack has also been completely redesigned. A PEI-coated flexible steel build plate makes a big difference to productivity. Not only do you not need tools to pop a printed part off. But we also don’t recommend using or adhesion structures for UltiMaker materials (except PC, because...it’s PC). Along with that, 4 pins and 25 magnets make it easy to replace the flex plate perfectly – even with one hand.
         
        The re-engineered print head has an inductive sensor which reduces noise when probing the build plate. This effectively makes it much harder to not achieve a perfect first layer, improving overall print success. We also reversed the front fan direction (fewer plastic hairs, less maintenance), made the print core door magnets stronger, and add a sensor that helps avoid flooding.
         

         
        The UltiMaker S7 also includes quality of life improvements:
        Reliable bed tilt compensation (no more thumbscrews) 2.4 and 5 GHz Wi-Fi A 1080p camera (mounted higher for a better view) Compatibility with 280+ Marketplace materials Compatibility with S5 project files (no reslicing needed) And a whole lot more  
        Curious to see the S7 in action?
        We’re hosting a free tech demo on February 7.
        It will be live and you can ask any questions to our CTO, Miguel Calvo.
        Register here for the Webinar
          • Like
        • 18 replies
      • UltiMaker Cura Alpha 🎄 Tree Support Spotlight 🎄
        Are you a fan of tree support, but dislike the removal process and the amount of filament it uses? Then we would like to invite you to try this special release of UltiMaker Cura. Brought to you by our special community contributor @thomasrahm
         
        We generated a special version of Cura 5.2 called 5.3.0 Alpha + Xmas. The only changes we introduced compared to UltiMaker Cura 5.2.1 are those which are needed for the new supports. So keep in mind, this is not a sneak peek for Cura 5.3 (there are some really cool new features coming up) but a spotlight release highlighting this new version of tree supports.  
          • Like
        • 29 replies
    ×
    ×
    • Create New...