acoolrocket

Blender - Selected objects origin points to spawn spotlights

Oct 25th, 2026
24
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import bpy
  2.  
  3. # 1. Create a single shared Spotlight data-block
  4. light_data = bpy.data.lights.new(name="Shared_Ceiling_Spot", type='SPOT')
  5. light_data.energy = 500.0 # Initial power in Watts
  6.  
  7. # 2. Store current selection
  8. selected_objs = bpy.context.selected_objects.copy()
  9.  
  10. # 3. Deselect old objects so only the new lights are selected at the end
  11. bpy.ops.object.select_all(action='DESELECT')
  12.  
  13. # 4. Spawn a linked light at each selected object's transform
  14. for obj in selected_objs:
  15. light_obj = bpy.data.objects.new(name=f"Spot_{obj.name}", object_data=light_data)
  16.  
  17. # Link to the same collection as the target object (or active collection)
  18. if obj.users_collection:
  19. obj.users_collection[0].objects.link(light_obj)
  20. else:
  21. bpy.context.collection.objects.link(light_obj)
  22.  
  23. # Match exact position and rotation
  24. light_obj.matrix_world = obj.matrix_world.copy()
  25. light_obj.select_set(True)
  26.  
  27. # Set the last created light as the active object
  28. if bpy.context.selected_objects:
  29. bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment