Created
May 13, 2021 20:21
-
-
Save ksenobojca/90901d9c4893dadf29c9e518978ab96d to your computer and use it in GitHub Desktop.
NASA asteroids in Blender 2.92
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import bmesh; | |
import random, math | |
import time | |
# Obsługa bardzo słabego formatu z NASA... | |
def split(line, breaks): | |
return [line[s:e-1].strip() for s, e in zip(breaks, breaks[1:])] | |
def extract_lines(path): | |
with open(path) as file: | |
header = next(file) | |
dashes = next(file) | |
breaks = [0] | |
for field in dashes.strip().split(' '): | |
breaks.append(breaks[-1] + len(field) + 1) | |
header = split(header, breaks) | |
assert header == ['Num', 'Name', 'Epoch', 'a', | |
'e', 'i', 'w', 'Node', 'M', 'H', 'G', 'Ref'] | |
for line in file: | |
tab = split(line, breaks) | |
assert len(tab) == len(header) | |
Num, Name, Epoch, a, e, i, w, Node, M, H, G, Ref = tab | |
yield float(a), float(i) | |
# wczytujemy dane z pliku | |
start = time.time() | |
# data info: https://ssd.jpl.nasa.gov/?sb_elem | |
# direct download: https://ssd.jpl.nasa.gov/dat/ELEMENTS.NUMBR.gz | |
# uncompress and put path below: | |
path = r'E:\astro\ELEMENTS.NUMBR' | |
lista = [] | |
for a, i in extract_lines(path): | |
alpha = random.random()*2*math.pi | |
i_rad = math.radians(i) | |
x = a * math.sin(alpha) | |
y = a * math.cos(alpha) | |
z_amp = a * math.sin(i_rad) | |
# z = z_amp * 0.5 * (1 if random.random() > 0.5 else -1) # wersja z filmu | |
z = z_amp * (random.random() * 2 - 1) # to chyba blizsze prawdy | |
# z = z * 0.25 # na moje oko wizualnie lepiej przerwy widać... | |
lista.append((x, y, z)) | |
if len(lista) > 20000: # wywalic, zeby zaladowac calosc | |
break | |
print('czas wczytywania:', time.time() - start) | |
# rysujemy szybkim sposobem | |
start = time.time() | |
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=2, radius=0.01) | |
orig_cube = bpy.context.active_object; | |
bm = bmesh.new(); | |
for loc in lista: | |
bm.verts.new().co=loc; | |
bpy.ops.mesh.primitive_plane_add(); | |
o = bpy.context.active_object; | |
bm.to_mesh(o.data); | |
o.instance_type = 'VERTS'; | |
orig_cube.parent = o; | |
print('czas tworzenia:', time.time() - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment