Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import bpy
- import colorsys
- # Compute the color of a star from its color index:
- # see https://stackoverflow.com/questions/21977786/star-b-v-color-index-to-apparent-rgb-color
- def bv2rgb(bv):
- r,g,b = 0,0,0
- #extrema and out of range values
- if (bv<-0.4):
- bv=-0.4
- if (bv> 2.0):
- bv= 2.0
- #red
- if bv>=-0.40 and bv<0.00:
- t=(bv+0.40)/(0.00+0.40)
- r=0.61+(0.11*t)+(0.1*t*t)
- elif bv>= 0.00 and bv<0.40:
- t=(bv-0.00)/(0.40-0.00)
- r=0.83+(0.17*t)
- elif bv>= 0.40 and bv<2.10:
- t=(bv-0.40)/(2.10-0.40)
- r=1.00
- #green
- if bv>=-0.40 and bv<0.00:
- t=(bv+0.40)/(0.00+0.40)
- g=0.70+(0.07*t)+(0.1*t*t)
- elif bv>= 0.00 and bv<0.40:
- t=(bv-0.00)/(0.40-0.00)
- g=0.87+(0.11*t)
- elif bv>= 0.40 and bv<1.60:
- t=(bv-0.40)/(1.60-0.40)
- g=0.98-(0.16*t)
- elif bv>= 1.60 and bv<2.00:
- t=(bv-1.60)/(2.00-1.60)
- g=0.82-(0.5*t*t)
- #blue
- if bv>=-0.40 and bv<0.40:
- t=(bv+0.40)/(0.40+0.40)
- b=1.00
- elif bv>= 0.40 and bv<1.50:
- t=(bv-0.40)/(1.50-0.40)
- b=1.00-(0.47*t)+(0.1*t*t)
- elif bv>= 1.50 and bv<1.94:
- t=(bv-1.50)/(1.94-1.50)
- b=0.63-(0.6*t*t)
- return [r,g,b]
- #Create one star
- def createBlenderStar(x,y,z, col,mag):
- #create an icosphere
- bpy.context.scene.cursor_location = (x,y,z)
- bpy.ops.mesh.primitive_plane_add(radius=0.2)
- #add the vertex color
- mesh = bpy.context.active_object.data
- vertexColour = mesh.vertex_colors.new()
- faces = mesh.polygons
- for d in mesh.vertex_colors[0].data:
- d.color = col
- #Delete everything but the earth
- """
- bpy.ops.object.select_all(action="SELECT")
- bpy.data.objects["Earth"].select = False
- bpy.ops.object.delete(use_global=False)
- """
- #Path to the csv file
- starFile = "/home/loic/Downloads/hygdata_v3.csv"
- #Read in the 1000 first stars
- reader = csv.DictReader(open(starFile))
- for i, row in enumerate(reader):
- if i<1000:
- x = float(row["x"])
- y = float(row["y"])
- z = float(row["z"])
- mag = float(row["absmag"])
- col = bv2rgb(float(row["ci"])) if row["ci"]!="" else [0.8,0.8,0.8]
- createBlenderStar(x,y,z,col,mag)
- print(i)
- #Assign the emissive/vertex color material
- for o in bpy.data.objects:
- if o.name!="Earth" and o.type=="MESH":
- #assign the material
- mat = bpy.data.materials.get("Material")
- if o.data.materials:
- o.data.materials[0] = mat
- else:
- o.data.materials.append(mat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement