copy post "Python GCode processor"

108 views
Skip to first unread message

funBart

unread,
Dec 14, 2013, 6:53:35 PM12/14/13
to kisslicer-r...@googlegroups.com

  • Newbie
  • *
  • Posts: 35
    • View Profile
    •  
    • Email
    •  
    • Personal Message (Offline)
Python GCode processor
« on: May 02, 2013, 03:44:11 AM »
I posted a link to this deep in another thread, but likely many people will miss it there, so I'm starting a new thread so people can find it,  in case anyone finds it of use.  It's written in python 3.3.1, but should work in 3.2 or later.  If there's interest, I can look into making it an .exe instead of just a raw python script.

Some of the functionality will work on any g-code, but there's more stuff it can do with KISSlicer's output because of the copious comments (thanks Jonathan!) 

Here's what it is currently capable of:

First, the basics that work with any g-code:

 Offset XY position
 Remove redundant move coordinates (slightly smaller file) 
 Remove XY moves smaller than a given threshold
 Adjust fan speed, bed and extruder temperatures throughout file
 Adjust flow and feed rates throughout the file
 Basic replacement of one command for another (swap G0 for G1, for example)
 Comment out all instances of a specific code

Then the more fun KISSlicer specific stuff:

 Inject commands to display the current layer number on the LCD of your printer (M70 or M117 commands)
 Inject commands to set the RGB mood light LED to match the path type as shown in KISSlicer
 Inject commands to show the path type in the LCD of your printer
 Move the slicing summary from the bottom of the file to the top of the file
 Cool the bed temperature by X degrees at layer Y
 Turn on / off the fan for all 'Support Interface' paths (I find it makes it easier to remove the support cleanly)
 Turn on / off the fan for all 'Stacked Sparse Infill" paths
 Turn on the fan and adjust the extruder temperature to make the raft easier to remove cleanly (only activates if it detects there is a raft)

The layer and path type detection from the comments makes it easy to add additional functionality you may need based on path or layer.

April 30, 2013 -- Version 0.8 Initial Release
May 2, 2013 (quick fix) 

updates and issues can be found at the github: https://github.com/norpchen/ProcessGCode

Thanks,
Lars
* process_g_code.py (18 kB - downloaded 7 times.)

plexus

  • Newbie
  • *
  • Posts: 45
    • View Profile
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #1 on: May 06, 2013, 10:10:07 AM »
Wow there is lot of a good stuff in your script. now, a basic question that I have not been able to find an answer for: how you invoke this from KS? (im on a mac)

I was able to run the scrip in Terminal so that part works. but how do I call this script and its options from KS? I know there is a post-process field in KS but I am uncertain as to exactly what I have to enter there to run the script. 

FYI for others, here is the help output for the script. any chance we can get some how-to on this script? im not a coder and so some documentation would be nice, if possible, please.  :D

usage: process_g_code.py [-h] -i filename -o filename [-s] [-d mm]
                         [-u original replacement] [-f multiplier]
                         [-t multiplier] [-j degrees] [-n degrees]
                         [-b multiplier] [-k degrees layer] [-q fan_speed]
                         [-g fan_speed] [-w fan_speed temperaturedrop] [-x mm]
                         [-y mm] [-r multiplier] [-e multiplier] [-m] [-p]
                         [-v] [-l LCD_COMMAND] [-c] [--quote-comments]
                         [--version]


thanks!
« Last Edit: May 06, 2013, 10:25:11 AM by plexus »

selmo

  • Newbie
  • *
  • Posts: 35
    • View Profile
    •  
    • Email
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #2 on: May 07, 2013, 01:38:43 AM »
Run it with the command line option -h (for help) and it will give you a lot more useful information.  Sorry, I should have mentioned that.  ;)

As far as running it automatically, see the attached screenshot for the place to add it to KISSlicer. 

I call it through a batch file which sets my usual options:
Code: [Select]
python F:\ARTY_AND_CRAFTY\3dPrinting\process_g_code.py -i %1 -o %1.pp.gcode --cool-bed 6 10 --cool-support 255 --cool-sparse-infill 255 --cool-raft 255 35 -m -p --minimum-temperature 175 --replace M109 M104
The %1 means "the option passed to the batch file"   You could also paste the command into KISSlicer and replace the %1 with "<FILE>"  (the quotes are needed in case there's a space in the filename or path)

Also attached the latest version 0.8.2 (also available on github) which fixes a couple bugs and one feature (enforce a maximum and minimum extruder temperature)

* Clipboard Image.png (8.42 kB, 543x279 - viewed 31 times.)
* process_g_code.py (18.76 kB - downloaded 1 times.)
« Last Edit: May 07, 2013, 01:41:39 AM by selmo »

selmo

  • Newbie
  • *
  • Posts: 35
    • View Profile
    •  
    • Email
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #3 on: May 07, 2013, 01:43:44 AM »
Here's what you get if you use -h command line option: 

usage: process_g_code.py [-h] -i filename -o filename [-s] [-d mm]
                         [-u original replacement] [-f multiplier]
                         [-t multiplier] [-j degrees] [-n degrees]
                         [-b multiplier] [-k degrees layer] [-q fan_speed]
                         [-g fan_speed] [-w fan_speed temperaturedrop] [-x mm]
                         [-y mm] [-r multiplier] [-e multiplier] [-m] [-p]
                         [-v] [-l LCD_COMMAND] [-c] [--quote-comments]
                         [--version]

Monkey around with GCode (especially from KISSlicer) written by Lars Norpchen,
http://www.octopusmotor.com

optional arguments:
  -h, --help            show this help message and exit
  -i filename, --input filename
                        specify the input file to process
  -o filename, --output filename
                        specify the output file to generate
  -s, --strip           Strip redundant move command parameters. Saves a
                        little space, should not change the result, in
                        theory... use at your own risk!
  -d mm, --decimate mm  Drop XY movements smaller than this. Useful to get rid
                        of excessive "micromoves" that are below the printer's
                        resolution. Requires "--strip" option enabled to work
  -u original replacement, --replace original replacement
                        Replace a code with another code. Regex coding is
                        supported (^ for beginning of line, etc). Can be used
                        to comment out codes by adding a ";" to the code.
  -f multiplier, --fan multiplier
                        Multiply all fan speeds by this. This only affects fan
                        speeds that were in the original file, not those fan
                        speed commands added by options in this script
  -t multiplier, --temperature multiplier
                        Multiply all extruder temperatures by this.
  -j degrees, --minimum-temperature degrees
                        Enforce a minimum temperature for all extruder
                        temperature settings (including raft cooling). Will
                        not override extruder off (temp=0) commands.
  -n degrees, --maximum-temperature degrees
                        Enforce a maximum temperature for all extruder
                        temperature settings
  -b multiplier, --bed multiplier
                        Multiply all bed temps by this
  -k degrees layer, --cool-bed degrees layer
                        KISSlicer only. Decrease the bed temperature by
                        DEGREES at specified LAYER
  -q fan_speed, --cool-support fan_speed
                        KISSlicer only. Turns the fan on for all "Support
                        Interface" paths. Fan speed is 0 - 255.
  -g fan_speed, --cool-sparse-infill fan_speed
                        KISSlicer only. Turns the fan on for all "Sparse
                        Infill" paths. Fan speed is 0 - 255.
  -w fan_speed temperaturedrop, --cool-raft fan_speed temperaturedrop
                        KISSlicer only. Adjusts the fan and extrusion
                        temperature to make it easier to remove the raft. Set
                        the fan speed (0-255) and temperature reduction (in
                        degrees) for first object layer
  -x mm, --xoffset mm   Offset all X movements by this. Use only with absolute
                        coordinate mode.
  -y mm, --yoffset mm   Offset all Y movements by this. Use only with absolute
                        coordinate mode.
  -r multiplier, --feedrate multiplier
                        Multiply all movement rates by this (X, Y, Z and
                        Extruder)
  -e multiplier, --extrusion-flow multiplier
                        Multiply extrusion amount by this.
  -m, --move-header     KISSlicer only. Moves the slicing summary at the end
                        of the file to the head of the file
  -p, --print-layer     KISSlicer only. Print the current layer number on the
                        LCD display
  -v, --verbose         KISSlicer only. Show movement type comments on the LCD
                        display. This command can be risky on some machines
                        because it adds a lot of extra chatter to the user
                        interface and may cause problems during printing.
  -l LCD_COMMAND, --LCD-command LCD_COMMAND
                        Set the G-Code M command for showing a message on the
                        device display. M117 for Marlin, M70 for ReplicatorG
                        (default)
  -c, --colored-movements
                        KISSlicer only. Set RGB LED to show the KISSlicer path
                        type using the M420 command (Makerbot). This command
                        can be risky on some machines because it adds a lot of
                        extra chatter to the user interface and may cause
                        problems during printing.
  --quote-comments      LCD display commands will wrap quotes around the
                        message
  --version             show program's version number and exit

selmo

  • Newbie
  • *
  • Posts: 35
    • View Profile
    •  
    • Email
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #4 on: May 09, 2013, 06:26:14 AM »
FYI, I uploaded a new version on github that adds a few new features:

1. Resume from a given line number (experimental).  All movement commands prior to line number will be tracked but not move the head.  XY position will be recovered, but Z height must be set manually. Be careful of resuming after a retraction, as you might get a blob if your filament moves.
2.  Convert all movement to absolute or relative mode
3.  Remove or pad all comments.  Padding comments puts a null movement G0 command in front of each comment to prevent the line from being stripped when sent to the printer by the host (useful if you need to track line numbers for a resume).

Kiss-Lover

  • Newbie
  • *
  • Posts: 15
    • View Profile
    •  
    • Personal Message (Offline)
Selmo,

Great work!
I would like to submit an idea of utility script to implement, allowing to use KS for dualtrusion (or more) by merging 2 KS gcodes, at the layer level.

Preparing the 2 gcodes:
- You CAD a part with 2 bodies, actually representing each a variation (color, material, nozzle size),
- you export 2 STLs correctly mapped in XYZ,
- you render the 2 with KS, specifying a different extruder for each body/gcode.

Now, your script merges the 2 gcodes, layer by layer, where you insert T0 for gcode1 island(s) and T1 to gcode2 island(s) for each processed layer.
But the tricky thing is when switching between nozzles, like oozing.
Perhaps building a "wipe tower" where each nozzle goes when switching, in order to get rid of possible plastic leakage (as the extruder to enter in action was sleeping hot).

If this is possible, we can experiment with dualstrusion before KS implement it !  :)

Finishing a dual extruder printer (ORD BOT Hadron), I can help you and test it very soon.
« Last Edit: June 03, 2013, 01:16:41 PM by Kiss-Lover »
Printing with Makergear M2
Designing custom Hotend & nozzles. (High-temp)

(Dual ORD BOT under construction)

selmo

  • Newbie
  • *
  • Posts: 35
    • View Profile
    •  
    • Email
    •  
    • Personal Message (Offline)
I had been thinking about the ability to split and merge two gcode files, at the layer level, which could be used to support things like different layer heights in the same print or importing a first layer brim from another slicer app... but I wasn't considering interleaving

But the tricky thing is when switching between nozzles, like oozing.
Perhaps building a "wipe tower" where each nozzle goes when switching, in order to get rid of possible plastic leakage (as the extruder to enter in action was sleeping hot).

yah, that's the sort of thing why it is best handled within the slicer app.  A script would have a harder time finding a good place to position a wipe tower, for example.  To add the wipe tower it would need to generate new movement and extrusion commands which means it needs to know things like printer speeds, extrusion rates, extrusion widths, etc.  At that point you've rewritten half the internals of a slicer!

If this is possible, we can experiment with dualstrusion before KS implement it !  :)

I understand the desire to support dual extruders asap -- I would be in the same position if I had a dual extruder machine (I'm sure I will eventually).  But I'm also not comfortable with the idea of trying to do an end-run around KS here.  Dual extruder support is one of the few things that Jonathon has as a pro-only feature, while generously leaving most other functionality available for free.  While I'm sure it wasn't your intent, I feel that creating workarounds would be undermining his income.  

Let's give him a chance to finish his currently in-progress multi-color printing effort--I'm sure it will work better than a scripted post-processing hack could be.

Kiss-Lover

  • Newbie
  • *
  • Posts: 15
    • View Profile
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #7 on: June 04, 2013, 12:07:52 AM »
I totally support Jonathan's work, with a pro license, and I don't think merging 2 gcodes layer by layer is a problem for KS upcoming multimat features.
Even if KS is not open-source, there is a free spirit to always go beyond features, and ask for more.
KS is not CUBEX firmware, gcode is not encrypted, and what KS generates, gcode, can already be tweaked by your existing script, and other solutions.
So the same reasoning could apply: KS is supplemented by your existing post processings and could undermine potential optimization features planned by jonathan. Obviously, it is not, of course!  :)

Not true. KS PRO is not allowing dualstrusion (Merging 2 objects) at this time.
... Dual extruder support is one of the few things that Jonathon has as a pro-only feature, while generously leaving most other functionality available for free...
KS PRO allows to specialize up to 3 extruders for: Object, Interface, Support and Raft. But KS is not supporting multimaterial printing, like having conceptually Object1 and Object2 (Part having 2 subparts).
So, merging 2 gcodes by interleaving layers is not something KS PRO can do already.
The wipe tower is not a necessity, brute merging with T0/T1 is already good. This avoids any slicer aspect.

As Jonathan mentioned, it is interesting to pave the way towards dualstrusion, and all fans who can help in this direction, can help !
You can already merge gcode with replicator and github/Gblend.
Experimenting merged layers dualstrusion can only reveal issues and features from experimenters, that can inspire slicers features for an optimal slicer integrated dualstrusion KS PRO new feature. Adding pure gcode merge allows to demonstrate early dualstrusion by loyal KS PRO licensees. 

Jonathan, does this merging script  idea endanger KS upcoming features? I hear you saying: "No! go ahead, and show bicolor prints sliced by KS PRO!"  :D

« Last Edit: June 04, 2013, 06:19:32 AM by Kiss-Lover »
Printing with Makergear M2
Designing custom Hotend & nozzles. (High-temp)

(Dual ORD BOT under construction)

lonesock (Jonathan)

  • KISSlicer Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1042
  • Alibre -> KISSlicer -> RapMan 3.1
    • View Profile
    •  
    • The KISSlicer Website
    •  
    • Email
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #8 on: June 05, 2013, 12:38:55 PM »
I appreciate the concern, thanks.  I am OK with this.  Just know that I am working on this right now (including > 3 extruders [8^).

Jonathan
*A2TD : "Added to the ToDo list"

Kiss-Lover

  • Newbie
  • *
  • Posts: 15
    • View Profile
    •  
    • Personal Message (Offline)
Green Light!
« Reply #9 on: June 06, 2013, 04:11:58 AM »
Obviously. Thanks, Jonathan!
And good news: dualstrusion & tristrusion!

Selmo, we got the green light to proceed in this very interesting layer interleaving script (to dualstrude).

As you know, Jonathan, the "tristrusion" requires a modified ramps with an added extruder output.
I don't know many such of them being reliable.
The one I am considering is the ELEFU one: www.elefu.com/index.php?route=product/product&product_id=51
Also for tristrusion, it seems wise to switch to triple bowden, avoiding carriage excessive weight.
« Last Edit: October 17, 2013, 06:28:59 AM by Kiss-Lover »
Printing with Makergear M2
Designing custom Hotend & nozzles. (High-temp)

(Dual ORD BOT under construction)

Kenzu

  • Newbie
  • *
  • Posts: 11
    • View Profile
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #10 on: December 06, 2013, 02:30:50 AM »
Will it be posible to adjust temperature based on layer time/speed? Say you print a pyramid, then lower the temp the closer you get to the top and the lower the speed get. 
Or up the temp on layer with high speed to acount for bigger flow?
Can you be a zero if you come before number one?

PenskeGuy

  • Hero Member
  • *****
  • Posts: 1236
  • Any Day I Learn Something New Is A Good Day
    • View Profile
    •  
    • Spherical Magic | Blown Glass, Carbon Fiber and Imagination
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #11 on: December 06, 2013, 05:10:34 AM »
That sure would be good. Optimum flow characteristics depend upon a good velocity/viscosity relationship.
3D Touch Dual Head | KISSlicer Pro
Space Art | 3D Modeling | Blown Glass Sculpture | Blog | Bio

Kiss-Lover

  • Newbie
  • *
  • Posts: 15
    • View Profile
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #12 on: December 13, 2013, 02:49:40 AM »
Selmo,

Not considering gcode merging to help us experiment with dualstrusion?

Jonathan was like implementing this, but i see that Jonathan is not logged here since 30th of september, and the news section is infected with unrelated posts.
Does KS pro is still in development ? expecting new features (dualstrusion).
I do totally stick to Jonathan work, and will continue to support (donate), but pro users deserve some infos/updates.

Penske, some news about all this? Thanks.
Printing with Makergear M2
Designing custom Hotend & nozzles. (High-temp)

(Dual ORD BOT under construction)

PenskeGuy

  • Hero Member
  • *****
  • Posts: 1236
  • Any Day I Learn Something New Is A Good Day
    • View Profile
    •  
    • Spherical Magic | Blown Glass, Carbon Fiber and Imagination
    •  
    • Personal Message (Offline)
Re: Python GCode processor
« Reply #13 on: December 13, 2013, 01:57:56 PM »
Nothing yet to report. We're working on some options.
3D Touch Dual Head | KISSlicer Pro
Space Art | 3D Modeling | Blown Glass Sculpture | Blog | Bio
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages