Gcode reader for RepRap variant generated by Slic3r #42925

Closed
opened 2014-12-16 23:08:04 +01:00 by Lee A. Butler · 10 comments

This script adds an "Import" capability for GCode files generated by the popular Slic3r generator. Each layer is created as an individual object consisting of a set of polyline curves which are beveled with an ellipse. Filenames should be of the form "file.gcode"

Source code and documentation also available on on GitHub

ioImportGcode.py

This script adds an "Import" capability for GCode files generated by the popular Slic3r generator. Each layer is created as an individual object consisting of a set of polyline curves which are beveled with an ellipse. Filenames should be of the form "file.gcode" Source code and documentation also available on [on GitHub ](https://github.com/iraytrace/BlenderGcodeImport.wiki.git) [ioImportGcode.py](https://archive.blender.org/developer/F132300/ioImportGcode.py)
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @butler

Added subscriber: @butler

Added subscriber: @ideasman42

Added subscriber: @ideasman42

Checked the script.

Can you exmplain the use case for having this?

Typically gcode is generated from 3D models (isnt it more of an output only format?).

Checked over the code, some basic notes.

  • polyline.points.add - try to batch these calls (add the total number of points, then fill them in), adding one at a time is quite inefficient (it realloc's each time internally).

  • I think the Operator subclass could be split out from the import class. The operator can just define the draw/execute/invoke functions. Would prefer to use a separate class for the internal loading logic. (most Blender importers do this too).

  • Using eval to run functions is insecure (gcode files could have code embedded) and unnecessary. Instead you can use:

    func = getattr(self, tokens[0], None)
    if func is not None:
        func(tokens[1:])
  • if delta in self.thickness.keys(): self.thickness[delta] isn't very efficient, you can instead do:
    try:
        self.thickness[delta] += 1
    except KeyError:
        self.thickness[delta] = 1
  

... this is faster too.

  • if axis not in npos.keys() can be written as if axis not in npos... applies to other areas.

  • if axis in ['X', 'Y', 'Z', 'E']:, can use a static-set eg: if axis in {'X', 'Y', 'Z', 'E'}:

  • replace for extensions is error prone (.gcode could exist in middle of string). use os.path.splitext instead.

Checked the script. Can you exmplain the use case for having this? Typically gcode is generated from 3D models (isnt it more of an output only format?). Checked over the code, some basic notes. - `polyline.points.add` - try to batch these calls (add the total number of points, then fill them in), adding one at a time is quite inefficient (it realloc's each time internally). - I think the Operator subclass could be split out from the import class. The operator can just define the draw/execute/invoke functions. Would prefer to use a separate class for the internal loading logic. (most Blender importers do this too). - Using `eval` to run functions is insecure (gcode files could have code embedded) and unnecessary. Instead you can use: ``` func = getattr(self, tokens[0], None) if func is not None: func(tokens[1:]) ``` - `if delta in self.thickness.keys(): self.thickness[delta]` isn't very efficient, you can instead do: ``` try: self.thickness[delta] += 1 except KeyError: self.thickness[delta] = 1 ``` ... this is faster too. - `if axis not in npos.keys()` can be written as `if axis not in npos`... applies to other areas. - `if axis in ['X', 'Y', 'Z', 'E']:`, can use a static-set eg: `if axis in {'X', 'Y', 'Z', 'E'}:` - `replace` for extensions is error prone (.gcode could exist in middle of string). use `os.path.splitext` instead.
Author

Thanks for the feedback! I'll look into making the changes you suggested.

The use case is to visualize the 3D print before printing. People just getting started with 3D printing often have trouble understanding the resolution limitations of 3D printing. The goal is to be able to pull the Gcode back into Blender and compare the 3D print to the object you modeled so you know what to fix before printing.

With regard to the "Operator" and "import" classes, can you point me at reference documentation so I can separate them out? I'm afraid that was something I just copied from someone else's script.

Thanks for the feedback! I'll look into making the changes you suggested. The use case is to visualize the 3D print before printing. People just getting started with 3D printing often have trouble understanding the resolution limitations of 3D printing. The goal is to be able to pull the Gcode back into Blender and compare the 3D print to the object you modeled so you know what to fix before printing. With regard to the "Operator" and "import" classes, can you point me at reference documentation so I can separate them out? I'm afraid that was something I just copied from someone else's script.

Regarding splitting the functions out

PLY Import is an example:

class:
https://developer.blender.org/diffusion/BA/browse/master/io_mesh_ply/init.py;b8577cee6fd9aa8483ce66e79df0cdfd6542f403$155

export function:
https://developer.blender.org/diffusion/BA/browse/master/io_mesh_ply/export_ply.py;b8577cee6fd9aa8483ce66e79df0cdfd6542f403$185

In this case theres no need to make them into separate files though.

Regarding splitting the functions out PLY Import is an example: class: https://developer.blender.org/diffusion/BA/browse/master/io_mesh_ply/__init__.py;b8577cee6fd9aa8483ce66e79df0cdfd6542f403$155 export function: https://developer.blender.org/diffusion/BA/browse/master/io_mesh_ply/export_ply.py;b8577cee6fd9aa8483ce66e79df0cdfd6542f403$185 In this case theres no need to make them into separate files though.

Added subscriber: @mont29

Added subscriber: @mont29

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Bastien Montagne self-assigned this 2015-05-22 11:40:05 +02:00

Nothing new here since months… closing for now, we can always reopen it should requested points be addressed.

Nothing new here since months… closing for now, we can always reopen it should requested points be addressed.

Changed status from 'Archived' to: 'Archived'

Changed status from 'Archived' to: 'Archived'
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#42925
No description provided.