Skip to content

Instantly share code, notes, and snippets.

@tamask
Created February 8, 2019 11:42
Show Gist options
  • Save tamask/1b37c0a12a04b0db7120df11387b523f to your computer and use it in GitHub Desktop.
Save tamask/1b37c0a12a04b0db7120df11387b523f to your computer and use it in GitHub Desktop.
Blender: Export the active curve object (U-component only)
# This exporter just serializes Blender's bpy curve data API verbatim,
# so the exported json structure mirrors the variable names and
# arrays. The exporter only goes so far to export the U-component of
# the curve data, since we're more interested in curves and not NURBS
# surfaces for use in videogames. An asset importer in Unity need only
# look up the Blender Python API to choose how to interpret this data.
import bpy
import json
from bpy_extras.io_utils import ExportHelper
VERSION = 1
bl_info = {
'name': 'Export Curve (.curve)',
'author': 'Tamas Kemenczy',
'blender': (2, 80, 0),
'version': (0, 1),
'description': 'Export the active curve object (U-component only)',
'category': 'Import-Export',
}
def curve_to_dict(curve):
splines = []
curve_data = {
'version': VERSION,
'name': curve.name,
'splines': splines,
}
for spline in curve.splines:
points = []
bezier_points = []
spline_data = {
'type': spline.type,
'points': points,
'bezier_points': bezier_points,
'tilt_interpolation': spline.tilt_interpolation,
'radius_interpolation': spline.radius_interpolation,
'use_cyclic_u': spline.use_cyclic_u,
'use_endpoint_u': spline.use_endpoint_u,
'use_bezier_u': spline.use_bezier_u,
}
for p in spline.points:
p_data = {
'weight': p.weight,
'tilt': p.tilt,
'radius': p.radius,
'point': list(p.co),
}
points.append(p_data)
for bp in spline.bezier_points:
bp_data = {
'handle_left_type': bp.handle_left_type,
'handle_right_type': bp.handle_right_type,
'handle_left': list(bp.handle_left),
'handle_right': list(bp.handle_right),
'co': list(bp.co),
'tilt': bp.tilt,
'radius': bp.radius,
}
bezier_points.append(bp_data)
splines.append(spline_data)
return curve_data
class EXPORT_OTHER_OT_curve(bpy.types.Operator, ExportHelper):
bl_idname = 'export_other.curve'
bl_label = 'Export Curve (.curve)'
bl_options = {'REGISTER'}
filename_ext = ".curve"
filter_glob: bpy.props.StringProperty(default="*.curve", options={'HIDDEN'})
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.type == 'CURVE'
def execute(self, context):
if not self.filepath:
raise Exception("filepath not set")
data = curve_to_dict(context.active_object.data)
with open(self.filepath, 'w') as fp:
json.dump(data, fp, indent=2, sort_keys=True)
self.report({'INFO'}, 'Exported %s' % self.filepath)
return {'FINISHED'}
classes = (
EXPORT_OTHER_OT_curve,
)
def menu_func_export(self, context):
self.layout.operator(EXPORT_OTHER_OT_curve.bl_idname, text="Curve (.curve)")
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
def unregister():
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
for cls in classes:
bpy.utils.unregister_class(cls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment