forked from qerrant/BezierBlenderToUE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportBezierToGodot.py
102 lines (86 loc) · 4.6 KB
/
ExportBezierToGodot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Blender Plugin
# Later on, switch from CSV to a TRES File
bl_info = {
"name": "Export BezierCSV for Godot",
"blender": (2, 80, 0),
"author": "Elliot Page, Alex Z.",
"location": "File > Export > BezierCSV For Godot (.tres)",
"category": "Import-Export",
}
import sys, getopt
import os
import bpy
from bpy_extras.io_utils import ImportHelper
class ObjectExportPoints(bpy.types.Operator, ImportHelper):
bl_idname = "me.export_bezier_points"
bl_label = "Export BezierCSV to Godot"
bl_options = {'REGISTER'}
def execute(self, context):
obj = bpy.context.active_object
objType = bpy.context.object.type
if objType == 'CURVE':
beziers = []
for subcurve in obj.data.splines:
if subcurve.type == 'BEZIER':
beziers.append(subcurve)
#DEBUG
print(beziers)
# Actual loop
if len(beziers) > 0:
count = 1
saveFile = open(self.filepath + ".tres", "w")
saveFile.write("[gd_resource type=\"Curve3D\" format=3]\n\n[resource]\n_data = {\n\"points\": PackedVector3Array(") # Writes front matter and opens the array
string = '%f,%f,%f,%f,%f,%f,%f,%f,%f' # Each point on the curve is a series of 9 floats
for bezier in beziers:
pointtotal = len(bezier.bezier_points)
for point in bezier.bezier_points:
# Calculate relative positions of the in/out handles, Blender provides these as global locations, Godot wants relative positions.
# also, Godot uses Y for "up" instead of Z, so lets swap these
inx = point.handle_left.x - point.co.x
iny = point.handle_left.z - point.co.z
inz = point.handle_left.y - point.co.y
oux = point.handle_right.x - point.co.x
ouy = point.handle_right.z - point.co.z
ouz = point.handle_right.y - point.co.y
if count != 1: saveFile.write(", "); # Add a comma if not the first point so the list is concatenated properly.
# If this is the first point in the curve, the "in" vector must be zeroed out.
if count == 1:
line = string % (0.0, 0.0, 0.0, oux, ouy, ouz, point.co.x, point.co.z, point.co.y)
# If this is the last point in the curve, the "out" vector must be zeroed out
elif count == pointtotal:
line = string % (inx, iny, inz, 0.0, 0.0, 0.0, point.co.x, point.co.z, point.co.y)
# All the rest are "Main sequence" points, with both "In" and "Out" vectors
else:
line = string % (inx, iny, inz, oux, ouy, ouz, point.co.x, point.co.z, point.co.y)
# write the point to the list
saveFile.write(line)
count = count + 1
# Loop complete, now add remaining remaining items
saveFile.write("),\n\"tilts\": PackedFloat32Array(")
# Loop for tilts here, one per point/count
tilt = 1
while tilt <= pointtotal: # This initially used "count" which was 1 point too many.
saveFile.write("0")
if tilt != pointtotal: saveFile.write(", ") # add seperator, unless its the final entry.
tilt = tilt + 1
saveFile.write(")\n}\npoint_count = " + str(pointtotal)) # This initially used "count" which was 1 point too many.
# Generation complete, close file
saveFile.close()
self.report({"INFO"}, "The curve was exported")
return {'FINISHED'}
else:
self.report({"WARNING"}, "Selected object isn't a Bezier curve")
return {'CANCELLED'}
else:
self.report({"WARNING"}, "Selected object isn't a curve")
return {'CANCELLED'}
def menu_func(self, context):
self.layout.operator(ObjectExportPoints.bl_idname,text="Export BezierCSV For Godot (.tres)")
def register():
bpy.utils.register_class(ObjectExportPoints)
bpy.types.TOPBAR_MT_file_export.append(menu_func)
def unregister():
bpy.utils.unregister_class(ObjectExportPoints)
bpy.types.TOPBAR_MT_file_export.remove(menu_func)
if __name__ == "__main__":
register()