Blender 2.8, Python, light スポット照明

今回試したのは、照明の角度指定と動き。1秒間の動画で投稿。
動画はtwitter投稿の
[blender 2.8,  python 動画1秒。 点光源と、スポット光源](https://twitter.com/naohiko7/status/1297175807127769091)
 です。スポット光源は照らす方向も大切。少々変な部分も含めて全部スクリプト公開。
スポット照明はデフォルトで地面向き、つまりz軸のマイナス方向。（ (0,0,0)で真下向き。）角度指定の数字は今回の delta_rotation_euler = (x,y,z)を使うとxの数値で、x軸周りの回転、角度は原点からx軸プラスを見たときの時計回り。xに(3.14/2)とすると 90度でy軸のプラス（北）方向。約3.14 = πで180度 。2πで360度。例(3.14, 0, -0)で空をむく。 y数値で、y軸周りの回転、原点からy軸プラスを見たときの時計回り。 例(0, 3.14/2, 0)でxマイナスの向きつまり西向き。例(0, 3.14/4, 3.14/4)ではやや下、西南向き、になる。オイラー角の理解がz軸あたり半分怪しいけどひとまずこれでよし。
（アニメーションでスポット照明角度を変えるのは次回課題。）
スクリプトの引用元
借りてきたスクリプトもたくさん増えたので
◎立方体は[凹みTips Blender 2.63 での Python の使い方...](http://tips.hecomi.com/entry/20120818/1345307205)
◎ランプの新規設置は　[ 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氏の回答欄。
今回のスクリプトはbpy_nh09とします。
![bpy_nh09sc_shot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/690216/dcac7ad2-b75e-e65f-a957-5a492202e219.png)



```python3
import bpy
# nh09   SPOT照明 動かす。複数照明に個別に名前つけて座標移動、。

# 既存 mesh, light, camera, みな削除
for item in bpy.data.objects:
	bpy.data.objects.remove(item)

#立方体 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 #(この行は2.8で試していない)
            #mat.alpha = 0.6 #(この行は2.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 （出典 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) #ゼロゼロゼロで真下を向く。
# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()
# ================

# ================
#  MOVE a LIGHT  mesh with KEY FRAME ポイント照明の移動
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  スポット 照明の移動
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 (背景) 
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            
#=====

```