2 #Info: Change printing parameters at a given height
6 #Param: targetZ(float:5.0) Z height to tweak at (mm)
7 #Param: targetL(int:) (ALT) Layer no. to tweak at
8 #Param: speed(int:) New Speed (%)
9 #Param: flowrate(int:) New Flow Rate (%)
10 #Param: platformTemp(int:) New Bed Temp (deg C)
11 #Param: extruderOne(int:) New Extruder 1 Temp (deg C)
12 #Param: extruderTwo(int:) New Extruder 2 Temp (deg C)
13 #Ex3 #Param: extruderThree(int:) New Extruder 3 Temp (deg C)
14 #Param: fanSpeed(int:) New Fan Speed (0-255 PWM)
16 ## Written by Steven Morlock, smorloc@gmail.com
17 ## Modified by Ricardo Gomez, ricardoga@otulook.com, to add Bed Temperature and make it work with Cura_13.06.04+
18 ## Modified by Stefan Heule, Dim3nsioneer@gmx.ch, to add Flow Rate, restoration of initial values when returning to low Z, extended stage numbers, direct stage manipulation by GCODE-comments, UltiGCode regocnition, addition of fan speed, alternative selection by layer no., disabling extruder three
19 ## This script is licensed under the Creative Commons - Attribution - Share Alike (CC BY-SA) terms
22 # M220 S<factor in percent> - set speed factor override percentage
23 # M221 S<factor in percent> - set flow factor override percentage
24 # M104 S<temp> T<0-#toolheads> - set extruder <T> to target temperature <S>
25 # M140 S<temp> - set bed target temperature
26 # M106 S<PWM> - set fan speed to target speed <S>
29 #V3.0.1: TweakAtZ-state default 1 (i.e. the plugin works without any TweakAtZ comment)
30 #V3.1: Recognizes UltiGCode and deactivates value reset, fan speed added, alternatively layer no. to tweak at, extruder three temperature disabled by '#Ex3'
36 def getValue(line, key, default = None):
37 if not key in line or (';' in line and line.find(key) > line.find(';') and not ";TweakAtZ" in key and not ";LAYER:" in key):
39 subPart = line[line.find(key) + len(key):] #allows for string lengths larger than 1
40 if ";TweakAtZ" in key:
41 m = re.search('^[0-3]', subPart)
42 elif ";LAYER:" in key:
43 m = re.search('^[+-]?[0-9]*', subPart)
45 m = re.search('^[0-9]+\.?[0-9]*', subPart)
49 return float(m.group(0))
53 with open(filename, "r") as f:
61 #Ex3 old_extruderThree = -1
67 layer = -100000 #layer no. may be negative (raft) but never that low
68 state = 1 #state 0: deactivated, state 1: activated, state 2: active, but below z, state 3: active, passed z
69 no_reset = 0 #Default setting is reset (ok for Marlin/Sprinter), has to be set to 1 for UltiGCode (work-around for missing default values)
72 targetL_i = int(targetL)
77 with open(filename, "w") as f:
80 if 'FLAVOR:UltiGCode' in line: #Flavor is UltiGCode! No reset of values
82 if ';TweakAtZ-state' in line: #checks for state change comment
83 state = getValue(line, ';TweakAtZ-state', state)
84 if ';LAYER:' in line: #new layer no. found
85 layer = getValue(line, ';LAYER:', layer)
86 if targetL_i > -100000: #target selected by layer no.
87 if state == 2 and layer >= targetL_i: #determine targetZ from layer no.
89 if (getValue(line, 'T', None) is not None) and (getValue(line, 'M', None) is None): #looking for single T-command
90 pres_ext = getValue(line, 'T', pres_ext)
91 if 'M190' in line or 'M140' in line and state < 3: #looking for bed temp, stops after target z is passed
92 old_platformTemp = getValue(line, 'S', old_platformTemp)
93 if 'M109' in line or 'M104' in line and state < 3: #looking for extruder temp, stops after target z is passed
94 if getValue(line, 'T', pres_ext) == 0:
95 old_extruderOne = getValue(line, 'S', old_extruderOne)
96 elif getValue(line, 'T', pres_ext) == 1:
97 old_extruderTwo = getValue(line, 'S', old_extruderTwo)
98 #Ex3 elif getValue(line, 'T', pres_ext) == 2:
99 #Ex3 old_extruderThree = getValue(line, 'S', old_extruderThree)
100 if 'M107' in line: #fan is stopped; is always updated in order not to miss switch off for next object
102 if 'M106' in line and state < 3: #looking for fan speed
103 old_fanSpeed = getValue(line, 'S', old_fanSpeed)
104 if 'G1' in line or 'G0' in line:
105 newZ = getValue(line, 'Z', z)
106 x = getValue(line, 'X', x)
107 y = getValue(line, 'Y', y)
110 if z < targetZ and state == 1:
112 if z >= targetZ and state == 2:
114 if targetL_i > -100000:
115 f.write(";TweakAtZ V%s: executed at Layer %d\n" % (version,targetL_i))
117 f.write(";TweakAtZ V%s: executed at %1.2f mm\n" % (version,targetZ))
118 if speed is not None and speed != '':
119 f.write("M220 S%f\n" % float(speed))
120 if flowrate is not None and flowrate != '':
121 f.write("M221 S%f\n" % float(flowrate))
122 if platformTemp is not None and platformTemp != '':
123 f.write("M140 S%f\n" % float(platformTemp))
124 if extruderOne is not None and extruderOne != '':
125 f.write("M104 S%f T0\n" % float(extruderOne))
126 if extruderTwo is not None and extruderTwo != '':
127 f.write("M104 S%f T1\n" % float(extruderTwo))
128 #Ex3 if extruderThree is not None and extruderThree != '':
129 #Ex3 f.write("M104 S%f T2\n" % float(extruderThree))
130 if fanSpeed is not None and fanSpeed != '':
131 f.write("M106 S%d\n" % int(fanSpeed))
132 if z < targetZ and state == 3: #re-activates the plugin if executed by pre-print G-command, resets settings
134 if no_reset == 0: #executes only for UM Original and UM2 with RepRap flavor
135 if targetL_i > -100000:
136 f.write(";TweakAtZ V%s: reset below Layer %d\n" % (version,targetL_i))
138 f.write(";TweakAtZ V%s: reset below %1.2f mm\n" % (version,targetZ))
139 if speed is not None and speed != '':
140 f.write("M220 S%f\n" % float(old_speed))
141 if flowrate is not None and flowrate != '':
142 f.write("M221 S%f\n" % float(old_flowrate))
143 if platformTemp is not None and platformTemp != '':
144 f.write("M140 S%f\n" % float(old_platformTemp))
145 if extruderOne is not None and extruderOne != '':
146 f.write("M104 S%f T0\n" % float(old_extruderOne))
147 if extruderTwo is not None and extruderTwo != '':
148 f.write("M104 S%f T1\n" % float(old_extruderTwo))
149 #Ex3 if extruderThree is not None and extruderThree != '':
150 #Ex3 f.write("M104 S%f T2\n" % float(old_extruderThree))
151 if fanSpeed is not None and fanSpeed != '':
152 f.write("M106 S%d;\n" % int(old_fanSpeed))