Files
3d-model-generator/scene_setup.py
2026-05-14 10:23:47 +02:00

78 lines
2.8 KiB
Python

import bpy
import sys
import os
argv = sys.argv
argv = argv[argv.index("--") + 1:] if "--" in argv else []
glb_path = argv[0] if len(argv) > 0 else None
hdri_path = argv[1] if len(argv) > 1 else None
#bpy.ops.wm.read_factory_settings(use_empty=True)
if "Cube" in bpy.data.objects:
bpy.data.objects.remove(bpy.data.objects["Cube"], do_unlink=True)
# GLB importieren
if glb_path and os.path.isfile(glb_path):
bpy.ops.import_scene.gltf(filepath=glb_path)
else:
print("GLB file missing:", glb_path)
# HDRI oder Sonne
hdri_loaded = False
if hdri_path and os.path.isfile(hdri_path):
try:
world = bpy.data.worlds.new("World") if not bpy.data.worlds else bpy.data.worlds[0]
bpy.context.scene.world = world
world.use_nodes = True
ntree = world.node_tree
nodes = ntree.nodes
for node in nodes: nodes.remove(node)
node_bg = nodes.new(type='ShaderNodeBackground')
node_env = nodes.new(type='ShaderNodeTexEnvironment')
node_out = nodes.new(type='ShaderNodeOutputWorld')
node_env.image = bpy.data.images.load(hdri_path)
node_env.location = (-300, 0)
node_bg.location = (0, 0)
node_out.location = (300, 0)
ntree.links.new(node_env.outputs['Color'], node_bg.inputs['Color'])
ntree.links.new(node_bg.outputs['Background'], node_out.inputs['Surface'])
hdri_loaded = True
except Exception as e:
print("Failed to load HDRI:", e)
hdri_loaded = False
if not hdri_loaded:
# Schöne Sonnenlampe (leicht schräg von oben)
light_data = bpy.data.lights.new(name="Sun", type='SUN')
light_data.energy = 4.5
light = bpy.data.objects.new(name="Sun", object_data=light_data)
bpy.context.collection.objects.link(light)
light.location = (4, 10, 10)
light.rotation_euler = (0.8, 0.3, 0.1) # leicht schräg
# Optionale Fill-Light/Soft-Ambient
light_data2 = bpy.data.lights.new(name="Fill", type='SUN')
light_data2.energy = 1.1
light2 = bpy.data.objects.new(name="Fill", object_data=light_data2)
bpy.context.collection.objects.link(light2)
light2.location = (-8, -6, 4)
light2.rotation_euler = (1.4, -0.8, -0.2)
for window in bpy.context.window_manager.windows:
for area in window.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
space.shading.type = 'RENDERED'
for obj in bpy.data.objects:
# Entparenten, falls Parent ein Empty namens "world" ist
if obj.parent and obj.parent.name == "world":
obj.parent = None
if "world" in bpy.data.objects and bpy.data.objects["world"].type == "EMPTY":
bpy.data.objects.remove(bpy.data.objects["world"], do_unlink=True)
if "Cube" in bpy.data.objects:
bpy.data.objects.remove(bpy.data.objects["Cube"], do_unlink=True)