From d971ebade34905d5cf148e5fc74175035a6db04c Mon Sep 17 00:00:00 2001 From: Pyogenics Date: Thu, 10 Apr 2025 11:43:05 +0100 Subject: [PATCH] Make lightmapdata importing optional and fix error where we attempted to load it even when the map didn't have lightmapdata (like legacy maps) --- io_scene_a3d/BattleMapBlenderImporter.py | 57 +++++++++++++----------- io_scene_a3d/__init__.py | 14 ++++-- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/io_scene_a3d/BattleMapBlenderImporter.py b/io_scene_a3d/BattleMapBlenderImporter.py index 09747bb..431c56e 100644 --- a/io_scene_a3d/BattleMapBlenderImporter.py +++ b/io_scene_a3d/BattleMapBlenderImporter.py @@ -150,13 +150,14 @@ class BattleMapBlenderImporter: # Allows subsequent map loads to be faster libraryCache = {} - def __init__(self, mapData, lightmapData, propLibrarySourcePath, import_static_geom=True, import_collision_geom=False, import_spawn_points=False): + def __init__(self, mapData, lightmapData, propLibrarySourcePath, import_static_geom=True, import_collision_geom=False, import_spawn_points=False, import_lightmapdata=False): self.mapData = mapData self.lightmapData = lightmapData self.propLibrarySourcePath = propLibrarySourcePath self.import_static_geom = import_static_geom self.import_collision_geom = import_collision_geom self.import_spawn_points = import_spawn_points + self.import_lightmapdata = import_lightmapdata self.materials = {} @@ -212,25 +213,26 @@ class BattleMapBlenderImporter: for ob in spawnPointObjects: ob.parent = groupOB - # Create a sun light object - li = bpy.data.lights.new("DirectionalLight", "SUN") - li.color = decodeIntColorToTuple(self.lightmapData.lightColour) - - ob = bpy.data.objects.new(li.name, li) - ob.location = (0.0, 0.0, 1000.0) # Just place it like 10 meters off the ground (in alternativa units) - lightAngleX, lightAngleZ = self.lightmapData.lightAngle - ob.rotation_mode = "XYZ" - ob.rotation_euler = (lightAngleX, 0.0, lightAngleZ) - objects.append(ob) + if self.import_lightmapdata: + # Create a sun light object + li = bpy.data.lights.new("DirectionalLight", "SUN") + li.color = decodeIntColorToTuple(self.lightmapData.lightColour) + + ob = bpy.data.objects.new(li.name, li) + ob.location = (0.0, 0.0, 1000.0) # Just place it like 10 meters off the ground (in alternativa units) + lightAngleX, lightAngleZ = self.lightmapData.lightAngle + ob.rotation_mode = "XYZ" + ob.rotation_euler = (lightAngleX, 0.0, lightAngleZ) + objects.append(ob) - # Set ambient world light - scene = bpy.context.scene - if scene.world == None: - wd = bpy.data.worlds.new("map") - scene.world = wd - world = scene.world - world.use_nodes = False - world.color = decodeIntColorToTuple(self.lightmapData.ambientLightColour) + # Set ambient world light + scene = bpy.context.scene + if scene.world == None: + wd = bpy.data.worlds.new("map") + scene.world = wd + world = scene.world + world.use_nodes = False + world.color = decodeIntColorToTuple(self.lightmapData.ambientLightColour) return objects @@ -275,14 +277,15 @@ class BattleMapBlenderImporter: propOB.scale = propScale # Lighting info - lightingMapObject = None - for mapObject in self.lightmapData.mapObjects: - if mapObject.index == propData.ID: - lightingMapObject = mapObject - break - if lightingMapObject != None: - #XXX: do something with lightingMapObject.recieveShadows?? - propOB.visible_shadow = lightingMapObject.castShadows + if self.import_lightmapdata: + lightingMapObject = None + for mapObject in self.lightmapData.mapObjects: + if mapObject.index == propData.ID: + lightingMapObject = mapObject + break + if lightingMapObject != None: + #XXX: do something with lightingMapObject.recieveShadows?? + propOB.visible_shadow = lightingMapObject.castShadows # Material ma = self.materials[propData.materialID] diff --git a/io_scene_a3d/__init__.py b/io_scene_a3d/__init__.py index 4ca63db..3d0827c 100644 --- a/io_scene_a3d/__init__.py +++ b/io_scene_a3d/__init__.py @@ -31,7 +31,6 @@ from .BattleMap import BattleMap from .BattleMapBlenderImporter import BattleMapBlenderImporter from .LightmapData import LightmapData -from glob import glob from time import time ''' @@ -112,6 +111,7 @@ class ImportBattleMap(Operator, ImportHelper): import_static_geom: BoolProperty(name="Import static geometry", description="Static geometry includes all the visual aspects of the map", default=True) import_collision_geom: BoolProperty(name="Import collision geometry", description="Collision geometry defines the geometry used for collision checks and cannot normally be seen by players", default=False) import_spawn_points: BoolProperty(name="Import spawn points", description="Places a marker at locations where tanks can spawn", default=False) + import_lightmapdata: BoolProperty(name="Import lighting information", description="Loads the lightmapdata file which stores information about the sun, ambient lighting and shadow settings. Only works on remaster maps.", default=True) def draw(self, context): import_panel_options_battlemap(self.layout, self) @@ -124,9 +124,14 @@ class ImportBattleMap(Operator, ImportHelper): importStartTime = time() + # lightmapdata files only exist for remaster maps lightmapData = LightmapData() - with open(f"{self.directory}/lightmapdata", "rb") as file: - lightmapData.read(file) + if self.import_lightmapdata: + try: + with open(f"{self.directory}/lightmapdata", "rb") as file: lightmapData.read(file) + except: + print("Couldn't open lightmapdata file, ignoring") + self.import_lightmapdata = False mapData = BattleMap() with open(self.filepath, "rb") as file: @@ -134,7 +139,7 @@ class ImportBattleMap(Operator, ImportHelper): # Import data into blender preferences = context.preferences.addons[__package__].preferences # TODO: check if this is set before proceeding - mapImporter = BattleMapBlenderImporter(mapData, lightmapData, preferences.propLibrarySourcePath, self.import_static_geom, self.import_collision_geom, self.import_spawn_points) + mapImporter = BattleMapBlenderImporter(mapData, lightmapData, preferences.propLibrarySourcePath, self.import_static_geom, self.import_collision_geom, self.import_spawn_points, self.import_lightmapdata) objects = mapImporter.importData() # Link objects @@ -165,6 +170,7 @@ def import_panel_options_battlemap(layout, operator): body.prop(operator, "import_static_geom") body.prop(operator, "import_collision_geom") body.prop(operator, "import_spawn_points") + body.prop(operator, "import_lightmapdata") def menu_func_import_a3d(self, context): self.layout.operator(ImportA3D.bl_idname, text="Alternativa3D HTML5 (.a3d)")