Timing info added to the progress report system.
authorLukas Tönne <lukas.toenne@gmail.com>
Mon, 15 Dec 2014 13:58:00 +0000 (14:58 +0100)
committerLukas Tönne <lukas.toenne@gmail.com>
Mon, 15 Dec 2014 13:58:00 +0000 (14:58 +0100)
object_physics_meadow/patch.py
object_physics_meadow/util.py

index 888c70d..1270a18 100644 (file)
@@ -253,34 +253,10 @@ def bake_all(context, progress_reporter):
     total = count_bakeable(context)
     
     with progress_reporter("Bake Blob", 0, total):
-        """
-        num = 0
-        """
         for ob in patch_objects(context):
             for psys in ob.particle_systems:
-                """
-                sys.stdout.write("Baking blob {}/{} ... ".format(str(num).rjust(5), str(total).ljust(5)))
-                sys.stdout.flush()
-                
-                start_time = time.time()
-                """
-                
                 progress_add(1)
                 bake_psys(context, ob, psys)
-                
-                """
-                duration = time.time() - start_time
-                total_time += duration
-                avg_time = total_time / float(num + 1)
-                
-                time_string = lambda x: time.strftime("%H:%M:%S", time.gmtime(x)) + ".%02d" % (int(x * 100.0) % 100)
-                durstr = time_string(duration)
-                avgstr = time_string(avg_time) if avg_time > 0.0 else "--:--:--"
-                etastr = time_string(avg_time * (total - num)) if avg_time > 0.0 else "--:--:--"
-                sys.stdout.write("{} (avg. {}, ETA {})\n".format(durstr, avgstr, etastr))
-                sys.stdout.flush()
-                num += 1
-                """
 
 def patch_objects_rebake(context, progress_reporter=DummyProgressContext):
     settings = _settings.get(context)
index b709b65..5f2127a 100644 (file)
@@ -99,6 +99,12 @@ def set_object_parent(ob, parent):
 
 _progress_context = None
 
+def get_time_string(x):
+    if x >= 0.0:
+        return time.strftime("%H:%M:%S", time.gmtime(x)) + ".%02d" % (int(x * 100.0) % 100)
+    else:
+        return "??:??:??.??"
+
 def make_progress_reporter(show_progress_bar=True, show_stdout=False):
 
     # internal class returned by the function, bound to output args
@@ -113,12 +119,17 @@ def make_progress_reporter(show_progress_bar=True, show_stdout=False):
             self.pcur = pmin
             self.perc_show = -2.0 # last displayed percentage, init to make sure we show the first time
 
+            self.duration = 0.0
+            self.start_time = 0.0
+
         def __enter__(self):
             global _progress_context
 
             assert(_progress_context is None)
             _progress_context = self
             
+            self.start_time = time.time()
+
             if show_progress_bar:
                 wm = bpy.context.window_manager
                 # always use 0..100 percentage on the progress counter,
@@ -133,6 +144,11 @@ def make_progress_reporter(show_progress_bar=True, show_stdout=False):
                 wm.progress_end()
 
             if show_stdout:
+                # make a final report
+                done = self.pcur - self.pmin
+                sys.stdout.write("\r>> {}: {}/{}, {}".format(self.name,
+                                                                    str(done).rjust(len(str(self.tot))), str(self.tot),
+                                                                    get_time_string(self.duration)))
                 # clean newline
                 sys.stdout.write("\n")
                 sys.stdout.flush()
@@ -140,6 +156,13 @@ def make_progress_reporter(show_progress_bar=True, show_stdout=False):
             assert(_progress_context is self)
             _progress_context = None
 
+        def estimate_total_duration(self):
+            done = self.pcur - self.pmin
+            if done > 0:
+                return self.duration * self.tot / done
+            else:
+                return -1.0
+
         def set_progress(self, value, message):
             self.pcur = value
             done = value - self.pmin
@@ -149,14 +172,25 @@ def make_progress_reporter(show_progress_bar=True, show_stdout=False):
             # avoids overhead for very frequent updates
             if perc > self.perc_show + 1.0:
                 self.perc_show = perc
-                perc = min(max(int(perc), 0), 100)
+                perc = min(max(perc, 0), 100)
+
+                self.duration = time.time() - self.start_time
 
                 if show_progress_bar:
                     wm = bpy.context.window_manager
                     wm.progress_update(perc)
 
                 if show_stdout:
-                    sys.stdout.write("\r>> {}: {}/{} [{}{}] {}".format(self.name, str(done).rjust(len(str(self.tot))), str(self.tot), '.' * perc, ' ' * (100 - perc), message))
+                    bar = 50
+                    filled = int(bar * done * self.norm)
+
+                    eta = self.estimate_total_duration()
+
+                    sys.stdout.write("\r>> {}: {}/{} [{}{}] {}/{} | {}".format(self.name,
+                                                                               str(done).rjust(len(str(self.tot))), str(self.tot),
+                                                                               '.' * filled, ' ' * (bar - filled),
+                                                                               get_time_string(self.duration), get_time_string(eta),
+                                                                               message))
                     sys.stdout.flush()
 
     return ProgressContext