Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- # Copyright (C) 2012 Niklas Rosenstein
- #
- # This program is free software; you can redistribute it and/or modify it under
- # the terms of the GNU General Public License as published by the Free Software
- # Foundation; either version 3 of the License, or (at your option) any later
- # version.
- #
- # This program is distributed in the hope that it will be useful, but WITHOUT
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License along with
- # this program; if not, see <http://www.gnu.org/licenses/>.
- import os
- import c4d
- import string
- def write_vector(vector, file, prependix='', appendix='', delimiter=',',
- precision=2):
- """ Writes the passed `c4d.Vector` instance *vector* to the file-like
- object *file*. *prependix* and *appendix* are optional strings that
- will enclose the written vector. The vectors float-values will be
- separated by *delimiter*. The float-value precision is given with the
- integer value *precision*. """
- precision = int(precision)
- format = delimiter.join('%%.%df' % precision for i in xrange(3))
- file.write(prependix + (format % (vector.x, vector.y, vector.z)) + appendix)
- def write_spline(spline, file, precision=2):
- """ Writes the passed `c4d.SplineObject` *spline* in ASCII format to
- *file*. """
- if not isinstance(spline, c4d.SplineObject):
- raise TypeError("Expected SplineObject.")
- type = spline.GetInterpolationType()
- segment_count = spline.GetSegmentCount()
- type_ascii = { c4d.SPLINETYPE_BEZIER: 'bezier',
- c4d.SPLINETYPE_LINEAR: 'linear',
- c4d.SPLINETYPE_CUBIC: 'cubic',
- c4d.SPLINETYPE_AKIMA: 'akima',
- c4d.SPLINETYPE_BSPLINE: 'bspline', }.get(type, None)
- if type_ascii is None:
- raise ValueError('spline.GetInterpolationType() returned invalid type.')
- if segment_count > 0:
- segments = [spline.GetSegment(i) for i in xrange(segment_count)]
- else:
- segments = [{'cnt': op.GetPointCount(), 'closed': spline.IsClosed()}]
- segment_count = 1
- # write spline information
- file.write('spline %s, %d segments\n' % (type_ascii, segment_count))
- segment_offset = 0
- for segment in segments:
- # write segment information
- file.write('%d points, ' % segment['cnt'])
- if segment['closed']:
- file.write('closed\n')
- else:
- file.write('open\n')
- # write segment vectors
- for index in xrange(segment['cnt']):
- index += segment_offset
- point = spline.GetPoint(index)
- write_vector(point, file, precision=precision)
- # write tangent information if the spline is a Bezièr-Spline.
- if type == c4d.SPLINETYPE_BEZIER:
- tangents = op.GetTangent(index)
- tl, tr = tangents['vl'], tangents['vr']
- write_vector(tl, file, precision=precision, prependix=',',
- appendix=',')
- write_vector(tl, file, precision=precision)
- file.write('\n')
- segment_offset += segment['cnt']
- def main():
- if not op:
- return
- if not isinstance(op, c4d.SplineObject):
- c4d.gui.MessageDialog("select a spline object.")
- return
- path = c4d.storage.SaveDialog()
- if os.path.isdir(path):
- c4d.gui.MessageDialog("the seleected path is a directory.")
- return
- with open(path, 'w') as fl:
- write_spline(op, fl)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement