Skip to content

Commit bc59de8

Browse files
committed
initial implementation of multi-cell structure
1 parent ab3b837 commit bc59de8

1 file changed

Lines changed: 35 additions & 65 deletions

File tree

simloader/importer.py

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,84 +7,55 @@
77
from mathutils import Matrix
88
import mzd
99

10-
11-
def create_face_data(cells):
12-
# TODO, extend this to 3d mesh
13-
if len(cells) > 1:
14-
show_message_box("Multi Structure mesh unsupported yet, use first cell only")
15-
16-
return cells[0].type, cells[0].data
17-
10+
supported_mesh_format = [ 'triangle', 'quad']
1811

1912
def update_mesh(meshio_mesh, object):
20-
# extrace information from the meshio mesh
13+
# extract information from the meshio mesh
2114
mesh = object.data
2215
mesh_vertices = meshio_mesh.points
23-
type, mesh_faces = create_face_data(meshio_mesh.cells)
24-
25-
# if is_pointcloud, can speed up a little bit, for later operations
26-
is_pointcloud = None
27-
is_sinlgecell = len(meshio_mesh.cells) == 1
28-
29-
30-
if type == "triangle" or type == "quad":
31-
is_pointcloud = False
32-
elif type == "vertex":
33-
is_pointcloud = True
34-
else:
35-
# if unknown, then show as point cloud only
36-
is_pointcloud = True
37-
show_message_box("unsupported mesh yet , will use point cloud to show vertices only")
3816

39-
face_shape = mesh_faces.shape
17+
n_poly = 0
18+
n_loop = 0
4019
n_verts = len(mesh_vertices)
41-
npoly = face_shape[1]
42-
n_primitives = face_shape[0]
4320

44-
# extrace information from the blender mesh
21+
faces_loop_start = np.array([], dtype=np.int32)
22+
faces_loop_total = np.array([], dtype=np.int32)
23+
loops_vert_idx = np.array([], dtype=np.int32)
4524
shade_scheme = False
4625
if mesh.polygons:
4726
shade_scheme = mesh.polygons[0].use_smooth
27+
for cell in meshio_mesh.cells:
28+
if cell.type not in supported_mesh_format:
29+
if cell.type!="vertex":
30+
show_message_box(cell.type + " is unsupported mesh format yet")
31+
continue
32+
data = cell.data
33+
n_poly += len(data)
34+
n_loop += data.shape[0] * data.shape[1]
35+
loops_vert_idx = np.append(loops_vert_idx, data.ravel())
36+
faces_loop_total = np.append(faces_loop_total, np.ones((len(data)), dtype=np.int32) * data.shape[1])
37+
if faces_loop_total.size>0:
38+
faces_loop_start = np.cumsum(faces_loop_total)
39+
# Add a zero as first entry
40+
faces_loop_start = np.roll(faces_loop_start, 1)
41+
faces_loop_start[0] = 0
4842

49-
if not is_pointcloud and \
50-
len(mesh.vertices) == n_verts and \
51-
len(mesh.polygons) == n_primitives and \
52-
len(mesh.loops) == npoly * n_primitives:
53-
# the strucutre doesn't change, no need to add or remove vertices/ edges/ polygons, then directly go to next step
54-
# In theory, it could have a bug here, because it doesn't check the number of edges, but it's too hard to do that,
55-
# because edge data is not stored in files, it has to be calculated from mesh_face manually
56-
# So the problem is, if existing mesh has more edges than the next mesh, then these extra edges won't be removed,
57-
# It won't effect the rendered image, because edges won't be rendered
58-
# but it will look ugly in viewport, especially go into edit mode
59-
# this can happen only in a very rare case.
60-
pass
61-
elif is_pointcloud and len(mesh.vertices) == n_verts and len(mesh.polygons) ==0:
62-
# len(mesh.polygons)==0, to make sure it was pointcloud as well in the previous frame
43+
44+
if len(mesh.vertices) == n_verts and \
45+
len(mesh.polygons) == n_poly and \
46+
len(mesh.loops) == n_loop:
6347
pass
6448
else:
65-
# since the structure has been changed, so delete it first, then create a new one
66-
# and reconstruct some other attributes here(if there are), e.g. uv maps, etc.
6749
mesh.clear_geometry()
6850
mesh.vertices.add(n_verts)
69-
mesh.loops.add(npoly * n_primitives)
70-
mesh.polygons.add(n_primitives)
51+
mesh.loops.add(n_loop)
52+
mesh.polygons.add(n_poly)
7153

7254
mesh.vertices.foreach_set("co", mesh_vertices.ravel())
73-
74-
if not is_pointcloud:
75-
loops_vert_idx = mesh_faces.ravel()
76-
77-
faces_loop_total = np.ones((len(mesh_faces)), dtype=np.int32) * npoly
78-
79-
faces_loop_start = np.cumsum(faces_loop_total)
80-
# Add a zero as first entry
81-
faces_loop_start = np.roll(faces_loop_start, 1)
82-
faces_loop_start[0] = 0
83-
84-
mesh.loops.foreach_set("vertex_index", loops_vert_idx)
85-
mesh.polygons.foreach_set("loop_start", faces_loop_start)
86-
mesh.polygons.foreach_set("loop_total", faces_loop_total)
87-
mesh.polygons.foreach_set("use_smooth", [shade_scheme] * len(faces_loop_total))
55+
mesh.loops.foreach_set("vertex_index", loops_vert_idx)
56+
mesh.polygons.foreach_set("loop_start", faces_loop_start)
57+
mesh.polygons.foreach_set("loop_total", faces_loop_total)
58+
mesh.polygons.foreach_set("use_smooth", [shade_scheme] * len(faces_loop_total))
8859

8960
mesh.update()
9061
mesh.validate()
@@ -123,8 +94,7 @@ def update_mesh(meshio_mesh, object):
12394
attribute.data.foreach_set(name_string, v.ravel())
12495

12596

126-
def create_obj(fileseq, use_relaitve, transform_matrix=Matrix([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0,
127-
1]])):
97+
def create_obj(fileseq, use_relaitve, transform_matrix=Matrix([[1, 0, 0, 0], [0, 0, -1, 0], [0, 1, 0, 0], [0, 0, 0, 1]])):
12898

12999
current_frame = bpy.context.scene.frame_current
130100
filepath = fileseq[current_frame % len(fileseq)]
@@ -136,8 +106,8 @@ def create_obj(fileseq, use_relaitve, transform_matrix=Matrix([[1, 0, 0, 0], [0
136106
meshio_mesh = meshio.read(filepath)
137107
except Exception as e:
138108
show_message_box("Error when reading: " + filepath + ",\n" + traceback.format_exc(),
139-
"Meshio Loading Error" + str(e),
140-
icon="ERROR")
109+
"Meshio Loading Error" + str(e),
110+
icon="ERROR")
141111
enabled = False
142112

143113
# create the object

0 commit comments

Comments
 (0)