Make lightmapdata importing optional and fix error where we attempted to load it even when the map didn't have lightmapdata (like legacy maps)

This commit is contained in:
Pyogenics
2025-04-10 11:43:05 +01:00
parent 4866a3ff8a
commit d971ebade3
2 changed files with 40 additions and 31 deletions

View File

@@ -150,13 +150,14 @@ class BattleMapBlenderImporter:
# Allows subsequent map loads to be faster # Allows subsequent map loads to be faster
libraryCache = {} 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.mapData = mapData
self.lightmapData = lightmapData self.lightmapData = lightmapData
self.propLibrarySourcePath = propLibrarySourcePath self.propLibrarySourcePath = propLibrarySourcePath
self.import_static_geom = import_static_geom self.import_static_geom = import_static_geom
self.import_collision_geom = import_collision_geom self.import_collision_geom = import_collision_geom
self.import_spawn_points = import_spawn_points self.import_spawn_points = import_spawn_points
self.import_lightmapdata = import_lightmapdata
self.materials = {} self.materials = {}
@@ -212,25 +213,26 @@ class BattleMapBlenderImporter:
for ob in spawnPointObjects: for ob in spawnPointObjects:
ob.parent = groupOB ob.parent = groupOB
# Create a sun light object if self.import_lightmapdata:
li = bpy.data.lights.new("DirectionalLight", "SUN") # Create a sun light object
li.color = decodeIntColorToTuple(self.lightmapData.lightColour) li = bpy.data.lights.new("DirectionalLight", "SUN")
li.color = decodeIntColorToTuple(self.lightmapData.lightColour)
ob = bpy.data.objects.new(li.name, li) 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) 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 lightAngleX, lightAngleZ = self.lightmapData.lightAngle
ob.rotation_mode = "XYZ" ob.rotation_mode = "XYZ"
ob.rotation_euler = (lightAngleX, 0.0, lightAngleZ) ob.rotation_euler = (lightAngleX, 0.0, lightAngleZ)
objects.append(ob) objects.append(ob)
# Set ambient world light # Set ambient world light
scene = bpy.context.scene scene = bpy.context.scene
if scene.world == None: if scene.world == None:
wd = bpy.data.worlds.new("map") wd = bpy.data.worlds.new("map")
scene.world = wd scene.world = wd
world = scene.world world = scene.world
world.use_nodes = False world.use_nodes = False
world.color = decodeIntColorToTuple(self.lightmapData.ambientLightColour) world.color = decodeIntColorToTuple(self.lightmapData.ambientLightColour)
return objects return objects
@@ -275,14 +277,15 @@ class BattleMapBlenderImporter:
propOB.scale = propScale propOB.scale = propScale
# Lighting info # Lighting info
lightingMapObject = None if self.import_lightmapdata:
for mapObject in self.lightmapData.mapObjects: lightingMapObject = None
if mapObject.index == propData.ID: for mapObject in self.lightmapData.mapObjects:
lightingMapObject = mapObject if mapObject.index == propData.ID:
break lightingMapObject = mapObject
if lightingMapObject != None: break
#XXX: do something with lightingMapObject.recieveShadows?? if lightingMapObject != None:
propOB.visible_shadow = lightingMapObject.castShadows #XXX: do something with lightingMapObject.recieveShadows??
propOB.visible_shadow = lightingMapObject.castShadows
# Material # Material
ma = self.materials[propData.materialID] ma = self.materials[propData.materialID]

View File

@@ -31,7 +31,6 @@ from .BattleMap import BattleMap
from .BattleMapBlenderImporter import BattleMapBlenderImporter from .BattleMapBlenderImporter import BattleMapBlenderImporter
from .LightmapData import LightmapData from .LightmapData import LightmapData
from glob import glob
from time import time 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_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_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_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): def draw(self, context):
import_panel_options_battlemap(self.layout, self) import_panel_options_battlemap(self.layout, self)
@@ -124,9 +124,14 @@ class ImportBattleMap(Operator, ImportHelper):
importStartTime = time() importStartTime = time()
# lightmapdata files only exist for remaster maps
lightmapData = LightmapData() lightmapData = LightmapData()
with open(f"{self.directory}/lightmapdata", "rb") as file: if self.import_lightmapdata:
lightmapData.read(file) 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() mapData = BattleMap()
with open(self.filepath, "rb") as file: with open(self.filepath, "rb") as file:
@@ -134,7 +139,7 @@ class ImportBattleMap(Operator, ImportHelper):
# Import data into blender # Import data into blender
preferences = context.preferences.addons[__package__].preferences # TODO: check if this is set before proceeding 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() objects = mapImporter.importData()
# Link objects # Link objects
@@ -165,6 +170,7 @@ def import_panel_options_battlemap(layout, operator):
body.prop(operator, "import_static_geom") body.prop(operator, "import_static_geom")
body.prop(operator, "import_collision_geom") body.prop(operator, "import_collision_geom")
body.prop(operator, "import_spawn_points") body.prop(operator, "import_spawn_points")
body.prop(operator, "import_lightmapdata")
def menu_func_import_a3d(self, context): def menu_func_import_a3d(self, context):
self.layout.operator(ImportA3D.bl_idname, text="Alternativa3D HTML5 (.a3d)") self.layout.operator(ImportA3D.bl_idname, text="Alternativa3D HTML5 (.a3d)")