forked from Emmanuelcsam/Safe-Sound
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlender.txt
More file actions
228 lines (188 loc) · 8.14 KB
/
Blender.txt
File metadata and controls
228 lines (188 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import bpy
import random
import math
from mathutils import Vector
# =======================
# Configuration Settings
# =======================
NUM_DRONES = random.randint(3, 7)
NUM_HOSPITALS = random.randint(5, 10)
NUM_CLOUDS = NUM_HOSPITALS * 2
CITY_GRID_SIZE = 20
BUILDING_SPACING = 5.0
MIN_FLOORS = 2
MAX_FLOORS = 12
HOSPITAL_RADIUS = 2
HOSPITAL_HEIGHT = 8
CLOUD_RADIUS = 3
CLOUD_FLATTEN_SCALE_Z = 0.3
CLOUD_ALTITUDE = 10
CLOUD_SPEED = 0.1
DRONE_ALTITUDE = CLOUD_ALTITUDE # Drones move at the same Z-axis as clouds
DRONE_SIZE = 1.5 # Increased drone size
DRONE_SPEED = 0.15 # Slightly faster for better avoidance
DRONE_AVOIDANCE_RADIUS = CLOUD_RADIUS + 3 # Larger avoidance area
CITY_CENTER = Vector((0, 0, 0))
CITY_BOUNDARY_X = CITY_GRID_SIZE * BUILDING_SPACING / 2
CITY_BOUNDARY_Y = CITY_GRID_SIZE * BUILDING_SPACING / 2
# ================
# Helper Functions
# ================
def clear_scene():
"""Delete all existing objects in the scene."""
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
def create_rain_cloud(location, direction):
"""Create a rain cloud at the specified location with a random direction."""
bpy.ops.object.metaball_add(type='BALL', location=location)
cloud = bpy.context.object
cloud.name = "Rain_Cloud"
cloud.scale = (CLOUD_RADIUS, CLOUD_RADIUS, CLOUD_FLATTEN_SCALE_Z)
cloud["direction"] = direction
return cloud
def create_drone(location):
"""Create a detailed drone model at the specified location."""
bpy.ops.mesh.primitive_uv_sphere_add(radius=DRONE_SIZE / 3, location=location)
drone_body = bpy.context.object
drone_body.name = "Drone_Body"
# Add propellers
for i in range(4):
angle = math.radians(90 * i)
propeller_offset = Vector((math.cos(angle), math.sin(angle), 0)) * DRONE_SIZE * 0.5
propeller_location = location + propeller_offset
bpy.ops.mesh.primitive_cylinder_add(radius=DRONE_SIZE / 10, depth=0.2, location=propeller_location)
propeller = bpy.context.object
propeller.rotation_euler = (0, 0, angle)
propeller.name = f"Drone_Propeller_{i}"
return drone_body
def create_building(location, width, depth, height):
"""Create a unique building with specified dimensions."""
bpy.ops.mesh.primitive_cube_add(size=1, location=location)
building = bpy.context.object
building.scale = (width, depth, height / 2)
building.location.z = height / 2 # Raise to ground level
# Random material
mat = bpy.data.materials.new(name="Building_Material")
mat.diffuse_color = (random.random(), random.random(), random.random(), 1)
building.data.materials.append(mat)
return building
def create_hospital(location):
"""Create a hospital cylinder."""
bpy.ops.mesh.primitive_cylinder_add(radius=HOSPITAL_RADIUS, depth=HOSPITAL_HEIGHT, location=location)
hospital = bpy.context.object
hospital.name = "Hospital"
# Assign hospital material
mat = bpy.data.materials.new(name="Hospital_Material")
mat.diffuse_color = (1, 0, 0, 1) # Red color
hospital.data.materials.append(mat)
return hospital
def move_clouds(clouds):
"""Move clouds in a smooth random path, avoiding hospitals."""
for cloud in clouds:
direction = Vector(cloud["direction"])
new_pos = cloud.location + direction * CLOUD_SPEED
# Check hospital proximity
near_hospital = any(
(Vector((h.location.x, h.location.y, CLOUD_ALTITUDE)) - new_pos).length < CLOUD_RADIUS + HOSPITAL_RADIUS
for h in hospitals
)
# Bounce off boundaries or hospitals
if abs(new_pos.x) > CITY_BOUNDARY_X or near_hospital:
direction.x *= -1
if abs(new_pos.y) > CITY_BOUNDARY_Y or near_hospital:
direction.y *= -1
cloud.location += direction * CLOUD_SPEED
cloud["direction"] = direction
def move_drone(drone, target, clouds):
"""Move the drone towards the target, avoiding clouds."""
current_pos = Vector(drone.location)
target_vector = target - current_pos
attraction = target_vector.normalized() if target_vector.length > 0 else Vector()
repulsion = Vector()
for cloud in clouds:
cloud_pos = Vector((cloud.location.x, cloud.location.y, 0))
drone_pos_2d = Vector((current_pos.x, current_pos.y, 0))
distance = (cloud_pos - drone_pos_2d).length
if distance < DRONE_AVOIDANCE_RADIUS:
direction = (drone_pos_2d - cloud_pos).normalized()
strength = 1.5 * (DRONE_AVOIDANCE_RADIUS - distance) / DRONE_AVOIDANCE_RADIUS
repulsion += direction * strength
movement = attraction + repulsion
if movement.length > 0:
movement.normalize()
new_pos = current_pos + movement * DRONE_SPEED
new_pos.z = DRONE_ALTITUDE # Maintain altitude (same as clouds)
drone.location = new_pos
# =========================
# Main Script Execution
# =========================
clear_scene()
# Create city grid
for x in range(CITY_GRID_SIZE):
for y in range(CITY_GRID_SIZE):
width = random.uniform(1, 3)
depth = random.uniform(1, 3)
height = random.uniform(MIN_FLOORS, MAX_FLOORS)
pos_x = (x - CITY_GRID_SIZE/2) * BUILDING_SPACING
pos_y = (y - CITY_GRID_SIZE/2) * BUILDING_SPACING
create_building(Vector((pos_x, pos_y, 0)), width, depth, height)
# Create hospitals
hospitals = []
hospital_positions = []
for _ in range(NUM_HOSPITALS):
while True:
pos = Vector((
random.uniform(-CITY_BOUNDARY_X, CITY_BOUNDARY_X),
random.uniform(-CITY_BOUNDARY_Y, CITY_BOUNDARY_Y),
HOSPITAL_HEIGHT / 2
))
if all((pos - p).length > HOSPITAL_RADIUS * 2 for p in hospital_positions):
break
hospital_positions.append(pos)
hospitals.append(create_hospital(pos))
# Create clouds
clouds = []
for _ in range(NUM_CLOUDS):
while True:
pos = Vector((
random.uniform(-CITY_BOUNDARY_X, CITY_BOUNDARY_X),
random.uniform(-CITY_BOUNDARY_Y, CITY_BOUNDARY_Y),
CLOUD_ALTITUDE
))
if all((pos - p).length > CLOUD_RADIUS * 2 for p in [c.location for c in clouds]) and \
all((pos - p).length > (CLOUD_RADIUS + HOSPITAL_RADIUS) for p in hospital_positions):
break
direction = Vector((random.uniform(-1, 1), random.uniform(-1, 1), 0)).normalized()
clouds.append(create_rain_cloud(pos, direction))
# Initialize drones
drones = []
for _ in range(NUM_DRONES):
start_hospital = random.choice(hospitals)
start = start_hospital.location.copy()
start.z = DRONE_ALTITUDE # Set drone altitude to match clouds
end_hospital = random.choice([h for h in hospitals if h != start_hospital])
end = end_hospital.location.copy()
end.z = DRONE_ALTITUDE # Set target altitude to match clouds
drone = create_drone(start)
drones.append((drone, Vector(end), start_hospital))
# Simulation handler
def update_scene(scene):
move_clouds(clouds)
for idx, (drone, target, current_hospital) in enumerate(drones):
# Check if target reached before moving
if (drone.location - target).length < DRONE_SPEED * 2:
# Find new target hospital (different from current)
new_hospital = random.choice([h for h in hospitals if h != current_hospital])
new_target = new_hospital.location.copy()
new_target.z = DRONE_ALTITUDE # Set target altitude to match clouds
# Move drone to new starting position
departure_point = current_hospital.location.copy()
departure_point.z = DRONE_ALTITUDE # Set departure altitude to match clouds
drone.location = departure_point
drones[idx] = (drone, Vector(new_target), new_hospital)
continue
# Normal movement
move_drone(drone, target, clouds)
# Register the simulation update function
bpy.app.handlers.frame_change_pre.append(update_scene)
print("Enhanced simulation setup complete!")