# Adding lights to selected objects with python?

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]



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!

• You probably need a for loop somewhere in there, along the lines of for item in bpy.context.selected_objects – Duarte Farrajota Ramos Sep 30 '19 at 14:44
• @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. – crilly Sep 30 '19 at 14:50
• 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. – Duarte Farrajota Ramos Sep 30 '19 at 14:57
• 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! – crilly Sep 30 '19 at 15:00

import bpy