Blender 2.8, Python, light Spot lighting

What I tried this time was the angle specification and movement of the lighting. Post with a 1 second video. The video is posted on twitter blender 2.8, python video 1 second. Point light source and spot light source is. The direction in which the spot light source is illuminated is also important. All scripts are released, including some strange parts. Spot lighting defaults to the ground, that is, in the minus direction of the z-axis. ((0,0,0) points directly downward.) The angle specification number is the value of x using this delta_rotation_euler = (x, y, z), the rotation around the x-axis, and the angle is the x-axis plus from the origin. Clockwise when looking at. If x is (3.14 / 2), it is 90 degrees and is in the plus (north) direction of the y-axis. Approximately 3.14 = 180 degrees at π. 360 degrees at 2π. Peel the sky with the example (3.14, 0, -0). Rotation around the y-axis with y-value, clockwise when looking at the y-axis plus from the origin. In the example (0, 3.14 / 2, 0) x minus direction, that is, west direction. In the example (0, 3.14 / 4, 3.14 / 4), it is slightly downward, facing southwest. Understanding Euler angles is half suspicious per z-axis, but this is fine for the time being. (The next task is to change the spot lighting angle with animation.) Source of script I borrowed a lot of scripts, so ◎ The cube is How to use Python with Recess Tips Blender 2.63 ... ◎ New installation of lamp is [stack overflow; Can you add a light source in blender using python](https://stackoverflow.com/questions/17355617/can-you-add-a-light-source-in-blender- using-python), onorabil's answer column. The script this time is bpy_nh09. bpy_nh09sc_shot.png

import bpy
#nh09 SPOT Lighting Move. Name multiple lights individually and move coordinates ,.

#Existing mesh, light, camera,Delete all
for item in bpy.data.objects:
	bpy.data.objects.remove(item)

#Cube original script---- http://tips.hecomi.com/entry/20120818/1345307205

START = 0
END   = 100
N     = 3

# Add color cubes
for x in range(0, N):
    for y in range(0, N):
        for z in range(0, N):
            # Add a color cube
            bpy.ops.mesh.primitive_cube_add( location=(x*3, y*3, z*3) )
            #obj = bpy.context.scene.objects.active #(old blender2.7script)
            obj = bpy.context.view_layer.objects.active
            #mat.diffuse_color = (x/N, y/N, z/N)#(old blender2.7script)
            mat = bpy.data.materials.new('Cube')
            mat.diffuse_color = (x/N, y/N, z/N, 0)
            #mat.use_transparency = True #(This line is 2.Not tried in 8)
            #mat.alpha = 0.6 #(This line is 2.Not tried in 8)
            obj.data.materials.append(mat)

# new camera
bpy.ops.object.add(radius=1.0, type='CAMERA', enter_editmode=False, align='WORLD', location=(20.0, -6.0, 8.0), rotation=(1.4, 0.0, 1.2))

#new lamps (source stack overflow Can you add a light source in blender using python)
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_2.80", type='POINT')
light_data.energy = 1000
# create new object with our light datablock
light_object = bpy.data.objects.new(name="light_2.80", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object)
# make it active 
bpy.context.view_layer.objects.active = light_object
#change location
light_object.location = (5, -4, 10)
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()
# ============== "light_spot1"
# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_spot1", type='SPOT')
light_data.energy = 3000
# create new object with our light datablock
light_object1 = bpy.data.objects.new(name="light_spot1", object_data=light_data)
# link light object
bpy.context.collection.objects.link(light_object1)
# make it active 
bpy.context.view_layer.objects.active = light_object1
#change location
light_object1.location = (5, -5, 10)
light_object1.delta_rotation_euler = (1.2, 0, -0.3) #Look straight down at zero zero zero.
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()
# ================

# ================
#MOVE a LIGHT mesh with KEY FRAME Point lighting movement
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 30
bpy.context.scene.frame_current = 10

# make POINT  light ACTIVE 

bpy.context.scene.objects["light_spot1"].select_set(False) #spot ---- deselect
bpy.context.scene.objects["light_2.80"].select_set(True) # point ----- select

#cdnow  = bpy.context.object          # get ACTIVE object 
cdnow  = bpy.context.scene.objects["light_2.80"]    # get ACTIVE object 
# print(cdnow) #------- DDD debugging DDDD

bpy.context.scene.frame_current = 1 # set frame to 1
cdnow.location = (1,-10,1) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME

bpy.context.scene.frame_current = 10 # set frame to 10
cdnow.location = (1,-10,8) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME

bpy.context.scene.frame_current = 20 # set frame to 20
cdnow.location = (8,-5,8)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME

bpy.context.scene.frame_current = 30 # set frame to 30
cdnow.location = (1,-10,1)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
#  ==================

# ================
#MOVE a SPOT LIGHT Spot lighting movement
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 30
bpy.context.scene.frame_current = 10

# make light ACTIVE 
bpy.context.scene.objects["light_2.80"].select_set(False) # point ----- deselect
bpy.context.scene.objects["light_spot1"].select_set(True) #spot ---- select

cdnow  = bpy.context.object          # get ACTIVE object 
bpy.context.scene.frame_current = 1 # set frame to 1
cdnow.location = (5, -5, 8) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME

bpy.context.scene.frame_current = 10 # set frame to 10
cdnow.location = (5, -5, 13) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME

bpy.context.scene.frame_current = 20 # set frame to 20
cdnow.location = (2, -5, 13)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME

bpy.context.scene.frame_current = 30 # set frame to 30
cdnow.location = (5, -5, 8)
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
#  ==================

# world - surface - background (background) 
bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[0].default_value = (0.01, 0.15, 0.25, 1)
bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[1].default_value = 0.7            
#=====

Recommended Posts

Blender 2.8, Python, light Spot lighting
Blender 2.9, Python background light color test
Blender 2.9 Python Extrude extrude
blender, python, spiral staircase
Run Blender with python
blender, python, sphere behavior
Blender 2.8, Python Cube Scaling
Operate Blender with Python
[Blender x Python] Blender Python tips (11/100)
Light blue with AtCoder @Python
Blender 2.9, Python Buildings, Cars, Videos
blender, python, spiral staircase, color
WIFI spot scan in Python
Blender 2.9, Python Odd Even Building
Blender 2.9, Python, hexadecimal color specification
Blender Python API in Houdini (Python 3)
Generate 8 * 8 (64) cubes in Blender Python
Draw Sine Waves in Blender Python
Run mruby with Python or Blender
Install Pytorch on Blender 2.90 python on Windows
[Blender x Python] Particle Animation (Part 1)
Use blender as a python module
Implementation of Light CNN (Python Keras)
[Blender x Python] Let's master random !!
[Blender x Python] Let's master rotation !!
Access Blender Shader Nodes from Python
Easy modeling with Blender and Python
Get started with Python in Blender