]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor some parts of the Blender exporter to improve reusability
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Oct 2021 11:59:42 +0000 (14:59 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Oct 2021 12:00:42 +0000 (15:00 +0300)
blender/io_mspgl/export_object.py
blender/io_mspgl/export_scene.py
blender/io_mspgl/util.py

index a8f55e92c34834a01ac0f17115b44b2d1343fd31..b39307c8704fd78a74864a4e012ddda990ca44c3 100644 (file)
@@ -2,19 +2,6 @@ import os
 import mathutils
 
 class ObjectExporter:
-       def compute_bounding_sphere(self, obj):
-               p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
-               p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
-               center = (p1+p2)/2
-               radius = (p1-p2).length/2
-               for v in obj.data.vertices:
-                       d = v.co-center
-                       if d.length>radius:
-                               center += d*(1-radius/d.length)/2
-                               radius = (radius+d.length)/2
-
-               return center, radius
-
        def collect_object_lods(self, obj):
                lods = [obj]
                lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
@@ -90,7 +77,8 @@ class ObjectExporter:
                obj_res = Resource(obj.name+".object", "object")
                statements = obj_res.statements
 
-               center, radius = self.compute_bounding_sphere(obj)
+               from .util import compute_bounding_sphere
+               center, radius = compute_bounding_sphere([v.co for v in obj.data.vertices])
                statements.append(Statement("bounding_sphere_hint", *center, radius))
 
                prev_mesh = None
index 8666e98beee0ef9489557d28da89fdcf788ee036..00f91c1a4afcd6c5e5687066bc397c98baea628f 100644 (file)
@@ -122,10 +122,7 @@ class SceneExporter:
                if scene.use_hdr:
                        seq_res.statements.append(Statement("hdr", True))
 
-               ss = Statement("clear")
-               ss.sub.append(Statement("color", 0.0, 0.0, 0.0, 0.0))
-               ss.sub.append(Statement("depth", 1.0))
-               seq_res.statements.append(ss)
+               self.add_clear(seq_res.statements, (0.0, 0.0, 0.0, 0.0), 1.0)
 
                scene_res = resources[scene.name+".scene"]
                seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
@@ -142,17 +139,13 @@ class SceneExporter:
                                any_blended = True
                        s = s.background_set
 
-               if any_opaque:
-                       ss = Statement("step", "", "content")
-                       ss.sub.append(Statement("depth_test", Token("LEQUAL")))
-                       ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
-                       seq_res.statements.append(ss)
 
+               main_tags = []
+               if any_opaque:
+                       main_tags.append("")
                if any_blended:
-                       ss = Statement("step", "blended", "content")
-                       ss.sub.append(Statement("depth_test", Token("LEQUAL")))
-                       ss.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
-                       seq_res.statements.append(ss)
+
+               self.add_content_steps(seq_res, "content", lighting_res, main_tags)
 
                if scene.use_ao:
                        ss = Statement("postprocessor")
@@ -180,3 +173,23 @@ class SceneExporter:
                        seq_res.statements.append(ss)
 
                return seq_res
+
+       def add_clear(self, statements, color, depth):
+               from .datafile import Statement
+
+               st = Statement("clear")
+               if color is not None:
+                       st.sub.append(Statement("color", *color))
+               if depth is not None:
+                       st.sub.append(Statement("depth", depth))
+               statements.append(st)
+
+       def add_content_steps(self, seq_res, renderable, lighting, tags):
+               from .datafile import Statement, Token
+
+               for t in tags:
+                       st = Statement("step", t, renderable)
+                       st.sub.append(Statement("depth_test", Token("LEQUAL")))
+                       if lighting:
+                               st.sub.append(seq_res.create_reference_statement("lighting", lighting))
+                       seq_res.statements.append(st)
index e77e544751abe7b9bcdbfe8ad968ec451b86b2ca..4141e9f77600891eb7ef405ecf2cc6bb3d642944 100644 (file)
@@ -85,3 +85,16 @@ def get_linked_node_and_socket(node_tree, 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