Guest User

Blender 2.8+ Add Target Spot

a guest
Jan 24th, 2020
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. import bpy
  2.  
  3. def draw(self, context):
  4.     self.layout.operator("object.add_target_spot", icon="LIGHTPROBE_CUBEMAP")
  5.  
  6. class OBJECT_OT_add_target_spot(bpy.types.Operator):
  7.     """Tooltip"""
  8.     bl_idname = "object.add_target_spot"
  9.     bl_label = "Target Spot"
  10.  
  11.     def execute(self, context):
  12.         #bpy.ops.object.light_add(type="SPOT", radius=1)
  13.         root_obj = bpy.data.objects.new("spot_root", None) # create the empty
  14.         root_obj.empty_display_size = 2.0
  15.         root_obj.empty_display_type = 'SPHERE'
  16.        
  17.         light = bpy.data.objects.new("spot", bpy.data.lights.new("spot", "SPOT"))
  18.         light.lock_location = (True, True, True)
  19.  
  20.         light.parent = root_obj
  21.        
  22.         target_obj = bpy.data.objects.new("spot_target", None) # create the empty
  23.         target_obj.location = (0, 0, -27)
  24.         target_obj.parent = root_obj
  25.         target_obj.empty_display_size = 1.0
  26.         target_obj.empty_display_type = 'CUBE'
  27.        
  28.         constraint = light.constraints.new("DAMPED_TRACK")
  29.         constraint.track_axis = 'TRACK_NEGATIVE_Z'
  30.         constraint.target = target_obj
  31.  
  32.         bpy.context.layer_collection.collection.objects.link(root_obj)
  33.         bpy.context.layer_collection.collection.objects.link(target_obj)
  34.         bpy.context.layer_collection.collection.objects.link(light)
  35.        
  36.         for o in bpy.data.objects:
  37.             o.select_set(False)
  38.         root_obj.select_set(True)
  39.         root_obj.location = context.scene.cursor.location
  40.         bpy.context.view_layer.objects.active = root_obj
  41.         return {'FINISHED'}
  42.  
  43. def register():
  44.     bpy.utils.register_class(OBJECT_OT_add_target_spot)
  45.     bpy.types.VIEW3D_MT_light_add.append(draw)
  46.  
  47.  
  48. def unregister():
  49.     bpy.utils.unregister_class(OBJECT_OT_add_target_spot)
  50.     bpy.types.VIEW3D_MT_light_add.remove(draw)
  51.  
  52.  
  53. if __name__ == "__main__":
  54.     register()
Advertisement
Add Comment
Please, Sign In to add comment