Files
Diabolo/tools/review_iron_maiden.py

42 lines
2.8 KiB
Python

"""Render source/game prop in Blender for orientation and hinge review.
blender -b --python tools/review_iron_maiden.py -- [--source]
"""
import bpy, math, sys, json
from pathlib import Path
from mathutils import Vector
ROOT=Path(__file__).resolve().parents[1]
source='--source' in sys.argv
path=ROOT/('tools/model_sources/iron_maiden_v01/source.glb' if source else 'Demon_v0.19/art/equipment/iron_maiden_v01/iron_maiden.glb')
out=ROOT/'Demon_v0.19/captures/iron-maiden';out.mkdir(parents=True,exist_ok=True)
bpy.ops.wm.read_factory_settings(use_empty=True)
bpy.ops.import_scene.gltf(filepath=str(path))
mesh=[o for o in bpy.context.scene.objects if o.type=='MESH']
points=[o.matrix_world@v.co for o in mesh for v in o.data.vertices]
lo=Vector([min(v[i] for v in points) for i in range(3)]);hi=Vector([max(v[i] for v in points) for i in range(3)])
center=(lo+hi)/2;size=max(hi-lo)
print('MODEL_BOUNDS',list(lo),list(hi),'POLYGONS',sum(len(o.data.polygons) for o in mesh))
scene=bpy.context.scene;scene.render.engine='CYCLES';scene.cycles.samples=16
scene.render.resolution_x=900;scene.render.resolution_y=900;scene.render.resolution_percentage=100
scene.world=bpy.data.worlds.new('Studio');scene.world.use_nodes=True
nodes=scene.world.node_tree.nodes;nodes.clear()
background=nodes.new('ShaderNodeBackground');background.inputs[0].default_value=(.12,.14,.18,1);background.inputs[1].default_value=.5
output=nodes.new('ShaderNodeOutputWorld');scene.world.node_tree.links.new(background.outputs[0],output.inputs[0])
for name,location,power,scale in [('Key',(3,-4,5),700,4),('Fill',(-3,-2,2),450,3),('Rim',(1,3,4),900,3)]:
lamp=bpy.data.lights.new(name,'AREA');lamp.energy=power;lamp.shape='DISK';lamp.size=scale
obj=bpy.data.objects.new(name,lamp);scene.collection.objects.link(obj);obj.location=center+Vector(location)*size/3
obj.rotation_euler=(center-obj.location).to_track_quat('-Z','Y').to_euler()
camera=bpy.data.objects.new('Camera',bpy.data.cameras.new('Camera'));scene.collection.objects.link(camera);scene.camera=camera
camera.data.type='ORTHO';camera.data.ortho_scale=size*1.30
views=[('source-front' if source else 'studio-closed',(0,-6,1.0)),('source-back' if source else 'studio-back',(0,6,1.0))]
if not source:views += [('studio-half',(4,-6,2.0)),('studio-open',(4,-6,2.0))]
for name,direction in views:
if name in ('studio-half','studio-open'):
angle=.9 if name=='studio-half' else 1.8
bpy.data.objects['LeftDoor'].rotation_mode='XYZ'
bpy.data.objects['RightDoor'].rotation_mode='XYZ'
bpy.data.objects['LeftDoor'].rotation_euler.z=-angle
bpy.data.objects['RightDoor'].rotation_euler.z=angle
camera.location=center+Vector(direction)*size
camera.rotation_euler=(center-camera.location).to_track_quat('-Z','Y').to_euler()
scene.render.filepath=str(out/(name+'.png'));bpy.ops.render.render(write_still=True)