+import os
+
class Progress:
def __init__(self, context):
self.task = ""
self.start = 0.0
self.delta = 1.0
self.last = 0.0
- self.window_manager = context.window_manager
- self.window_manager.progress_begin(0.0, 1.0)
+ self.stack = []
+ 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((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):
+ 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:
+ 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 value>self.last+0.001:
+ if self.window_manager:
+ self.window_manager.progress_update(value)
self.last = value
+
+
+def linear_to_srgb(l):
+ if l<0.0031308:
+ return 12.92*l
+ else:
+ return 1.055*(l**(1/2.4))-0.055
+
+def get_colormap(srgb):
+ if srgb:
+ return linear_to_srgb
+ else:
+ return lambda x: x
+
+def basename(path):
+ if path.startswith("//"):
+ path = path[2:]
+ return os.path.basename(path)
+
+def make_unique(values):
+ seen = set()
+ result = []
+ for i in values:
+ if i not in seen:
+ result.append(i)
+ seen.add(i)
+ return result
+
+def get_linked_node_and_socket(node_tree, socket):
+ for l in node_tree.links:
+ if socket==l.to_socket:
+ return (l.from_node, l.from_socket)
+ elif socket==l.from_socket:
+ return (l.to_node, l.to_socket)
+ return (None, None)
+
+def compute_bounding_sphere(points):
+ p1 = max(((p, p.length) for p in points), key=lambda x:x[1])[0]
+ p2 = max(((p, (p-p1).length) for p in points), key=lambda x:x[1])[0]
+ center = (p1+p2)/2
+ radius = (p1-p2).length/2
+ for p in points:
+ d = p-center
+ if d.length>radius:
+ center += d*(1-radius/d.length)/2
+ radius = (radius+d.length)/2
+
+ return center, radius