From 77b358049bc1ef38ea013175a485cecd71a173c9 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 20 Oct 2021 14:59:42 +0300 Subject: [PATCH] Refactor some parts of the Blender exporter to improve reusability --- blender/io_mspgl/export_object.py | 16 ++----------- blender/io_mspgl/export_scene.py | 39 ++++++++++++++++++++----------- blender/io_mspgl/util.py | 13 +++++++++++ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/blender/io_mspgl/export_object.py b/blender/io_mspgl/export_object.py index a8f55e92..b39307c8 100644 --- a/blender/io_mspgl/export_object.py +++ b/blender/io_mspgl/export_object.py @@ -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 diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index 8666e98b..00f91c1a 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -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) diff --git a/blender/io_mspgl/util.py b/blender/io_mspgl/util.py index e77e5447..4141e9f7 100644 --- a/blender/io_mspgl/util.py +++ b/blender/io_mspgl/util.py @@ -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 -- 2.45.2