3

I'm using a Python script to generate a star-field using data from ~9k of the brightest stars (data is taken from here) Spheres are generated equidistant from the camera (eventually these will be filled circle billboards), and I'm hoping to adjust their magnitudes using an emission shader.

Is there a way to adjust the emission strength of each individual sphere without creating 9000+ separate textures?

My first run at the code was able to change the emission strength of the material, but it's changing it for all of the spheres each time. The code is below:

...
# Load star data from file
starData = np.loadtxt('/stars/catalog_text')
distanceMultiplier = 1000 * 5

# Create base stare
star1 = Star(starData[0])
starMesh = star1.getMesh()

# Duplicate mesh for every star in the data
i = 0
for star in starData:
    i += 1

    # Set Location variables from data
    magnitude = star[2]
    x = star[3] * distanceMultiplier
    y = star[4] * distanceMultiplier
    z = star[5] * distanceMultiplier

    # Set star magnitude from data
    # (convertMagnitude maps the magnitude value to an emission strength value in Blender) 
    emission = bpy.context.active_object.active_material.node_tree.nodes.get("Emission")
emission.inputs[1].default_value = convertMagnitude(magnitude)

    # Create a copy of our base star
    newOb = starMesh.copy()

    # Assign unique name and location to mesh
    newOb.name = "star_" + str(i)
    newOb.location = (x, y, z)

    # Link mesh to the scene
    bpy.context.scene.objects.link(newOb)
bpy.context.scene.update()
nfojunky
  • 33
  • 3
  • Do the stars' materials need to have the texture? – someonewithpc Feb 04 '15 at 20:59
  • you may group the stars that have the same emission strength in group than set the emission to that value render only this group than move to the next group when done combine the result in the compositor – Chebhou Feb 04 '15 at 21:32
  • This will help: http://blender.stackexchange.com/questions/23417/change-color-with-object-rotation/23429#23429 also you will find there how to assign those vertex colors with python – Jaroslav Jerryno Novotny Feb 05 '15 at 23:49

2 Answers2

2

There currently appears to be no way to have one material with different settings for each object using it. While an object knows what materials it uses, a material has no link back to the object using it, as a single material may be used by many objects there is also the trouble of choosing which object is currently active.

I see two ways to tackle your problem, one material for each object or a range of materials to be shared across objects. I think a range of materials with varying emission strengths could be generated and one chosen as each object is created. I'm not sure of the range of values in your data, but expect that emission values could be averaged out a bit so that magnitudes within a range could use the same material.

if magnitude >= 100 and magnitude < 200
    obj.material_slots[0] = bpy.data.materials['mag150']
sambler
  • 55,387
  • 3
  • 59
  • 192
2

here is a quick solution : use this node setup

enter image description here

set the pass index of the star to its emission brightness

enter image description here

and here is the result :

enter image description here

Chebhou
  • 19,533
  • 51
  • 98
  • 1
    This worked, thanks!

    I knew something like this had to be possible - I was using almost exactly the same setup using "random" to fake the magnitude.

    – nfojunky Feb 07 '15 at 19:31