import bpy
# 1つ目の球体を作成する
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, enter_editmode=False, location=(-30, 0, 0))
obj1 = bpy.context.active_object
# 1つ目の球体にマテリアルを設定する
mat1 = bpy.data.materials.new(name="SphereMat1")
mat1.use_nodes = True
nodes = mat1.node_tree.nodes
principled_bsdf = nodes.get("Principled BSDF")
principled_bsdf.inputs[0].default_value = (1, 0, 0, 1)
principled_bsdf.inputs[7].default_value = 0.5
obj1.data.materials.append(mat1)
# 1つ目の球体を移動する
frames = 40 * bpy.context.scene.render.fps
for i in range(frames):
if obj1.location[0] < 0:
obj1.location = ((60 / frames) * i - 30, 0, 0)
else:
obj1.location = (30, 0, 0)
obj1.keyframe_insert(data_path="location", frame=i)
# 2つ目の球体を作成する
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, enter_editmode=False, location=(30, 0, 0))
obj2 = bpy.context.active_object
# 2つ目の球体にマテリアルを設定する
mat2 = bpy.data.materials.new(name="SphereMat2")
mat2.use_nodes = True
nodes = mat2.node_tree.nodes
principled_bsdf = nodes.get("Principled BSDF")
principled_bsdf.inputs[0].default_value = (1, 0, 0, 1)
principled_bsdf.inputs[7].default_value = 0.5
obj2.data.materials.append(mat2)
# 2つ目の球体を移動する
for i in range(frames):
if obj2.location[0] > 0:
obj2.location = ((-60 / frames) * i + 30, 0, 0)
else:
obj2.location = (-30, 0, 0)
obj2.keyframe_insert(data_path="location", frame=i)
# 3つ目の球体を作成する
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, enter_editmode=False, location=(0, 0, 0))
obj3 = bpy.context.active_object
# 3つ目の球体にマテリアルを設定する
mat3 = bpy.data.materials.new(name="SphereMat3")
mat3.use_nodes = True
nodes = mat3.node_tree.nodes
principled_bsdf = nodes.get("Principled BSDF")
principled_bsdf.inputs[0].default_value = (0, 1, 0, 1)
principled_bsdf.inputs[7].default_value = 0.5
obj3.data.materials.append(mat3)
# 3つ目の球体を移動する
for i in range(frames):
if obj1.location[0] > 0 or obj2.location[0] < 0:
obj3.location = (0, 0, 10)
else:
obj3.location = (0, 0, 0)
obj3.keyframe_insert(data_path="location", frame=i)
bbb