70 lines
3.7 KiB
Python
70 lines
3.7 KiB
Python
"""Fetch Poly Haven authoring assets; never called by the game or installer."""
|
|
from pathlib import Path
|
|
import argparse,concurrent.futures,hashlib,json,urllib.request
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
MODELS=ROOT/'assets/cinematics/blender/models'
|
|
HEADERS={'User-Agent':'AssemblerCasino-OfflineAssetAuthoring/1.0'}
|
|
|
|
|
|
def read(url):
|
|
return urllib.request.urlopen(urllib.request.Request(url,headers=HEADERS),timeout=60).read()
|
|
|
|
|
|
def fetch(name, resolution='2k'):
|
|
variants=json.loads(read('https://api.polyhaven.com/files/'+name))['gltf']
|
|
spec=variants.get(resolution,variants.get('2k',next(iter(variants.values()))))['gltf']
|
|
files=[(name+'.gltf',spec),*spec['include'].items()];records=[]
|
|
directory=(MODELS/name).resolve();directory.mkdir(parents=True,exist_ok=True)
|
|
for relative,info in files:
|
|
target=(directory/relative).resolve();assert target.is_relative_to(directory)
|
|
target.parent.mkdir(parents=True,exist_ok=True)
|
|
if not target.exists() or hashlib.md5(target.read_bytes()).hexdigest()!=info['md5']:
|
|
data=read(info['url']);assert hashlib.md5(data).hexdigest()==info['md5'],relative
|
|
target.write_bytes(data)
|
|
records.append(dict(file=target.relative_to(ROOT).as_posix(),source=info['url'],md5=info['md5'],license='CC0 / https://polyhaven.com/license',asset='https://polyhaven.com/a/'+name))
|
|
print('Verified scan:',name,flush=True);return records
|
|
|
|
|
|
def fetch_surface(name):
|
|
variants=json.loads(read('https://api.polyhaven.com/files/'+name))
|
|
directory=MODELS/'surfaces'/name
|
|
directory.mkdir(parents=True,exist_ok=True)
|
|
records=[]
|
|
for kind in ('Diffuse','Rough','nor_gl'):
|
|
spec=variants[kind]['2k']['jpg']
|
|
path=directory/(kind+'.jpg')
|
|
if not path.is_file() or hashlib.md5(path.read_bytes()).hexdigest()!=spec['md5']:
|
|
data=read(spec['url'])
|
|
assert hashlib.md5(data).hexdigest()==spec['md5'],path
|
|
path.write_bytes(data)
|
|
records.append(dict(file=path.relative_to(ROOT).as_posix(),source=spec['url'],md5=spec['md5'],
|
|
license='CC0 / https://polyhaven.com/license',asset='https://polyhaven.com/a/'+name))
|
|
print('Verified surface:',name,flush=True)
|
|
return records
|
|
|
|
|
|
if __name__=='__main__':
|
|
sets = {
|
|
5: ('trashbag','cardboard_box_01','binder_notebook'),
|
|
6: ('vintage_electric_kettle','alarm_clock_01','screwdrivers_02','circuit_board',
|
|
'book_encyclopedia_set_01','vintage_radio_transceiver','russian_food_cans_01',
|
|
'pot_enamel_01'),
|
|
7: ('worn_metal_rack','vintage_day_bed','throw_pillows_01','wooden_table_02',
|
|
'vintage_wooden_drawer_01','wooden_crate_01','plastic_crate_02','metal_toolbox',
|
|
'folding_wooden_stool','garden_gloves_01','office_notepads','classic_laptop',
|
|
'portable_cassette_player','pliers','bench_vice_01','vintage_telephone_wall_clock',
|
|
'mounted_fluorescent_lights','drawer_cabinet'),
|
|
}
|
|
parser=argparse.ArgumentParser()
|
|
parser.add_argument('--version',type=int,choices=sets,default=5)
|
|
version=parser.parse_args().version
|
|
resolution='1k' if version==7 else '2k'
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=6 if version==7 else 3) as pool:
|
|
jobs=[pool.submit(fetch,name,resolution) for name in sets[version]]
|
|
if version==7:
|
|
for name in ('plaster_grey_04','blue_plaster_weathered','old_linoleum_flooring_01',
|
|
'weathered_peeling_timber'):
|
|
jobs.append(pool.submit(fetch_surface,name))
|
|
records=[record for job in jobs for record in job.result()]
|
|
(MODELS/f'sources-v{version}.json').write_text(json.dumps(records,indent=2)+'\n')
|