Skip to content
This repository has been archived by the owner on Jan 17, 2018. It is now read-only.

creating to post-processing script #31

Open
mypeopoly opened this issue Dec 7, 2016 · 10 comments
Open

creating to post-processing script #31

mypeopoly opened this issue Dec 7, 2016 · 10 comments

Comments

@mypeopoly
Copy link

Hi team,

I am looking to create a post processing script that will examine gcode and make some modification to the g0 / g1 x/y values for my DIY printer. I am not quite sure where to start. I tried to edit script directly in C:\Program Files\Cura 2.3\plugins\PostProcessingPlugin\scripts . It would work if I just change a value but if I add additional lines of codes Cura would not recognize the script t all.

Is there are way to create own script and added to cura? Thanks.

@Ghostkeeper
Copy link
Collaborator

Ghostkeeper commented Dec 7, 2016

I think a good place to start is to copy one of the scripts that is already in that scripts folder. You adding lines should normally not stop Cura from recognising it, unless you made a Python syntax error. Be aware that we use spaces for indenting in all of the pre-made scripts (you can use your preferred way of indenting, but if you mix indenting styles in one file you're asking for trouble).

Alternatively, you might want to check out if you can use the mesh_position_x, mesh_position_y and mesh_position_z settings instead, or machine_center_is_zero. It might be easier to just add one of those settings to your machine definition file if all you need to do is modify the X/Y positions. These settings are hidden in the interface, but you can still manually put them in the text files for your machines. Let me know if you need help.

@mypeopoly
Copy link
Author

Hi Ghostkeeper

Thanks for the quick reply. I just duplicating the existing an existing code and it still doesn't work:
image

after saving, Cura 2.3 only showing one script
image

These are scripts in C:\Program Files\Cura 2.3\plugins\PostProcessingPlugin\scripts and I tested on PauseAtHeight.py .

So how do I add a new script to the postprocessingplugin properly so it will be recognized and not messed up other scripts.

Regarding the mesh_position settings, the changes I intended to do in Gcode is non-uniform so a constant added to X/Y position wouldn't work. The script would scan line by line and make adjustments depending on the value of X and Y. I am happy to give out the script if other users find this useful.

@peteruithoven
Copy link
Contributor

You did give it a unique key and name (in the getSettingDataString function)?

@Ghostkeeper
Copy link
Collaborator

I am happy to give out the script if other users find this useful.

If you can make it sort-of generic, I think it'd be nice to merge it once you've got it working. Like, if you can make it apply a shear, or some other useful or aesthetic modification. The PostProcessing structure was specifically designed to allow for as many community contributions as possible.

@mypeopoly
Copy link
Author

Adding new script is ok now after giving it unique key and name.

But as soon as I add a line of code, even just a very simple one that I think follows the syntax. It wouldn't load again:
image

the link: park_z = park_x
is there a synatx error there? I have some programming experience but not in python. what am I missing and is there a reference I can use? Thanks for all your help

@Ghostkeeper
Copy link
Collaborator

Doesn't look like anything is wrong with that.

To help with debugging, take a look at the logs of Cura, found in C:\Users\<mypeopoly>\AppData\Local\low\cura\cura.log. If a script is not loading properly, it should give a message about it there.

@mypeopoly
Copy link
Author

Hey guys,

I am able to show my own script in Cura but still having problem making changes and making them loaded.

Here is an example: I stripped out most of the codes from one of the existing scripts and made these 2. One loads fine (xycorrect-cleaned up.py) and one doesn't (xycorrect.py). And the difference is only one character (y=0. instead of x=0.) I have some programming experience but not in python. Could you put to a reference where I can learn simply syntax for Python that would work in this context.

xycorrect.zip

@Ghostkeeper
Copy link
Collaborator

The file you provided mixes tabs and spaces for indenting. In Python, indentation matters, and tabs are counted as having a width of one character. That's why your script doesn't work. In your "cleaned up" version, you use spaces everywhere to indent.

@mypeopoly
Copy link
Author

Thanks. That took care of that. Notepad++ use tab instead of space and I just needed to change a setting.

Please help me understand the data structure. I use PauseAtHeight.py as reference.

in the code, the data refers to the silced gcode from Cura. It is broken down into layers, and then in each layer, there are individual lines of gcodes.

If I want to, say change every gcode line that starts with G0 or G1 into G0 X35 Y35, here is a code I created using codes from PauseAtHeight

    for layer in data:
        lines = layer.split("\n")
        for line in lines:
            if self.getValue(line, 'G') == 1 or self.getValue(line, 'G') == 0:
                line ="G1 X35.0 Y35.0"
    return data

--
I know it doesn't work and issue is were to put the "G1 X35.0 Y35.0" back into the right spot in the data. Could you point me to the right direction?

@Ghostkeeper
Copy link
Collaborator

Ghostkeeper commented Dec 10, 2016

line is a variable that's made for the for loop and filled with a line of g-code. You can change it, but it won't get updated in the lines list.
Details: Everything is by reference in Python. line gets filled with a reference to the string of g-code, which is a copy of the reference that the lines list has. Then you create a new string and alter the reference in line to point to the new string. But the reference in lines still points to the old string.

To update the lines, you're going to have to keep track of the index to put the new string back in place of the old string in the lines list, like so:

for layer in data:
    lines = layer.split("\n")
    for i, line in enumerate(lines): #enumerate() returns a sequence of pairs: (42, "G1 X21.662 Y22.442"), (43, "G10 S1"), etc...
        if self.getValue(line, "G") == 1 or self.getValue(line, "G") == 0:
            lines[i] = "G1 X35.0 Y35.0"
return data

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants