Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created November 3, 2013 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selfsame/7286776 to your computer and use it in GitHub Desktop.
Save selfsame/7286776 to your computer and use it in GitHub Desktop.
def do_magic(context):
lights = []
for o in context.scene.objects:
if o.type == "LAMP":
lights.append(o)
print(lights)
obj = context.active_object
bm = bmesh.new()
mesh = obj.data
bm.from_mesh(mesh)
color = bm.loops.layers.color[0]
for face in bm.faces:
for loop in face.loops:
vcol = loop[color]
vquat = obj.matrix_world.decompose()[1]
v = loop.vert
n = v.normal * obj.matrix_world.inverted()
co = (vquat * v.co) + obj.location #* obj.matrix_world
final = Vector((0,0,0))
for lamp in lights:
lv = lamp.location - co
distance = lv.length / (lamp.data.distance)
energy = lamp.data.energy*10
lcolor = lamp.data.color
intensity = (1/(distance*distance))*energy
d = n.dot(lv)
f = d*(intensity)
if d > 0:
final = final + (Vector((lcolor.r,lcolor.g,lcolor.b)) * f)
lc = Vector((vcol.r,vcol.b,vcol.g))
fc = final * lc
# print("-------")
# print(final)
# print( lc )
#print((final*lc)[0]+":"+(final*lc)[1]+":"+(final*lc)[2])
loop[color] = (final[0], final[1], final[2])
bm.to_mesh(mesh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment