2023-10-10 05:16:26 -04:00
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from hub.city_model_structure.city import City
|
|
|
|
from hub.exports.formats.obj import Obj
|
|
|
|
|
|
|
|
|
|
|
|
class GltExceptionError(Exception):
|
|
|
|
"""
|
|
|
|
Glt execution error
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class Glb:
|
|
|
|
def __init__(self, city, path, target_buildings=None):
|
|
|
|
self._city = city
|
|
|
|
self._path = path
|
|
|
|
if target_buildings is None:
|
|
|
|
target_buildings = [b.name for b in self._city.buildings]
|
|
|
|
self._target_buildings = target_buildings
|
|
|
|
self._export()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _obj2gtl(self):
|
|
|
|
"""
|
|
|
|
Get the SRA installation path
|
|
|
|
:return: str
|
|
|
|
"""
|
|
|
|
return shutil.which('obj2gltf')
|
|
|
|
|
|
|
|
def _export(self):
|
|
|
|
try:
|
|
|
|
for building in self._city.buildings:
|
2023-10-11 11:16:18 -04:00
|
|
|
print(self._city.lower_corner, self._city.upper_corner)
|
|
|
|
city = City(self._city.lower_corner, self._city.upper_corner, self._city.srs_name)
|
2023-10-10 05:16:26 -04:00
|
|
|
city.name = building.name
|
|
|
|
city.add_city_object(building)
|
2023-10-11 11:16:18 -04:00
|
|
|
Obj(city, self._path)
|
2023-10-10 05:16:26 -04:00
|
|
|
glb = f'{self._path}/{building.name}.glb'
|
|
|
|
subprocess.run([
|
|
|
|
self._obj2gtl,
|
|
|
|
'-i', f'{self._path}/{building.name}.obj',
|
|
|
|
'-b',
|
2023-10-11 08:32:31 -04:00
|
|
|
'-o', f'{glb}',
|
|
|
|
'--triangleWindingOrderSanitization',
|
|
|
|
'--inputUpAxis', 'Y',
|
|
|
|
'--outputUpAxis', 'Y'
|
2023-10-10 05:16:26 -04:00
|
|
|
])
|
2023-10-11 08:32:31 -04:00
|
|
|
|
2023-10-10 05:16:26 -04:00
|
|
|
os.unlink(f'{self._path}/{building.name}.obj')
|
|
|
|
os.unlink(f'{self._path}/{building.name}.mtl')
|
|
|
|
except (subprocess.SubprocessError, subprocess.TimeoutExpired, subprocess.CalledProcessError) as err:
|
|
|
|
raise GltExceptionError from err
|
|
|
|
|