Skip to content

Commit d971eba

Browse files
committed
Make lightmapdata importing optional and fix error where we attempted to load it even when the map didn't have lightmapdata (like legacy maps)
1 parent 4866a3f commit d971eba

2 files changed

Lines changed: 41 additions & 32 deletions

File tree

io_scene_a3d/BattleMapBlenderImporter.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,14 @@ class BattleMapBlenderImporter:
150150
# Allows subsequent map loads to be faster
151151
libraryCache = {}
152152

153-
def __init__(self, mapData, lightmapData, propLibrarySourcePath, import_static_geom=True, import_collision_geom=False, import_spawn_points=False):
153+
def __init__(self, mapData, lightmapData, propLibrarySourcePath, import_static_geom=True, import_collision_geom=False, import_spawn_points=False, import_lightmapdata=False):
154154
self.mapData = mapData
155155
self.lightmapData = lightmapData
156156
self.propLibrarySourcePath = propLibrarySourcePath
157157
self.import_static_geom = import_static_geom
158158
self.import_collision_geom = import_collision_geom
159159
self.import_spawn_points = import_spawn_points
160+
self.import_lightmapdata = import_lightmapdata
160161

161162
self.materials = {}
162163

@@ -212,25 +213,26 @@ def importData(self):
212213
for ob in spawnPointObjects:
213214
ob.parent = groupOB
214215

215-
# Create a sun light object
216-
li = bpy.data.lights.new("DirectionalLight", "SUN")
217-
li.color = decodeIntColorToTuple(self.lightmapData.lightColour)
218-
219-
ob = bpy.data.objects.new(li.name, li)
220-
ob.location = (0.0, 0.0, 1000.0) # Just place it like 10 meters off the ground (in alternativa units)
221-
lightAngleX, lightAngleZ = self.lightmapData.lightAngle
222-
ob.rotation_mode = "XYZ"
223-
ob.rotation_euler = (lightAngleX, 0.0, lightAngleZ)
224-
objects.append(ob)
225-
226-
# Set ambient world light
227-
scene = bpy.context.scene
228-
if scene.world == None:
229-
wd = bpy.data.worlds.new("map")
230-
scene.world = wd
231-
world = scene.world
232-
world.use_nodes = False
233-
world.color = decodeIntColorToTuple(self.lightmapData.ambientLightColour)
216+
if self.import_lightmapdata:
217+
# Create a sun light object
218+
li = bpy.data.lights.new("DirectionalLight", "SUN")
219+
li.color = decodeIntColorToTuple(self.lightmapData.lightColour)
220+
221+
ob = bpy.data.objects.new(li.name, li)
222+
ob.location = (0.0, 0.0, 1000.0) # Just place it like 10 meters off the ground (in alternativa units)
223+
lightAngleX, lightAngleZ = self.lightmapData.lightAngle
224+
ob.rotation_mode = "XYZ"
225+
ob.rotation_euler = (lightAngleX, 0.0, lightAngleZ)
226+
objects.append(ob)
227+
228+
# Set ambient world light
229+
scene = bpy.context.scene
230+
if scene.world == None:
231+
wd = bpy.data.worlds.new("map")
232+
scene.world = wd
233+
world = scene.world
234+
world.use_nodes = False
235+
world.color = decodeIntColorToTuple(self.lightmapData.ambientLightColour)
234236

235237
return objects
236238

@@ -275,14 +277,15 @@ def getBlenderProp(self, propData):
275277
propOB.scale = propScale
276278

277279
# Lighting info
278-
lightingMapObject = None
279-
for mapObject in self.lightmapData.mapObjects:
280-
if mapObject.index == propData.ID:
281-
lightingMapObject = mapObject
282-
break
283-
if lightingMapObject != None:
284-
#XXX: do something with lightingMapObject.recieveShadows??
285-
propOB.visible_shadow = lightingMapObject.castShadows
280+
if self.import_lightmapdata:
281+
lightingMapObject = None
282+
for mapObject in self.lightmapData.mapObjects:
283+
if mapObject.index == propData.ID:
284+
lightingMapObject = mapObject
285+
break
286+
if lightingMapObject != None:
287+
#XXX: do something with lightingMapObject.recieveShadows??
288+
propOB.visible_shadow = lightingMapObject.castShadows
286289

287290
# Material
288291
ma = self.materials[propData.materialID]

io_scene_a3d/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from .BattleMapBlenderImporter import BattleMapBlenderImporter
3232
from .LightmapData import LightmapData
3333

34-
from glob import glob
3534
from time import time
3635

3736
'''
@@ -112,6 +111,7 @@ class ImportBattleMap(Operator, ImportHelper):
112111
import_static_geom: BoolProperty(name="Import static geometry", description="Static geometry includes all the visual aspects of the map", default=True)
113112
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)
114113
import_spawn_points: BoolProperty(name="Import spawn points", description="Places a marker at locations where tanks can spawn", default=False)
114+
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)
115115

116116
def draw(self, context):
117117
import_panel_options_battlemap(self.layout, self)
@@ -124,17 +124,22 @@ def execute(self, context):
124124

125125
importStartTime = time()
126126

127+
# lightmapdata files only exist for remaster maps
127128
lightmapData = LightmapData()
128-
with open(f"{self.directory}/lightmapdata", "rb") as file:
129-
lightmapData.read(file)
129+
if self.import_lightmapdata:
130+
try:
131+
with open(f"{self.directory}/lightmapdata", "rb") as file: lightmapData.read(file)
132+
except:
133+
print("Couldn't open lightmapdata file, ignoring")
134+
self.import_lightmapdata = False
130135

131136
mapData = BattleMap()
132137
with open(self.filepath, "rb") as file:
133138
mapData.read(file)
134139

135140
# Import data into blender
136141
preferences = context.preferences.addons[__package__].preferences # TODO: check if this is set before proceeding
137-
mapImporter = BattleMapBlenderImporter(mapData, lightmapData, preferences.propLibrarySourcePath, self.import_static_geom, self.import_collision_geom, self.import_spawn_points)
142+
mapImporter = BattleMapBlenderImporter(mapData, lightmapData, preferences.propLibrarySourcePath, self.import_static_geom, self.import_collision_geom, self.import_spawn_points, self.import_lightmapdata)
138143
objects = mapImporter.importData()
139144

140145
# Link objects
@@ -165,6 +170,7 @@ def import_panel_options_battlemap(layout, operator):
165170
body.prop(operator, "import_static_geom")
166171
body.prop(operator, "import_collision_geom")
167172
body.prop(operator, "import_spawn_points")
173+
body.prop(operator, "import_lightmapdata")
168174

169175
def menu_func_import_a3d(self, context):
170176
self.layout.operator(ImportA3D.bl_idname, text="Alternativa3D HTML5 (.a3d)")

0 commit comments

Comments
 (0)