Blender 4.1, Python 立方体拡大縮小
以前の記事「Python 立方体 拡大縮小」を書き直しました。blender 4.1.1向けです
主な修正点は
(1) "bpy.context.scene.frame_current" をやめて bpy.context.scene.frame_set(1) という方法にした。
資料は ホロモン氏「MRが楽しい」 本文中「 frame_current の変更は即時反映されないため」を参考にしています。
(2)照明の新規設置のやり方を変えました。
資料 StackExchange "How to create a Light with the Python API in Blender 2.92" brockmann 回答例。
# bpy_nh40 2024 0505 立方体 拡大縮小 (カメラ移動。照明1個移動 )
# ===動画 1sec, 141kB、FFmpeg video, [h264 in mp4]
import bpy
bpy.context.scene.render.filepath = "/Users/nh7/Movies/BlenderHikoArt/" #Output folder
# 既存 mesh, light, camera, みな削除
for item in bpy.data.objects:
bpy.data.objects.remove(item)
#立方体 original script は ---- http://tips.hecomi.com/entry/20120818/1345307205
#拡大参考資料 tatsuruM Qiita https://qiita.com/tatsuruM/items/9d4222c6c7d96b4c5b3c
N = 4
# 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),size = (1.2) )
bpy.ops.transform.resize(value=(0.8, 0.1, 2.0))
bpy.ops.transform.rotate(value= -3.1415/6 ,orient_axis='X')
obj = bpy.context.view_layer.objects.active
mat = bpy.data.materials.new('Cube')
mat.diffuse_color = (x/N, y/N, z/N, 0)
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))
# =========== camera movement (modified)
bpy.data.objects['Camera'].select_set(True)
cdnow = bpy.context.scene.objects["Camera"] # get ACTIVE object
bpy.context.scene.frame_set(1) # set frame to 1
cdnow.location = (20.0, -6.0, 2.0) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_set(10) # set frame to 10
cdnow.location = (20.0, -4.0, 9.0) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_set(20) # set frame to 20
cdnow.location = (15.0, -2.0, 10.0) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.data.objects['Camera'].select_set(False)
# ============= end of camera movement
# ======== one point
# light StackExchange "How to create a Light with the Python API in Blender 2.92" brockmann 回答例
# Create light datablock
light_data = bpy.data.lights.new(name="my-light-data", type='POINT')
light_data.energy = 2000
# Create new object, pass the light data
light_object = bpy.data.objects.new(name="my-light", object_data=light_data)
# Link object to collection in context
bpy.context.collection.objects.link(light_object)
# Change light position
light_object.location = (5, -4, -2)
# ====== light movement スポット照明移動 frame_set(1)を使用
bpy.data.objects['my-light'].select_set(True)
lightnow = bpy.context.scene.objects["my-light"] # get ACTIVE object
bpy.context.scene.frame_set(1) # set frame to 1
lightnow.location = (5, -4, -2) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
bpy.context.scene.frame_set(20) # set frame to 20
lightnow.location = (10, -2, 8) # set the location
bpy.ops.anim.keyframe_insert_menu(type='Location') # KEY FRAME
# ====== end of light movement
# ========= 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
#=====
#=== end of python script