Advertisement
Guest User

OpenSCAD surface export plugin for Gimp

a guest
Oct 17th, 2012
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # openscad_surface_export.py
  4. # by Rob Antonishen
  5. # http://ffaat.pointclark.net
  6. #
  7. # Version 1.0 (20120907)
  8. #
  9. # Description
  10. # File save handler for openscad dat format
  11. # Borrows heavily from colorxhtml.py
  12. #
  13. # Changes
  14. #
  15. # License:
  16. #
  17. # This program is free software; you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation; either version 2 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. # GNU General Public License for more details.
  26. #
  27. # The GNU Public License is available at
  28. # http://www.gnu.org/copyleft/gpl.datfile
  29.  
  30. import struct
  31. import datetime
  32. import gimp
  33. from gimpfu import *
  34.  
  35. gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
  36.  
  37. def save_openscad_dat(img, drawable, filename, raw_filename):
  38.     width = drawable.width
  39.     height = drawable.height
  40.     bpp = drawable.bpp
  41.    
  42.     gimp.tile_cache_ntiles(width / gimp.tile_width() + 1)
  43.  
  44.     fileOut = file(filename,"w")
  45.    
  46.     pr = drawable.get_pixel_rgn(0, 0, width, height, False, False)
  47.    
  48.     gimp.progress_init(_("Saving as OpenSCAD .dat file"))
  49.    
  50.     fileOut.write("# Created by gimp plugin openscad_surface_export.py, %s\n" % datetime.datetime.strftime(datetime.datetime.now(), "%c"))
  51.     fileOut.write("# Name: %s\n" % img.name)
  52.     fileOut.write("# type: matrix\n")
  53.     fileOut.write("# Rows: %s\n" % height)
  54.     fileOut.write("# Columns: %s\n" % width)
  55.  
  56.     for y in range(height-1, -1, -1):
  57.         row = pr[0:width, y]
  58.         for pixel in RowIterator(row, bpp):
  59.             if drawable.is_gray:
  60.                 pixval = pixel[0]
  61.             else:
  62.                 pixval = (pixel[0] + pixel[1] + pixel[2]) / 3
  63.             fileOut.write(" %s" % str((pixval-128.0)/256.0))
  64.         fileOut.write("\n")
  65.         gimp.progress_update((height-y) / float(height))
  66.  
  67.     fileOut.close()
  68.    
  69. class RowIterator:
  70.     def __init__(self, row, bpp):
  71.         self.row = row
  72.         self.bpp = bpp
  73.  
  74.         self.start = 0
  75.         self.stop = bpp
  76.  
  77.         self.length = len(row)
  78.         self.fmt = 'B' * bpp
  79.  
  80.     def __iter__(self):
  81.         return iter(self.get_pixel, None)
  82.  
  83.     def get_pixel(self):
  84.         if self.stop > self.length:
  85.             return None
  86.  
  87.         pixel = struct.unpack(self.fmt, self.row[self.start:self.stop])
  88.  
  89.         self.start += self.bpp
  90.         self.stop += self.bpp
  91.  
  92.         return pixel
  93.        
  94. def register_save_dat():
  95.     gimp.register_save_handler("file-openscad-save", "dat", "")
  96.  
  97. register(
  98.         "file-openscad-save",
  99.         N_("Saves an OpenSCAD surface dat file."),
  100.         "Saves an OpenSCAD surface dat file.",
  101.         "Rob Antonishen",
  102.         "Rob Antonishen",
  103.         "2012",
  104.         N_("OpenSCAD Surface"),
  105.         "RGB, GRAY",
  106.         [
  107.             (PF_IMAGE, "image", "Input image", None),
  108.             (PF_DRAWABLE, "drawable", "Input drawable", None),
  109.             (PF_STRING, "filename", "The name of the file", None),
  110.             (PF_STRING, "raw-filename", "The name of the file", None),
  111.         ],
  112.         [],
  113.         save_openscad_dat, on_query=register_save_dat,
  114.         menu="<Save>", domain=("gimp20-python", gimp.locale_directory))
  115. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement