]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/util.py
Use low-level handles when computing descriptor set hashes
[libs/gl.git] / blender / io_mspgl / util.py
1 import os
2
3 class Progress:
4         def __init__(self, context):
5                 self.task = ""
6                 self.start = 0.0
7                 self.delta = 1.0
8                 self.last = 0.0
9                 self.stack = []
10                 if context:
11                         self.window_manager = context.window_manager
12                         self.window_manager.progress_begin(0.0, 1.0)
13                 else:
14                         self.window_manager = None
15
16         def push_task(self, task, low, high):
17                 self.stack.append((self.task, self.start, self.delta))
18                 self.set_task(task, low, high)
19
20         def push_task_slice(self, task, index, count):
21                 self.push_task(task, index/count, (index+1)/count)
22
23         def pop_task(self):
24                 if not self.stack:
25                         return
26
27                 self.set_progress(1.0)
28                 self.task, self.start, self.delta = self.stack.pop()
29
30         def set_task(self, task, low, high):
31                 if self.stack:
32                         outer = self.stack[-1]
33                         if outer[0]:
34                                 task = "{}: {}".format(outer[0], task)
35                         low = outer[1]+low*outer[2]
36                         high = outer[1]+high*outer[2]
37
38                 self.task = task
39                 self.start = low
40                 self.delta = high-low
41
42                 self.set_progress(0.0)
43
44         def set_task_slice(self, task, index, count):
45                 self.set_task(task, index/count, (index+1)/count)
46
47         def set_progress(self, value):
48                 value = self.start+self.delta*value
49                 if value>self.last+0.001:
50                         if self.window_manager:
51                                 self.window_manager.progress_update(value)
52                         self.last = value
53
54
55 def linear_to_srgb(l):
56         if l<0.0031308:
57                 return 12.92*l
58         else:
59                 return 1.055*(l**(1/2.4))-0.055
60
61 def get_colormap(srgb):
62         if srgb:
63                 return linear_to_srgb
64         else:
65                 return lambda x: x
66
67 def basename(path):
68         if path.startswith("//"):
69                 path = path[2:]
70         return os.path.basename(path)
71
72 def make_unique(values):
73         seen = set()
74         result = []
75         for i in values:
76                 if i not in seen:
77                         result.append(i)
78                         seen.add(i)
79         return result
80
81 def get_linked_node_and_socket(node_tree, socket):
82         for l in node_tree.links:
83                 if socket==l.to_socket:
84                         return (l.from_node, l.from_socket)
85                 elif socket==l.from_socket:
86                         return (l.to_node, l.to_socket)
87         return (None, None)
88
89 def compute_bounding_sphere(points):
90         p1 = max(((p, p.length) for p in points), key=lambda x:x[1])[0]
91         p2 = max(((p, (p-p1).length) for p in points), key=lambda x:x[1])[0]
92         center = (p1+p2)/2
93         radius = (p1-p2).length/2
94         for p in points:
95                 d = p-center
96                 if d.length>radius:
97                         center += d*(1-radius/d.length)/2
98                         radius = (radius+d.length)/2
99
100         return center, radius