Jump to content

call meshwriter as tool


mafi

Recommended Posts

Posted · call meshwriter as tool

Hello,
I want to run my own post processsing script via a nice tool button in cura.
i have it running in file/export ... but this is so much more work 😉
It would be nice to have it on the left side and when i click on and i haven't clicked slice than it starts slicing and when finished it directly starts my srcipt.

I don't want to get a file which i have to pass to my script where i have to read and overwrite.
It should be possible to directly pipe the stream to my scritp so only one time a file is written.

I know its a difficult question, if you need more information pleas let me know.

kind regards

  • Link to post
    Share on other sites

    Posted · call meshwriter as tool
    12 hours ago, mafi said:

    I want to run my own post processsing script via a nice tool button in cura.

    That is not how a tool should work in Cura. The toolbuttons in the toolpanel on the left are meant to activate tools that manipulate the models on the buildplate.

     

    It would be more in line with how Cura works if you add a setting to enable your postprocessing code, like for example the Z Offset and Linear Advance plugins do:

    https://github.com/fieldOfView/Cura-ZOffsetPlugin

    https://github.com/fieldOfView/Cura-LinearAdvanceSettingPlugin

    Those plugins show you how to apply your postprocessing code when the user saves a gcode file (so the file that gets saved is processed).

    • Thanks 1
    Link to post
    Share on other sites

    Posted · call meshwriter as tool

    What is wrong with the button that is already there? What do you want to do that can not be done in the same way that for example the Z Offset plugin acts on the gcode?

  • Link to post
    Share on other sites

    Posted (edited) · call meshwriter as tool

    My post processing script is a complet standalone qui where i can modify multi extruder settings for each model for infill. All based on comments in the final gcode.

    So i want to do my own export function in cura.
    i dont want to do the three extra clicks on slicing, file export ....
    espacially i have to save the gcode data twice to disk. First before postprocessing and after.
    Would be very nice to grap the mesh stream from export and a custom button in gui.

    Edited by mafi
  • Link to post
    Share on other sites

    Posted · call meshwriter as tool

    It sounds like you want a "post processing plugin".  Which is what those examples are above.  It's easy (at least it used to be) to have one program be both a plugin and also a standalone.  Please look at one of the examples above.  We are talking about maybe 5 lines of code to make a standalone program also be a plugin (at least that's how it used to be the last time I looked into "post processing plugins".

     

    I assume you mean it's a CLI standalone and not some fancy gui where you have to click "file open" and such.  A standalone CLI program needs to be told where the file to process is located and so on.  I believe the post processing plugin gets the data on the input stream?  Maybe?  I'm not sure.  Writing a standalone program that can do either is pretty easy.  You don't even need a special option (e.g. "-plug") as the plugin should be able to detect which mode it is being used.

  • Link to post
    Share on other sites

    Posted · call meshwriter as tool

    Okay I just looked at some modern post processing plugins.  I looked at the 2 by fieldofview and I looked at this one:

    https://github.com/Ultimaker/Cura/blob/master/plugins/PostProcessingPlugin/scripts/PauseAtHeight.py

     

    And it is definitely more than 5 lines of code (legacy cura was much simpler).

     

    But it's not to hard to separate out the "guts" of the code into a function/functions and call that from either the standalone code or from the plugin version.  You can have the "basic business rules" of your plugin in one function or a set of functions and then add one python file for standalone mode and another python file for plugin mode.

     

    I wonder if someone already did this...

  • Link to post
    Share on other sites

    Posted (edited) · call meshwriter as tool
    from . import Prozessor
    from UM.i18n import i18nCatalog
    catalog = i18nCatalog("cura")
    
    def getMetaData():
        return {
            "mesh_writer": {
                "output": [{
                    "description": catalog.i18nc("@item:inlistbox", "00MyOWN00"),
                    "mime_type": "text/00foo00",
                    "mode": Prozessor.MyOWNGCodeWriter.OutputMode.TextMode
                }]
            }
        }
    
    def register(app):
        return {"mesh_writer": Prozessor.MyOWNGCodeWriter()}
    
    from UM.PluginRegistry import PluginRegistry
    from UM.Mesh.MeshWriter import MeshWriter
    from UM.Application import Application
    from UM.Logger import Logger
    
    from typing import cast
    import subprocess
    import pathlib
    
    class MyOWNGCodeWriter(MeshWriter):
    
    	version = 3
    	
    	def __init__(self):
    		super().__init__(add_to_recent_files = False)
    		self._application = Application.getInstance()
    
    	def write(self, stream, nodes, mode = MeshWriter.OutputMode.TextMode):
    		gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
    		success = gcode_writer.write(stream, None)
    		stream.close()
    		if not success:
    			self.setInformation(gcode_writer.getInformation())
    			return False
    
    		this_file_path = pathlib.Path(__file__).parent
    
    		exe_path = str(this_file_path.joinpath("Prozessor.exe").absolute())
    		command = ["cmd", "/c", exe_path, stream.name]
    		Logger.log("i", "-=-=-=-=-=-=-=-=-=-=> Execute: "+str(" ".join(command)))
    		process = subprocess.Popen(command)
    		process.wait()
    		if process.returncode == 0:
    			Logger.log("i", "-=-=-=-=-=-=-=-=-=-=>  processing was succesfull")
    		else:
    			Logger.log("w", "-=-=-=-=-=-=-=-=-=-=>  execution failed")
    
    		return True

    If you delet all the logging its only 5 lines 😉 (maybe more)
    Its not big.

    How do i get the cast if i am not a export plugin?
     

    cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))


    How do i get a button in gui if i am not a tool plugin?
    How do i start slicing via code?

    Edited by mafi
  • Link to post
    Share on other sites

    Posted · call meshwriter as tool
    5 hours ago, mafi said:

    How do i get a button in gui if i am not a tool plugin?

    Have a look at the Automatic Slicing Toggle plugin:

    https://marketplace.ultimaker.com/app/cura/plugins/fieldofview/PauseBackendPlugin

     

    5 hours ago, mafi said:

    How do i start slicing via code?

    From the QML button you could do this:

    https://github.com/Ultimaker/Cura/blob/master/resources/qml/ActionPanel/SliceProcessWidget.qml#L40

    Or from Python you could do

    Application.getInstance().getBackend().forceSlice()

     

    Having said that I still recommend you to implement this in a way that fits better with the normal Cura workflow (adjusting models and settings, slicing, previewing and then saving/printing), instead of going around it. What sort of standalone GUI are you talking about? Could it be integrated into Cura, as a stage? You could have a look at the Teton Smart Slice plugin.

  • Link to post
    Share on other sites

    Posted · call meshwriter as tool

    I don't know what a stage in CURA is, so far i trag n drop presliced gcode on my EXE and i can ajust the MultiExtrusion setting for Hight,  Infill or change speed at specifig Z hight.

    So far i have my Profiles where i dont have to ajust anything.
    And up to now i have to click slice, go up to file Export and export the gcode.
    When i am finisched i hopefully only click on my Workflow button.

    Will have a look on Weekend on your suggestion.

  • 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.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
        • 18 replies
      • S-Line Firmware 8.3.0 was released Nov. 20th on the "Latest" firmware branch.
        (Sorry, was out of office when this released)

        This update is for...
        All UltiMaker S series  
        New features
         
        Temperature status. During print preparation, the temperatures of the print cores and build plate will be shown on the display. This gives a better indication of the progress and remaining wait time. Save log files in paused state. It is now possible to save the printer's log files to USB if the currently active print job is paused. Previously, the Dump logs to USB option was only enabled if the printer was in idle state. Confirm print removal via Digital Factory. If the printer is connected to the Digital Factory, it is now possible to confirm the removal of a previous print job via the Digital Factory interface. This is useful in situations where the build plate is clear, but the operator forgot to select Confirm removal on the printer’s display. Visit this page for more information about this feature.
          • Like
        • 0 replies
    ×
    ×
    • Create New...