-
Notifications
You must be signed in to change notification settings - Fork 7
/
JAPatchExport.py
246 lines (195 loc) · 7.61 KB
/
JAPatchExport.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# python imports
import os
# blender imports
import bpy
import mathutils
# Patch format - the patch looks like this, where P is a normal point, C is a control point:
#
# p2 c1 p1
# c2 c5 c4
# p3 c3 p4
#
# double braces due to str.format()
patchFormat = """{{
patchDef2
{{
{shader}
( 3 3 0 0 0 )
(
( ( {p2[0]:.3f} {p2[1]:.3f} {p2[2]:.3f} 0 0 ) ( {c1[0]:.3f} {c1[1]:.3f} {c1[2]:.3f} 0 0 ) ( {p1[0]:.3f} {p1[1]:.3f} {p1[2]:.3f} 0 0 ) )
( ( {c2[0]:.3f} {c2[1]:.3f} {c2[2]:.3f} 0 0 ) ( {c5[0]:.3f} {c5[1]:.3f} {c5[2]:.3f} 0 0 ) ( {c4[0]:.3f} {c4[1]:.3f} {c4[2]:.3f} 0 0 ) )
( ( {p3[0]:.3f} {p3[1]:.3f} {p3[2]:.3f} 0 0 ) ( {c3[0]:.3f} {c3[1]:.3f} {c3[2]:.3f} 0 0 ) ( {p4[0]:.3f} {p4[1]:.3f} {p4[2]:.3f} 0 0 ) )
)
}}
}}
"""
def mean2(p1, p2):
return [(x[0] + x[1]) / 2 for x in zip(p1, p2)]
def mean4(p1, p2, p3, p4):
return mean2(mean2(p1, p2), mean2(p3, p4))
# Converts 3 or 4 vertice coordinates as [x, y, z] to a Patch Definition as in a .map file
def coordinatesToPatchDef(shader, p1, p2, p3, p4=None):
# Triangle?
if not p4:
p4 = p3
return patchFormat.format(
shader=shader,
# vertices
p1=p1,
p2=p2,
p3=p3,
p4=p4,
# control points
c1=mean2(p1, p2),
c2=mean2(p2, p3),
c3=mean2(p3, p4),
c4=mean2(p4, p1),
c5=mean4(p1, p2, p3, p4)
)
# same as above, but they're not nicely distributed
def coordinatesToPatchDefAlt(shader, p1, p2, p3, p4=None):
# Triangle?
if not p4:
p4 = p3
return patchFormat.format(
shader=shader,
# vertices
p1=p1,
p2=p2,
p3=p3,
p4=p4,
# control points
c1=p1,
c2=p3,
c3=p4,
c4=p4,
c5=p4
)
### The new operator ###
class patchmesh_export_operator(bpy.types.Operator):
# everything is scaled down by this factor
bl_idname = "export_scene.patchmesh_map"
bl_label = "Export Patch Mesh (.map)"
# gets set by the file select window - IBM (Internal Blender Magic) or whatever.
filepath = bpy.props.StringProperty(
name="File Path", description="File path used for the .map file", maxlen=1024, default="")
scale = bpy.props.FloatProperty(
name="Scale", description="Factor by which to scale the object up", min=1, default=1)
shader = bpy.props.StringProperty(
name="Shader", description="Shader to put on the patches", maxlen=64, default="system/physics_clip")
beautiful = bpy.props.BoolProperty(
name="Even distribution", description="Whether to evenly distribute the patch handles (if disabled, they'll be at the corners)", default=True)
def execute(self, context):
self.ExportStart(context)
return {'FINISHED'}
def invoke(self, context, event):
windowMan = context.window_manager
# sets self.properties.filename and runs self.execute()
windowMan.fileselect_add(self)
return {'RUNNING_MODAL'}
def ExportStart(self, context):
filename = self.properties.filepath
if os.path.exists(filename):
# TODO: Overwrite yes/no
pass
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
obj = bpy.context.active_object
if not obj:
self.report({"ERROR"}, "No active object!")
return
if obj.type != 'MESH':
self.report({"ERROR"}, "Active object is no mesh!")
return
scaleMat = mathutils.Matrix.Scale(self.scale, 4)
# if there are any non-quads/tris (n-gons), so we can warn in the end.
ngons = False
patchDefFunc = coordinatesToPatchDef
if not self.beautiful:
patchDefFunc = coordinatesToPatchDefAlt
mesh = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
try:
try:
file = open(filename, "w")
file.write("{\n\"classname\" \"worldspawn\"\n")
for face in mesh.tessfaces:
if len(face.vertices) in (3, 4):
coordinates = [scaleMat * obj.matrix_world *
mesh.vertices[x].co for x in face.vertices]
file.write(patchDefFunc(self.shader, *coordinates))
else:
ngons = True
file.write("}\n")
file.close()
except IOError:
self.report({"ERROR"}, "Couldn't create file!")
return
finally:
bpy.data.meshes.remove(mesh)
if ngons:
self.report({'WARNING'}, "Some N-Gons could not be exported!")
return
class Operator(bpy.types.Operator):
bl_idname = "export_scene.ja_patchmesh_map"
bl_label = "Export JA Patch Mesh (.map)"
# gets set by the file select window - IBM (Internal Blender Magic) or whatever.
filepath: bpy.props.StringProperty(
name="File Path", description="File path used for the .map file", maxlen=1024, default="") # type: ignore
scale: bpy.props.FloatProperty(
name="Scale", description="Factor by which to scale the object up", min=1, default=1) # type: ignore
shader: bpy.props.StringProperty(
name="Shader", description="Shader to put on the patches", maxlen=64, default="system/physics_clip") # type: ignore
beautiful: bpy.props.BoolProperty(
name="Even distribution", description="Whether to evenly distribute the patch handles (if disabled, they'll be at the corners)", default=True) # type: ignore
def execute(self, context):
self.ExportStart(context)
return {'FINISHED'}
def invoke(self, context, event):
windowMan = context.window_manager
# sets self.properties.filename and runs self.execute()
windowMan.fileselect_add(self)
return {'RUNNING_MODAL'}
def ExportStart(self, context):
filename = self.properties.filepath
if os.path.exists(filename):
# TODO: Overwrite yes/no
pass
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
obj = bpy.context.active_object
if not obj:
self.report({"ERROR"}, "No active object!")
return
if obj.type != 'MESH':
self.report({"ERROR"}, "Active object is no mesh!")
return
scaleMat = mathutils.Matrix.Scale(self.scale, 4)
# if there are any non-quads/tris (n-gons), so we can warn in the end.
ngons = False
patchDefFunc = coordinatesToPatchDef
if not self.beautiful:
patchDefFunc = coordinatesToPatchDefAlt
mesh = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
try:
try:
file = open(filename, "w")
file.write("{\n\"classname\" \"worldspawn\"\n")
for face in mesh.tessfaces:
if len(face.vertices) in (3, 4):
coordinates = [scaleMat * obj.matrix_world *
mesh.vertices[x].co for x in face.vertices]
file.write(patchDefFunc(self.shader, *coordinates))
else:
ngons = True
file.write("}\n")
file.close()
except IOError:
self.report({"ERROR"}, "Couldn't create file!")
return
finally:
bpy.data.meshes.remove(mesh)
if ngons:
self.report({'WARNING'}, "Some N-Gons could not be exported!")
return
def menu_func(self, context):
self.layout.operator(Operator.bl_idname, text="JA Patchmesh (.map)")