From dc2e08621ad586663e4c11475f03886a91463907 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 4 May 2019 16:42:34 +0300 Subject: [PATCH] Improve progress reporting in the Blender exporter Pushing and popping tasks now makes more sense. The Progress object always exists but it's possible to suppress reporting (see 9b57666) by giving it a falsy context. Progress reporting has been added to a few places that previously lacked it. --- blender/io_mspgl/export_mesh.py | 21 ++++-------- blender/io_mspgl/export_scene.py | 8 +++-- blender/io_mspgl/mesh.py | 55 ++++++++++++++++++++++++-------- blender/io_mspgl/util.py | 37 ++++++++++++++------- 4 files changed, 79 insertions(+), 42 deletions(-) diff --git a/blender/io_mspgl/export_mesh.py b/blender/io_mspgl/export_mesh.py index 6d90c8fa..39619f8b 100644 --- a/blender/io_mspgl/export_mesh.py +++ b/blender/io_mspgl/export_mesh.py @@ -130,8 +130,7 @@ class MeshExporter: cache.fetch_strip(strip) faces_done += len(island) - if progress: - progress.set_progress(float(faces_done)/len(mesh.faces)) + progress.set_progress(faces_done/len(mesh.faces)) # Collect any faces that weren't used in strips loose += [f for f in island if not f.flag] @@ -195,24 +194,19 @@ class MeshExporter: from .mesh import create_mesh_from_object from .util import Progress - if self.show_progress: - if not progress: - progress = Progress(context) - progress.set_task("Preparing", 0.0, 0.0) - else: - progress = None + if not progress: + progress = Progress(self.show_progress and context) + progress.push_task("", 0.0, 0.65) mesh = create_mesh_from_object(context, obj, progress) strips = [] loose = mesh.faces if self.use_strips: - if progress: - progress.set_task("Creating strips", 0.65, 0.95) + progress.set_task("Creating strips", 0.65, 0.95) strips, loose = self.stripify(mesh, progress) - if progress: - progress.set_task("Writing file", 0.95, 1.0) + progress.set_task("Writing file", 0.95, 1.0) from .outfile import open_output out_file = open_output(out_file) @@ -294,7 +288,6 @@ class MeshExporter: if mesh.winding_test: out_file.write("winding", "COUNTERCLOCKWISE") - if progress: - progress.set_task("Done", 1.0, 1.0) + progress.pop_task() return mesh diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index 84e6be3e..d51fb471 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -4,6 +4,7 @@ import os class SceneExporter: def __init__(self): self.resource_collection = True + self.show_progress = True def export(self, context, out_file): objs = context.selected_objects @@ -43,14 +44,15 @@ class SceneExporter: object_prototypes[c.name] = o from .util import Progress - progress = Progress(context) + progress = Progress(self.show_progress and context) + if self.resource_collection: res_out = open_output(os.path.join(path, base+"_resources.mdc")) # TODO Export techniques as separate items in the collection for i, o in enumerate(unique_objects): res_out.begin("object", '"{}.object"'.format(o.name)) - progress.push_task(o.name, i/len(objs), (i+1)/len(objs)) + progress.push_task_slice(o.name, i, len(objs)) object_export.export(context, res_out, o, progress) progress.pop_task() res_out.end() @@ -62,7 +64,7 @@ class SceneExporter: os.makedirs(res_dir) for i, o in enumerate(unique_objects): obj_out = open_output(os.path.join(res_dir, o.name+".object")) - progress.push_task(o.name, i/len(objs), (i+1)/len(objs)) + progress.push_task_slice(o.name, i, len(objs)) object_export.export(context, obj_out, o, progress) progress.pop_task() diff --git a/blender/io_mspgl/mesh.py b/blender/io_mspgl/mesh.py index a303db99..565fdc49 100644 --- a/blender/io_mspgl/mesh.py +++ b/blender/io_mspgl/mesh.py @@ -266,7 +266,7 @@ class Mesh: self.lines += other.lines - def prepare_smoothing(self, progress=None): + def prepare_smoothing(self, progress): smooth_limit = -1 if self.smoothing=='NONE': for f in self.faces: @@ -279,10 +279,14 @@ class Mesh: for e in self.edges: e.check_smooth(smooth_limit) + progress.push_task("Sharp edges", 0.0, 0.7) self.split_vertices(self.find_smooth_group, progress) if self.smoothing!='BLENDER': - self.compute_normals() + progress.set_task("Updating normals", 0.7, 1.0) + self.compute_normals(progress) + + progress.pop_task() def prepare_vertex_groups(self, obj): for v in self.vertices: @@ -305,7 +309,7 @@ class Mesh: for g in v.groups: g.group = group_index_map[g.group] - def prepare_uv(self, obj, progress=None): + def prepare_uv(self, obj, progress): if obj.material_tex and self.use_uv!='NONE': layer = UvLayer("material_tex") @@ -341,16 +345,30 @@ class Mesh: for u in self.uv_layers: f.uvs.append([u.uvs[i] for i in f.loop_indices]) + prog_count = len(self.uv_layers) + prog_step = 0 + tbn_layer_index = -1 if self.tbn_vecs: uv_names = [u.name for u in self.uv_layers] if self.tbn_uvtex in uv_names: + prog_count += 1 tbn_layer_index = uv_names.index(self.tbn_uvtex) - self.compute_tbn(tbn_layer_index) + progress.push_task_slice("Computing TBN", 0, prog_count) + self.compute_tbn(tbn_layer_index, progress) + progress.set_task_slice(self.tbn_uvtex, 1, prog_count) self.split_vertices(self.find_uv_group, progress, tbn_layer_index) + progress.pop_task() + prog_step = 2 - for i in range(len(self.uv_layers)): + for i, u in enumerate(self.uv_layers): + if i==tbn_layer_index: + continue + + progress.push_task_slice(u.name, prog_step, prog_count) self.split_vertices(self.find_uv_group, progress, i) + progress.pop_task() + prog_step += 1 for v in self.vertices: if v.faces: @@ -374,8 +392,7 @@ class Mesh: groups.append(vg) - if progress: - progress.set_progress(i*0.5/len(self.vertices)) + progress.set_progress(i*0.5/len(self.vertices)) for i in range(len(self.vertices)): for g in groups[i][1:]: @@ -416,8 +433,7 @@ class Mesh: f.vertices[f.vertices.index(self.vertices[i])] = v v.faces.append(f) - if progress: - progress.set_progress(0.5+i*0.5/len(self.vertices)) + progress.set_progress(0.5+i*0.5/len(self.vertices)) def find_smooth_group(self, vertex, face): face.flag = True @@ -450,8 +466,8 @@ class Mesh: return group - def compute_normals(self): - for v in self.vertices: + def compute_normals(self, progress): + for i, v in enumerate(self.vertices): v.normal = mathutils.Vector() for f in v.faces: fv = f.pivot_vertices(v) @@ -465,10 +481,12 @@ class Mesh: else: v.normal = mathutils.Vector((0, 0, 1)) - def compute_tbn(self, index): + progress.set_progress(i/len(self.vertices)) + + def compute_tbn(self, index, progress): layer_uvs = self.uv_layers[index].uvs - for v in self.vertices: + for i, v in enumerate(self.vertices): v.tan = mathutils.Vector() v.bino = mathutils.Vector() for f in v.faces: @@ -493,6 +511,8 @@ class Mesh: if v.bino.length: v.bino.normalize() + progress.set_progress(i/len(self.vertices)) + def drop_references(self): for v in self.vertices: v._mvert = None @@ -551,10 +571,12 @@ class Mesh: return result -def create_mesh_from_object(context, obj, progress=None): +def create_mesh_from_object(context, obj, progress): if obj.type!="MESH": raise Exception("Object is not a mesh") + progress.push_task("Preparing mesh", 0.0, 0.3) + objs = [(obj, mathutils.Matrix())] i = 0 while iself.last+0.01: - self.window_manager.progress_update(value) + if self.window_manager: + self.window_manager.progress_update(value) self.last = value -- 2.45.2