Advertisement
nux95

Spline Exporter Script

Jul 14th, 2012
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. # coding: utf-8
  2. # Copyright (C) 2012  Niklas Rosenstein
  3. #
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program; if not, see <http://www.gnu.org/licenses/>.
  15.  
  16. import os
  17. import c4d
  18. import string
  19.  
  20. def write_vector(vector, file, prependix='', appendix='', delimiter=',',
  21.                  precision=2):
  22.     """ Writes the passed `c4d.Vector` instance *vector* to the file-like
  23.        object *file*. *prependix* and *appendix* are optional strings that
  24.        will enclose the written vector. The vectors float-values will be
  25.        separated by *delimiter*. The float-value precision is given with the
  26.        integer value *precision*. """
  27.     precision = int(precision)
  28.     format = delimiter.join('%%.%df' % precision for i in xrange(3))
  29.     file.write(prependix + (format % (vector.x, vector.y, vector.z)) + appendix)    
  30.  
  31. def write_spline(spline, file, precision=2):
  32.     """ Writes the passed `c4d.SplineObject` *spline* in ASCII format to
  33.        *file*. """
  34.  
  35.     if not isinstance(spline, c4d.SplineObject):
  36.         raise TypeError("Expected SplineObject.")
  37.  
  38.     type = spline.GetInterpolationType()
  39.     segment_count = spline.GetSegmentCount()
  40.     type_ascii = { c4d.SPLINETYPE_BEZIER:  'bezier',
  41.                    c4d.SPLINETYPE_LINEAR:  'linear',
  42.                    c4d.SPLINETYPE_CUBIC:   'cubic',
  43.                    c4d.SPLINETYPE_AKIMA:   'akima',
  44.                    c4d.SPLINETYPE_BSPLINE: 'bspline', }.get(type, None)
  45.     if type_ascii is None:
  46.         raise ValueError('spline.GetInterpolationType() returned invalid type.')
  47.  
  48.     if segment_count > 0:
  49.         segments = [spline.GetSegment(i) for i in xrange(segment_count)]
  50.     else:
  51.         segments = [{'cnt': op.GetPointCount(), 'closed': spline.IsClosed()}]
  52.         segment_count = 1
  53.  
  54.     # write spline information
  55.     file.write('spline %s, %d segments\n' % (type_ascii, segment_count))
  56.  
  57.     segment_offset = 0
  58.     for segment in segments:
  59.  
  60.         # write segment information
  61.         file.write('%d points, ' % segment['cnt'])
  62.         if segment['closed']:
  63.             file.write('closed\n')
  64.         else:
  65.             file.write('open\n')
  66.  
  67.         # write segment vectors
  68.         for index in xrange(segment['cnt']):
  69.             index += segment_offset
  70.             point = spline.GetPoint(index)
  71.             write_vector(point, file, precision=precision)
  72.  
  73.             # write tangent information if the spline is a Bezièr-Spline.
  74.             if type == c4d.SPLINETYPE_BEZIER:
  75.                 tangents = op.GetTangent(index)
  76.                 tl, tr = tangents['vl'], tangents['vr']
  77.  
  78.                 write_vector(tl, file, precision=precision, prependix=',',
  79.                              appendix=',')
  80.                 write_vector(tl, file, precision=precision)
  81.  
  82.             file.write('\n')
  83.  
  84.         segment_offset += segment['cnt']
  85.  
  86. def main():
  87.     if not op:
  88.         return
  89.     if not isinstance(op, c4d.SplineObject):
  90.         c4d.gui.MessageDialog("select a spline object.")
  91.         return
  92.  
  93.     path = c4d.storage.SaveDialog()
  94.     if os.path.isdir(path):
  95.         c4d.gui.MessageDialog("the seleected path is a directory.")
  96.         return
  97.  
  98.     with open(path, 'w') as fl:
  99.         write_spline(op, fl)
  100.  
  101. if __name__ == "__main__":
  102.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement