-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMeshPort.py
More file actions
382 lines (305 loc) · 15 KB
/
MeshPort.py
File metadata and controls
382 lines (305 loc) · 15 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# --------------------------
# Mesh Porter
# 12.09.2007
# --------------------------
# Richard Gerum (Randrian)
# --------------------------
# API Upgrade 11.02.2022
# Robin Hohnsbeen (Ryou)
import bpy
import os.path
import glob
import os
from bpy import *
from enum import Enum
from pathlib import Path
from . import MetaData
def ReadFloatList(string):
x = 0
y = 0
list = []
for c in string:
if c == " " or c == "\n":
list.append(float(string[y:x]))
y = x
x += 1
if y == x+1:
list.append(float(string[y:x]))
return list
def ReadIntList(string):
x = 0
y = 0
list = []
for c in string:
if c == " " or c == "\n":
if string[y:x] != " ":
list.append(int(string[y:x]))
y = x
x += 1
if y == x+1:
list.append(int(string[y:x]))
return list
class mesh_import_state(Enum):
EMPTY = 0
VERTICES = 1
FACES = 2
VGROUPS = 3
FACEMATS = 4
MATERIALS = 5
UVCOORDS = 6
TEXTURES = 7
OBJECT = 8
MODIFIERS = 9
def get_mesh_import_state(line):
key = line[0:-1]
if key == "[Object]":
return mesh_import_state.OBJECT, "Loading Object"
elif key == "[Vertices]":
return mesh_import_state.VERTICES, "Loading Vertices"
elif key == "[Faces]":
return mesh_import_state.FACES, "Loading Faces"
elif key == "[VGroups]":
return mesh_import_state.VGROUPS, "Loading Vertex Groups"
elif key == "[UVCoords]":
return mesh_import_state.UVCOORDS, "Assigning UV Coordinates"
elif key == "[Material]":
return mesh_import_state.MATERIALS, "Loading Materials"
elif key == "[FaceMats]":
return mesh_import_state.FACEMATS, "Linking Faces to Materials"
elif key == "[Texture]":
return mesh_import_state.TEXTURES, "Loading Textures"
return mesh_import_state.EMPTY, "Undefined"
# war elif # TODO:
if line[0:-1] == "[Modifier]":
loadtext = "Loading Modifiers"
mode = 9
else:
mode = 0
def GetParameters(line):
name_and_values = line.split("=")
return name_and_values[0], name_and_values[1].split(",")
def lock_object(object, is_locked=True):
if object is None:
return
object.lock_location = [is_locked, is_locked, is_locked]
if len(object.lock_rotation) == 3:
object.lock_rotation = [is_locked, is_locked, is_locked]
else:
object.lock_rotation = [is_locked, is_locked, is_locked, is_locked]
object.lock_scale = [is_locked, is_locked, is_locked]
def import_mesh(path, insert_collection=None, reuse_materials=True):
meshpath = Path(path)
print('Importing "' + path + '"')
filename = meshpath.stem
if os.path.exists(path) == False:
raise FileNotFoundError("No valid mesh file at: " + path)
if ".meshblend" in path or ".mesh.blend" in path:
with bpy.data.libraries.load(path) as (data_from, data_to):
data_to.objects = data_from.objects
for new_object in data_to.objects:
lock_object(new_object, True)
# Default: Add to scene collection
if insert_collection == None:
bpy.context.view_layer.layer_collection.collection.objects.link(
new_object)
else:
insert_collection.objects.link(new_object)
if reuse_materials:
MetaData.replace_duplicate_materials(data_to.objects)
return data_to.objects # Return all objects and filter later
# Legacy import method for .mesh files.
new_object = None
file = None
try:
file = open(path, "r", encoding="ISO-8859-1") # Loading a .mesh file
tex = 0
mode = 0
lines = file.readlines()
verts = []
faces = []
new_mesh = bpy.data.meshes.new(filename + "_mesh")
new_object = bpy.data.objects.new(filename, new_mesh)
# This is used to measure at what UV index we are. It depends on the number of vertices per face we checked.
faceloop_index = 0
current_mat_name = ""
for line in lines:
if line[0] == "[":
last_mode = mode
mode, loadtext = get_mesh_import_state(line)
if last_mode == mesh_import_state.FACES and mode != mesh_import_state.FACES:
new_mesh.from_pydata(verts, [], faces)
new_object.data.uv_layers.new()
else:
if mode == mesh_import_state.OBJECT:
param_name, values = GetParameters(line)
if param_name == "Loc":
new_object.location = [
float(values[0]), float(values[1]), float(values[2])]
if param_name == "Rot":
if len(values) == 3:
new_object.rotation_euler = [
float(values[0]), float(values[1]), float(values[2])]
elif len(values) == 4:
new_object.rotation_quaternion = [float(values[0]), float(
values[1]), float(values[2]), float(values[4])]
if param_name == "Size":
new_object.scale = [float(values[0]), float(
values[1]), float(values[2])]
if param_name == "Mode":
# mesh.mode = int(values[0])
# Unfortunately, I don't know what this is used for. It could refer to Object Modes, but we don't really have to store/load them.
pass
if mode == mesh_import_state.VERTICES:
verts.append(ReadFloatList(line))
if mode == mesh_import_state.FACES:
faces.append(ReadIntList(line))
if mode == mesh_import_state.VGROUPS:
if line != "\n":
if line[-2] == ":":
group_name = line[0:-2]
mapping = MetaData.get_vgroup_mapping(group_name)
if mapping:
group_name = mapping
new_object.vertex_groups.new(name=group_name)
else:
new_object.vertex_groups.active.add(
ReadIntList(line), 1.0, "REPLACE")
if mode == mesh_import_state.UVCOORDS:
coordinates = line.split("=")[1]
UVs = coordinates.split(",")
for index in range(0, len(UVs), 2):
new_mesh.uv_layers.active.data[faceloop_index].uv = [
float(UVs[index]), float(UVs[index+1])]
faceloop_index += 1
if mode == mesh_import_state.MATERIALS:
param_name, values = GetParameters(line)
if param_name == "Name":
current_mat_name = values[0][0:-1]
mat: bpy.types.Material = None
if bpy.data.materials.find(current_mat_name) > -1 and reuse_materials:
mat = bpy.data.materials[current_mat_name]
else:
mat = bpy.data.materials.new(name=current_mat_name)
mat.use_nodes = True
reuse_materials = False
current_mat_name = mat.name
new_object.data.materials.append(mat)
if reuse_materials:
# Just ignore the next lines about materials
mode = mesh_import_state.EMPTY
current_mat_name = ""
elif param_name == "Color":
bpy.data.materials.get(current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Base Color'].default_value = [
float(values[0]), float(values[1]), float(values[2]), 1.0]
elif param_name == "Ref":
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Roughness'].default_value = 1.0 - float(values[0])
elif param_name == "Spec":
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Specular'].default_value = float(values[0])
elif param_name == "Hardness":
# Legacy from Blender 2.7
# 'convert' to roughness which is somewhat the opposite.
hardness = float(int(values[0]))/100.0
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Roughness'].default_value = 1.0 - max(min(hardness, 1.0), 0.0)
# For new shader values
elif param_name == "Metallic":
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Metallic'].default_value = float(values[0])
elif param_name == "Roughness":
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Roughness'].default_value = float(values[0])
elif param_name == "Emission_Strength":
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Emission Strength'].default_value = float(values[0])
elif param_name == "Emission_Color":
bpy.data.materials.get(current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Emission'].default_value = [
float(values[0]), float(values[1]), float(values[2]), 1.0]
elif param_name == "Subsurface_Color":
bpy.data.materials.get(current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Subsurface Color'].default_value = [
float(values[0]), float(values[1]), float(values[2]), 1.0]
elif param_name == "Subsurface_Strength":
bpy.data.materials.get(
current_mat_name).node_tree.nodes['Principled BSDF'].inputs['Subsurface'].default_value = float(values[0])
if mode == mesh_import_state.TEXTURES:
if current_mat_name == "": # The last material was already present, so we ignore the texture as well.
mode = mesh_import_state.EMPTY
continue
param_name, values = GetParameters(line)
if param_name == "Name":
tex = bpy.data.textures.find(values[0])
if tex is None:
tex = bpy.data.textures.new(values[0])
else:
mode = mesh_import_state.EMPTY
if param_name == "Image":
current_material = bpy.data.materials.get(
current_mat_name)
current_material.use_nodes = True
node_tree = current_material.node_tree
texture_node = node_tree.nodes.new(
"ShaderNodeTexImage")
node_tree.links.new(
node_tree.nodes['Principled BSDF'].inputs['Base Color'], texture_node.outputs['Color'])
tex_file_name = values[0].split("/")
searching_path = os.path.join(os.path.dirname(
path), "**", tex_file_name[len(tex_file_name)-1].replace("\n", ""))
paths_to_image = glob.glob(searching_path)
if len(paths_to_image) > 0:
img = bpy.data.images.load(paths_to_image[0])
texture_node.image = img
else:
print("Texture " + tex_file_name[len(
tex_file_name)-1] + " was not found. Searched recursively for " + searching_path)
if param_name == "Type":
# Could be added, if needed.
# tex.setType(values[0])
pass
if param_name == "Texco":
# This is usually just UVCoordinates which is the default
# texco = values[0]
pass
if mode == mesh_import_state.FACEMATS:
mat_list = ReadIntList(line)
list_index = 0
for material_index in mat_list:
new_object.data.polygons[list_index].material_index = material_index
list_index += 1
# TODO: Load Modifiers
if mode == mesh_import_state.MODIFIERS:
param_name, values = GetParameters(line)
print("Loading modifiers is not supported yet. " +
str(values[0]))
continue
if ReadParameter(line, 0, 0) == "Type":
tex = ob.modifiers.append(ReadParameter(line, 1, 2))
if ReadParameter(line, 0, 0) == "Height":
tex[Modifier.Settings.HEIGHT] = ReadParameter(
line, 1, 1)
if len(new_object.vertex_groups) == 0 and MetaData.get_vgroup_mapping(new_object.name):
new_object.vertex_groups.new(
name=MetaData.get_vgroup_mapping(new_object.name))
new_object.vertex_groups.active.add(
range(len(new_object.data.vertices)), 1.0, "REPLACE")
# Default: Add to scene collection
if insert_collection == None:
bpy.context.view_layer.layer_collection.collection.objects.link(
new_object)
else:
insert_collection.objects.link(new_object)
# Select and make active
bpy.context.view_layer.objects.active = new_object
new_object.select_set(True)
bpy.ops.object.shade_smooth()
lock_object(new_object, True)
except BaseException as Err:
path = Path(path)
print("", Err)
messagetype = "ERROR"
message = f"While reading {path.name} mesh: {Err}"
print(message)
finally:
if file:
file.close()
return [new_object]