Edit: double post.
Edited by Slashee_the_Cow
Thanks for the feedback. I was a bit quick in my post.
I am using the function extruderValue(0,'material_type') in the setting I have defined.
The setting is the following
"used_material":
{
"comments": "V3DP setting",
"default_value": "",
"description": "Comment with the materials used.",
"enabled": false,
"label": "Used materials",
"settable_per_extruder": true,
"type": "str",
"value": "'T0 '+ extruderValue(0,'material_type') if print_mode == \"singleT0\" else 'T1 ' + extruderValue(1,'material_type') if print_mode == \"singleT1\" else 'T0 ' + extruderValue(0,'material_type') + ' T1 ' + extruderValue(1,'material_type') if print_mode == \"dual\" else 'T0 ' + extruderValue(0,'material_type') + 'T1 ' + extruderValue(0,'material_type')"
},
I know there is a replacement pattern for that, but it is not conditional, so I would like to have a dedicated setting. That would allow only one machine instance for all the print modes of my printer.
- Solution
1 hour ago, V3DPrinting said:"value": "'T0 '+ extruderValue(0,'material_type') if print_mode == \"singleT0\" else 'T1 ' + extruderValue(1,'material_type') if print_mode == \"singleT1\" else 'T0 ' + extruderValue(0,'material_type') + ' T1 ' + extruderValue(1,'material_type') if print_mode == \"dual\" else 'T0 ' + extruderValue(0,'material_type') + 'T1 ' + extruderValue(0,'material_type')"
Are you chaining three ternaries in a single line? Not cool (from a programming perspective). But... are you actually evaluating Python in that string? Not cool (from a programming perspective). But if you are... can't you just use string functions to make your string upper case?
"value": "'T0 '+ extruderValue(0,'material_type').upper() if print_mode == \"singleT0\" else 'T1 ' + extruderValue(1,'material_type').upper() if print_mode == \"singleT1\" else 'T0 ' + extruderValue(0,'material_type').upper() + ' T1 ' + extruderValue(1,'material_type').upper() if print_mode == \"dual\" else 'T0 ' + extruderValue(0,'material_type').upper() + 'T1 ' + extruderValue(0,'material_type').upper()"
2 hours ago, Slashee_the_Cow said:Are you chaining three ternaries in a single line?
Yes, because you cannot do block statements.
2 hours ago, Slashee_the_Cow said:are you actually evaluating Python in that string
Yes, and Cura specifically allows for that, and it is used throughout many printer definitions.
@Slashee_the_Cow Thanks for the input works fine !
I was looking for the string function, but I haven't found it in my googling. I need to improve my Python knowledge ....
@ahoeben Thanks for your valuable help
@ahoeben I wasn't trying to criticise or anything, just my (unique and easily misunderstood) style of wit, commenting that neither of them are cool if you're writing a program and have free reign, but I could tell that @V3DPrinting knew what they were doing (anyone who can keep track of what they're doing when nesting ternaries three deep isn't just a one trick pony who thinks they're the solution for everything) so was working within limitations.
Although I didn't know you could use Python in machine definitions like that. You learn something new every day.
Edit: I just realised that it doesn't help that I really hate the ternary syntax in Python. Don't get me wrong, it was a gift from above when they added it, but I'll take a C-style condition ? if_true : if_false over that every day of the week.
Edited by Slashee_the_CowAdded comment about syntax
22 hours ago, V3DPrinting said:would like to return the material used in a specific extruder in my gcode
I have created a setting in my printer definition, children of blackmagic, using the extruder value extruderValue(extruder_nr,'material_type').
It is unnecessary to abuse the settings system for that. Instead, just hook into the outputdevicemanager writestarted signal like eg the postprocessing plugin does (or another place where you can access the "gcode_dict" attribute of the scene). Settings should be used for things that users can put a value in, which affect printing.
def __init__(self): super().__init__() self._application = Application.getInstance() output_manager = self._application.getOutputDeviceManager() output_manager.writeStarted.connect(self._filterGcode) def _filterGcode(self, output_device): scene = self._application.getController().getScene() gcode_dict = getattr(scene, "gcode_dict", {}) if not gcode_dict: # this also checks for an empty dict Logger.log("w", "Scene has no gcode to process") return for plate_id in gcode_dict: gcode_list = gcode_dict[plate_id] if len(gcode_list) < 2: Logger.log("w", "Plate %s does not contain any layers", plate_id) continue ... calculate/get used materials, insert them into gcode_list[0] ... setattr(scene, "gcode_dict", gcode_dict)
Edited by ahoeben
@ahoeben Thanks for the piece of information. I'll have a look.
Recommended Posts
Slashee_the_Cow 268
If you're declaring a setting like that... that doesn't look like anything I know about Cura... like, at all. You might have to provide some context as to where and how you're trying to use that, as well as the relevant files.
Link to post
Share on other sites