Advertisement
Guest User

Import stars in blender

a guest
May 26th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import csv
  2. import bpy
  3. import colorsys
  4.  
  5. # Compute the color of a star from its color index:
  6. # see https://stackoverflow.com/questions/21977786/star-b-v-color-index-to-apparent-rgb-color
  7. def bv2rgb(bv):
  8.     r,g,b = 0,0,0
  9.     #extrema and out of range values
  10.     if (bv<-0.4):
  11.         bv=-0.4
  12.     if (bv> 2.0):
  13.         bv= 2.0
  14.     #red
  15.     if bv>=-0.40 and bv<0.00:
  16.         t=(bv+0.40)/(0.00+0.40)
  17.         r=0.61+(0.11*t)+(0.1*t*t)
  18.     elif bv>= 0.00 and bv<0.40:
  19.         t=(bv-0.00)/(0.40-0.00)
  20.         r=0.83+(0.17*t)
  21.     elif bv>= 0.40 and bv<2.10:
  22.         t=(bv-0.40)/(2.10-0.40)
  23.         r=1.00
  24.     #green
  25.     if bv>=-0.40 and bv<0.00:
  26.         t=(bv+0.40)/(0.00+0.40)
  27.         g=0.70+(0.07*t)+(0.1*t*t)
  28.     elif bv>= 0.00 and bv<0.40:
  29.         t=(bv-0.00)/(0.40-0.00)
  30.         g=0.87+(0.11*t)
  31.     elif bv>= 0.40 and bv<1.60:
  32.         t=(bv-0.40)/(1.60-0.40)
  33.         g=0.98-(0.16*t)
  34.     elif bv>= 1.60 and bv<2.00:
  35.         t=(bv-1.60)/(2.00-1.60)
  36.         g=0.82-(0.5*t*t)
  37.     #blue
  38.     if bv>=-0.40 and bv<0.40:
  39.         t=(bv+0.40)/(0.40+0.40)
  40.         b=1.00
  41.     elif bv>= 0.40 and bv<1.50:
  42.         t=(bv-0.40)/(1.50-0.40)
  43.         b=1.00-(0.47*t)+(0.1*t*t)
  44.     elif bv>= 1.50 and bv<1.94:
  45.         t=(bv-1.50)/(1.94-1.50)
  46.         b=0.63-(0.6*t*t)
  47.     return [r,g,b]
  48.  
  49. #Create one star
  50. def createBlenderStar(x,y,z, col,mag):
  51.     #create an icosphere
  52.     bpy.context.scene.cursor_location = (x,y,z)
  53.     bpy.ops.mesh.primitive_plane_add(radius=0.2)
  54.     #add the vertex color
  55.     mesh = bpy.context.active_object.data
  56.     vertexColour = mesh.vertex_colors.new()
  57.     faces = mesh.polygons
  58.     for d in mesh.vertex_colors[0].data:
  59.         d.color = col
  60.  
  61. #Delete everything but the earth
  62. """
  63. bpy.ops.object.select_all(action="SELECT")
  64. bpy.data.objects["Earth"].select = False
  65. bpy.ops.object.delete(use_global=False)
  66. """
  67.  
  68. #Path to the csv file
  69. starFile = "/home/loic/Downloads/hygdata_v3.csv"
  70.  
  71. #Read in the 1000 first stars
  72. reader = csv.DictReader(open(starFile))
  73. for i, row in enumerate(reader):
  74.     if i<1000:
  75.         x = float(row["x"])
  76.         y = float(row["y"])
  77.         z = float(row["z"])
  78.         mag = float(row["absmag"])
  79.         col = bv2rgb(float(row["ci"])) if row["ci"]!="" else [0.8,0.8,0.8]
  80.         createBlenderStar(x,y,z,col,mag)
  81.         print(i)
  82.  
  83. #Assign the emissive/vertex color material
  84. for o in bpy.data.objects:
  85.     if o.name!="Earth" and o.type=="MESH":
  86.         #assign the material
  87.         mat = bpy.data.materials.get("Material")
  88.         if o.data.materials:
  89.             o.data.materials[0] = mat
  90.         else:
  91.             o.data.materials.append(mat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement