|
1 |
| -# |
| 1 | +#+ |
2 | 2 | # spaceship_generator.py
|
3 | 3 | #
|
4 | 4 | # This is a Blender script that uses procedural generation to create
|
5 |
| -# textured 3D spaceship models. Tested with Blender 2.79. |
6 |
| -# |
7 |
| - |
8 |
| -# https://github.com/a1studmuffin/SpaceshipGenerator |
| 5 | +# textured 3D spaceship models. Tested with Blender 2.82. |
9 | 6 | #
|
| 7 | +# [email protected], Lawrence D'Oliveiro |
| 8 | +# https://github.com/ldo/SpaceshipGenerator |
| 9 | +#- |
10 | 10 |
|
11 | 11 | import os
|
12 | 12 | import bpy
|
@@ -1097,141 +1097,3 @@ def add_disc_to_face(bm, face) :
|
1097 | 1097 |
|
1098 | 1098 | return obj
|
1099 | 1099 | #end generate_spaceship
|
1100 |
| - |
1101 |
| -if __name__ == "__main__" : |
1102 |
| - |
1103 |
| - import datetime |
1104 |
| - |
1105 |
| - # Deletes all existing spaceships and unused materials from the scene |
1106 |
| - def reset_scene() : |
1107 |
| - for item in bpy.data.objects : |
1108 |
| - item.select = item.name.startswith("Spaceship") |
1109 |
| - #end for |
1110 |
| - bpy.ops.object.delete() |
1111 |
| - for material in bpy.data.materials : |
1112 |
| - if not material.users : |
1113 |
| - bpy.data.materials.remove(material) |
1114 |
| - #end if |
1115 |
| - #end for |
1116 |
| - for texture in bpy.data.textures : |
1117 |
| - if not texture.users : |
1118 |
| - bpy.data.textures.remove(texture) |
1119 |
| - #end if |
1120 |
| - #end for |
1121 |
| - #end reset_scene |
1122 |
| - |
1123 |
| - # When true, this script will generate a single spaceship in the scene. |
1124 |
| - # When false, this script will render multiple movie frames showcasing lots of ships. |
1125 |
| - generate_single_spaceship = True |
1126 |
| - |
1127 |
| - if generate_single_spaceship : |
1128 |
| - # Reset the scene, generate a single spaceship and focus on it |
1129 |
| - reset_scene() |
1130 |
| - parms_defaults.geom_ranseed = "" |
1131 |
| - parms_defaults.mat_ranseed = "" |
1132 |
| - # add anything here to generate the same spaceship |
1133 |
| - obj = generate_spaceship(parms_defaults) |
1134 |
| - |
1135 |
| - # View the selected object in all views |
1136 |
| - for area in bpy.context.screen.areas : |
1137 |
| - if area.type == "VIEW_3D" : |
1138 |
| - ctx = bpy.context.copy() |
1139 |
| - ctx["area"] = area |
1140 |
| - ctx["region"] = area.regions[-1] |
1141 |
| - bpy.ops.view3d.view_selected(ctx) |
1142 |
| - #end if |
1143 |
| - #end for |
1144 |
| - |
1145 |
| - else : |
1146 |
| - # Export a movie showcasing many different kinds of ships |
1147 |
| - |
1148 |
| - # Settings |
1149 |
| - output_path = "" # leave empty to use script folder |
1150 |
| - total_movie_duration = 16 |
1151 |
| - total_spaceship_duration = 1 |
1152 |
| - yaw_rate = 45 * deg # angle/sec |
1153 |
| - yaw_offset = 220 * deg # angle/sec |
1154 |
| - camera_pole_rate = 1 |
1155 |
| - camera_pole_pitch_min = 15 * deg |
1156 |
| - camera_pole_pitch_max = 30 * deg |
1157 |
| - camera_pole_pitch_offset = 0 * deg |
1158 |
| - camera_pole_length = 10 |
1159 |
| - camera_refocus_object_every_frame = False |
1160 |
| - fov = 60 * deg |
1161 |
| - fps = 30 |
1162 |
| - res_x = 1920 |
1163 |
| - res_y = 1080 |
1164 |
| - |
1165 |
| - # Batch render the movie frames |
1166 |
| - inv_fps = 1 / fps |
1167 |
| - movie_duration = 0 |
1168 |
| - spaceship_duration = total_spaceship_duration |
1169 |
| - scene = bpy.data.scenes["Scene"] |
1170 |
| - scene.render.resolution_x = res_x |
1171 |
| - scene.render.resolution_y = res_y |
1172 |
| - scene.camera.rotation_mode = "XYZ" |
1173 |
| - scene.camera.data.angle = fov |
1174 |
| - frame = 0 |
1175 |
| - timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
1176 |
| - while movie_duration < total_movie_duration : |
1177 |
| - movie_duration += inv_fps |
1178 |
| - spaceship_duration += inv_fps |
1179 |
| - if spaceship_duration >= total_spaceship_duration : |
1180 |
| - spaceship_duration -= total_spaceship_duration |
1181 |
| - |
1182 |
| - # Generate a new spaceship |
1183 |
| - reset_scene() |
1184 |
| - obj = generate_spaceship(parms_defaults) |
1185 |
| - |
1186 |
| - # look for a mirror plane in the scene, and position it |
1187 |
| - # just underneath the ship if found |
1188 |
| - lowest_z = centre = min((Vector(b).z for b in obj.bound_box)) |
1189 |
| - plane_obj = bpy.data.objects.get("Plane") |
1190 |
| - if plane_obj : |
1191 |
| - plane_obj.location.z = lowest_z - 0.3 |
1192 |
| - #end if |
1193 |
| - #end if |
1194 |
| - |
1195 |
| - # Position and orient the camera |
1196 |
| - yaw = yaw_offset + yaw_rate * movie_duration |
1197 |
| - camera_pole_pitch_lerp = 0.5 * (1 + math.cos(camera_pole_rate * movie_duration)) # 0-1 |
1198 |
| - camera_pole_pitch = \ |
1199 |
| - ( |
1200 |
| - camera_pole_pitch_max * camera_pole_pitch_lerp |
1201 |
| - + |
1202 |
| - camera_pole_pitch_min * (1 - camera_pole_pitch_lerp) |
1203 |
| - ) |
1204 |
| - scene.camera.rotation_euler = \ |
1205 |
| - ( |
1206 |
| - 90 * deg - camera_pole_pitch + camera_pole_pitch_offset, |
1207 |
| - 0, |
1208 |
| - yaw |
1209 |
| - ) |
1210 |
| - scene.camera.location = \ |
1211 |
| - ( |
1212 |
| - math.sin(yaw) * camera_pole_length, |
1213 |
| - math.cos(yaw) * -camera_pole_length, |
1214 |
| - math.sin(camera_pole_pitch) * camera_pole_length |
1215 |
| - ) |
1216 |
| - if camera_refocus_object_every_frame : |
1217 |
| - bpy.ops.view3d.camera_to_view_selected() |
1218 |
| - #end if |
1219 |
| - |
1220 |
| - # Render the scene to disk |
1221 |
| - script_path = bpy.context.space_data.text.filepath if bpy.context.space_data else __file__ |
1222 |
| - folder = output_path if output_path else os.path.split(os.path.realpath(script_path))[0] |
1223 |
| - filename = os.path.join \ |
1224 |
| - ( |
1225 |
| - "renders", |
1226 |
| - timestamp, |
1227 |
| - timestamp + "_" + str(frame).zfill(5) + ".png" |
1228 |
| - ) |
1229 |
| - bpy.data.scenes["Scene"].render.filepath = os.path.join(folder, filename) |
1230 |
| - print("Rendering frame " + str(frame) + "...") |
1231 |
| - bpy.ops.render.render(write_still = True) |
1232 |
| - frame += 1 |
1233 |
| - #end while movie_duration < total_movie_duration |
1234 |
| - |
1235 |
| - #end if generate_single_spaceship |
1236 |
| - |
1237 |
| -#end if __name__ == "__main__" |
0 commit comments