From 3e90ac4727ab0bce452e5ac6d0584b59f64808be Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 18 Sep 2018 20:41:14 -0400 Subject: [PATCH] Fix lightmap preview operator devilry The "Preview Lightmap" operator no longer changes the active selected objects or the active layer selection. --- korman/helpers.py | 30 ++++++++++++++++++++++++++++++ korman/operators/op_export.py | 33 ++------------------------------- korman/operators/op_lightmap.py | 6 ++---- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/korman/helpers.py b/korman/helpers.py index fcd60c3..63e3105 100644 --- a/korman/helpers.py +++ b/korman/helpers.py @@ -48,6 +48,36 @@ class TemporaryObject: return getattr(self._obj, attr) +class UiHelper: + """This fun little helper makes sure that we don't wreck the UI""" + def __init__(self, context): + self.active_object = context.active_object + self.selected_objects = context.selected_objects + + def __enter__(self): + scene = bpy.context.scene + self.layers = tuple(scene.layers) + self.frame_num = scene.frame_current + scene.frame_set(scene.frame_start) + scene.update() + + # Some operators require there be an active_object even though they + # don't actually use it... + if scene.objects.active is None: + scene.objects.active = scene.objects[0] + return self + + def __exit__(self, type, value, traceback): + for i in bpy.data.objects: + i.select = (i in self.selected_objects) + + scene = bpy.context.scene + scene.objects.active = self.active_object + scene.layers = self.layers + scene.frame_set(self.frame_num) + scene.update() + + def ensure_power_of_two(value): return pow(2, math.floor(math.log(value, 2))) diff --git a/korman/operators/op_export.py b/korman/operators/op_export.py index 7134114..c31cfa5 100644 --- a/korman/operators/op_export.py +++ b/korman/operators/op_export.py @@ -20,6 +20,7 @@ from pathlib import Path import pstats from .. import exporter +from ..helpers import UiHelper from ..properties.prop_world import PlasmaAge, game_versions from ..korlib import ConsoleToggler @@ -131,7 +132,7 @@ class ExportOperator(bpy.types.Operator): # Separate blender operator and actual export logic for my sanity ageName = path.stem - with _UiHelper(context) as _ui: + with UiHelper(context) as _ui: e = exporter.Exporter(self) try: self.export_active = True @@ -181,36 +182,6 @@ class ExportOperator(bpy.types.Operator): setattr(PlasmaAge, name, prop(**age_options)) -class _UiHelper: - """This fun little helper makes sure that we don't wreck the UI""" - def __init__(self, context): - self.active_object = context.active_object - self.selected_objects = context.selected_objects - - def __enter__(self): - scene = bpy.context.scene - self.layers = tuple(scene.layers) - self.frame_num = scene.frame_current - scene.frame_set(scene.frame_start) - scene.update() - - # Some operators require there be an active_object even though they - # don't actually use it... - if scene.objects.active is None: - scene.objects.active = scene.objects[0] - return self - - def __exit__(self, type, value, traceback): - for i in bpy.data.objects: - i.select = (i in self.selected_objects) - - scene = bpy.context.scene - scene.objects.active = self.active_object - scene.layers = self.layers - scene.frame_set(self.frame_num) - scene.update() - - # Add the export operator to the Export menu :) def menu_cb(self, context): if context.scene.render.engine == "PLASMA_GAME": diff --git a/korman/operators/op_lightmap.py b/korman/operators/op_lightmap.py index bdbdd57..fc70d8e 100644 --- a/korman/operators/op_lightmap.py +++ b/korman/operators/op_lightmap.py @@ -17,7 +17,7 @@ import bpy from bpy.props import * from ..exporter.etlight import LightBaker -from ..helpers import GoodNeighbor +from ..helpers import UiHelper class _LightingOperator: @@ -38,9 +38,7 @@ class LightmapAutobakePreviewOperator(_LightingOperator, bpy.types.Operator): super().__init__() def execute(self, context): - with GoodNeighbor() as toggle: - toggle.track(context.scene, "layers", tuple(context.scene.layers)) - + with UiHelper(context): with LightBaker() as bake: if not bake.bake_static_lighting([context.active_object,]): self.report({"INFO"}, "No valid lights found to bake.")