I'm working in Blender 2.8 and I'm new to python and scripting in general.

I have a file that contains multiple lighting fixtures (100+). I'm trying to write a script that will add a spot light to each fixture that I've selected.

Here's what I have so far:

import bpy

selected = bpy.context.selected_objects[0]

bpy.ops.object.light_add(type='SPOT', location=selected.location)

The issue is this script only adds a light to one object, not every object I've selected.

I haven't gotten to it yet, but I'd also like every light to have share the same light data, so they can all be changed at once.

Thanks in advanced for the help!

  • $\begingroup$ You probably need a for loop somewhere in there, along the lines of for item in bpy.context.selected_objects $\endgroup$ Sep 30, 2019 at 14:44
  • $\begingroup$ @DuarteFarrajotaRamos, this helps! The result gave me lights for each selected object, but they all position themselves to a single object. I'm still missing something here. I've noticed if I change the 0 in selected = bpy.context.selected_objects[0] to another number, it changes the object that the lights are added to. $\endgroup$
    – crilly
    Sep 30, 2019 at 14:50
  • $\begingroup$ Well you need to complete it to actually work. Something along the lines of for item in bpy.context.selected_objects bpy.ops.object.light_add(type='SPOT', location=item.location). I'm not much of a coder myself, and I'm not even testing this so not sure it actually works. $\endgroup$ Sep 30, 2019 at 14:57
  • $\begingroup$ Right before you posted that, I tried the same thing! for items in bpy.context.selected_objects: bpy.ops.object.light_add(type='SPOT', location=items.location) does exactly what I need. Way more simple than I thought. Thanks for the help! $\endgroup$
    – crilly
    Sep 30, 2019 at 15:00

1 Answer 1

import bpy

for obj in bpy.data.collections["Collection"].objects.keys():
    light = bpy.context.active_object.name
    bpy.data.objects[light].location = bpy.data.objects[obj].location

This should do the trick, make sure you create a light first (this will control all the other light) and have it selected. Also you want all your light fixture objects in the same collection named "Collection".

  • $\begingroup$ This is the best method so far! You're a life saver. This'll save me so much time in the future. Thank you so much! $\endgroup$
    – crilly
    Sep 30, 2019 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .