]> git.tdb.fi Git - libs/gl.git/commitdiff
Improve progress reporting in the Blender exporter
authorMikko Rasa <tdb@tdb.fi>
Sat, 4 May 2019 13:42:34 +0000 (16:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 4 May 2019 13:50:34 +0000 (16:50 +0300)
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
blender/io_mspgl/export_scene.py
blender/io_mspgl/mesh.py
blender/io_mspgl/util.py

index 6d90c8faf6d5f435ac4daec803e8a02cb0200b86..39619f8bc02dd7fb0ff7cf3ac9a98d50a57820ba 100644 (file)
@@ -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
index 84e6be3e6aa40605f07d80ceeb895895db8c82ea..d51fb4718a0fdf1afda48a9587bf670dcbe3732e 100644 (file)
@@ -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()
 
index a303db9983811b398bf21e2c6255bd2d9487d283..565fdc4929e3a9db1deaecb0ecf8efd3695e378a 100644 (file)
@@ -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 i<len(objs):
@@ -588,12 +610,17 @@ def create_mesh_from_object(context, obj, progress=None):
                else:
                        mesh = me
 
+       progress.set_task("Smoothing", 0.3, 0.6)
        mesh.prepare_smoothing(progress)
+       progress.set_task("Vertex groups", 0.6, 0.7)
        mesh.prepare_vertex_groups(obj)
+       progress.set_task("Preparing UVs", 0.7, 1.0)
        mesh.prepare_uv(obj, progress)
 
        mesh.drop_references()
        for m in bmeshes:
                bpy.data.meshes.remove(m)
 
+       progress.pop_task()
+
        return mesh
index 850f1ddbb71dd754005d80a1e568fb01dc9f9819..3b5e8cd56f3349d6ab1f094b8a25cde41015f558 100644 (file)
@@ -5,31 +5,46 @@ class Progress:
                self.delta = 1.0
                self.last = 0.0
                self.stack = []
-               self.window_manager = context.window_manager
-               self.window_manager.progress_begin(0.0, 1.0)
+               if context:
+                       self.window_manager = context.window_manager
+                       self.window_manager.progress_begin(0.0, 1.0)
+               else:
+                       self.window_manager = None
 
        def push_task(self, task, low, high):
-               self.stack.append((task, low, high-low))
-               self.set_task("init", 0.0, 0.0)
+               self.stack.append((self.task, self.start, self.delta))
+               self.set_task(task, low, high)
+
+       def push_task_slice(self, task, index, count):
+               self.push_task(task, index/count, (index+1)/count)
 
        def pop_task(self):
-               self.set_task("finish", 1.0, 1.0)
-               self.stack.pop()
+               if not self.stack:
+                       return
+
+               self.set_progress(1.0)
+               self.task, self.start, self.delta = self.stack.pop()
 
        def set_task(self, task, low, high):
                if self.stack:
-                       task = self.stack[-1][0]+": "+task
-                       low = self.stack[-1][1]+self.stack[-1][2]*low
-                       high = self.stack[-1][1]+self.stack[-1][2]*high
+                       outer = self.stack[-1]
+                       if outer[0]:
+                               task = "{}: {}".format(outer[0], task)
+                       low = outer[1]+low*outer[2]
+                       high = outer[1]+high*outer[2]
 
                self.task = task
                self.start = low
                self.delta = high-low
 
-               self.set_progress(0)
+               self.set_progress(0.0)
+
+       def set_task_slice(self, task, index, count):
+               self.set_task(task, index/count, (index+1)/count)
 
        def set_progress(self, value):
                value = self.start+self.delta*value
                if value>self.last+0.01:
-                       self.window_manager.progress_update(value)
+                       if self.window_manager:
+                               self.window_manager.progress_update(value)
                        self.last = value