Jump to content

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

      • Help Us Improve Cura – Join the Ultimaker Research Program
        🚀 Help Shape the Future of Cura and Digital Factory – Join Our Power User Research Program!
        We’re looking for active users of Cura and Digital Factory — across professional and educational use cases — to help us improve the next generation of our tools.
        Our Power User Research Program kicks off with a quick 15-minute interview to learn about your setup and workflows. If selected, you’ll be invited into a small group of users who get early access to features and help us shape the future of 3D printing software.

        🧪 What to Expect:
        A short 15-minute kickoff interview to help us get to know you If selected, bi-monthly research sessions (15–30 minutes) where we’ll test features, review workflows, or gather feedback Occasional invites to try out early prototypes or vote on upcoming improvements
        🎁 What You’ll Get:
         
        Selected participants receive a free 1-year Studio or Classroom license Early access to new features and tools A direct voice in what we build next
        👉 Interested? Please fill out this quick form
        Your feedback helps us make Cura Cloud more powerful, more intuitive, and more aligned with how you actually print and manage your workflow.
        Thanks for being part of the community,

        — The Ultimaker Software Team
        • 0 replies
      • Cura 5.10 stable released!
        The full stable release of Cura 5.10 has arrived, and it brings support for the new Ultimaker S8, as well as new materials and profiles for previously supported UltiMaker printers. Additionally, you can now control your models in Cura using a 3D SpaceMouse and more!
          • Like
        • 18 replies
    ×
    ×
    • Create New...