|
BTD /
Lab5BTD/Modul 5: ÜbungenAufgabe 1: Prozedurales Geometry Nodes Tool
Aufgabe 2: Partikel- und Fluid-Simulation
Aufgabe 3: Simulation Cache Management Script
Material:Crowd-Sim mit Mesh-Deformation und Agentenbewegung![]() Ein prozedurales Gelände-Mesh ist die Grundlage für die Bewegung der Agenten (rote Kegel).
import bpy
import math
import random
from mathutils import Vector
# -----------------------------
# CLEAN SCENE
# -----------------------------
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
# -----------------------------
# PARAMETERS
# -----------------------------
AGENT_COUNT = 50
SURFACE_SIZE = 10
TIME_SCALE = 0.03
WAVE_AMPLITUDE = 1.0
NOISE_SCALE = 0.7
AGENT_SPEED = 0.4
# -----------------------------
# CREATE SURFACE
# -----------------------------
bpy.ops.mesh.primitive_grid_add(
size=SURFACE_SIZE,
x_subdivisions=200,
y_subdivisions=200
)
surface = bpy.context.object
surface.name = "Procedural_Surface"
# Subdivision for smooth deformation
sub = surface.modifiers.new("Subd", 'SUBSURF')
sub.levels = 2
# -----------------------------
# CREATE AGENT MESH
# -----------------------------
bpy.ops.mesh.primitive_cone_add(radius1=0.1, depth=0.4)
agent_mesh = bpy.context.object
agent_mesh.name = "AgentMesh"
agent_mesh.hide_viewport = True
agent_mesh.hide_render = True
# -----------------------------
# AGENT COLLECTION
# -----------------------------
agents = []
for i in range(AGENT_COUNT):
obj = agent_mesh.copy()
obj.data = agent_mesh.data.copy()
bpy.context.collection.objects.link(obj)
obj.name = f"Agent_{i:03d}"
obj["seed"] = random.uniform(0, 1000)
agents.append(obj)
# -----------------------------
# SURFACE DEFORMATION HANDLER
# -----------------------------
def deform_surface(scene):
t = scene.frame_current * TIME_SCALE
mesh = surface.data
for v in mesh.vertices:
x, y = v.co.x, v.co.y
wave = math.sin(x + t) * math.cos(y + t)
noise = math.sin((x + y) * NOISE_SCALE + t)
v.co.z = wave * WAVE_AMPLITUDE + noise * 0.5
mesh.update()
# -----------------------------
# AGENT MOTION HANDLER
# -----------------------------
def animate_agents(scene):
t = scene.frame_current * TIME_SCALE
depsgraph = bpy.context.evaluated_depsgraph_get()
eval_surface = surface.evaluated_get(depsgraph)
mesh = eval_surface.to_mesh()
for obj in agents:
seed = obj["seed"]
# Parametric movement
u = (math.sin(t + seed) + 1) * 0.5
v = (math.cos(t * 0.7 + seed) + 1) * 0.5
x = (u - 0.5) * SURFACE_SIZE
y = (v - 0.5) * SURFACE_SIZE
# Raycast to surface
hit, loc, normal, _ = eval_surface.ray_cast(
Vector((x, y, 10)),
Vector((0, 0, -1))
)
if hit:
obj.location = loc
obj.rotation_mode = 'QUATERNION'
obj.rotation_quaternion = normal.to_track_quat('Z', 'Y')
eval_surface.to_mesh_clear()
# -----------------------------
# REGISTER HANDLERS
# -----------------------------
bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append(deform_surface)
bpy.app.handlers.frame_change_pre.append(animate_agents)
# -----------------------------
# TIMELINE SETUP
# -----------------------------
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 500
print("Procedural animation demo initialized.")
|