RSS

キューブのアニメーション

キューブのアニメーション。Pythonスクリプトで生成。Blender 2.80使用。

ビデオはこちら Random colored cubes.

Pythonスクリプト

import bpy
from random import randint, random

def main():
    
    # delete existing meshes
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete(use_global=False, confirm=False)

    # add plane
    bpy.ops.mesh.primitive_plane_add(location = (0, 0, 0))
    obj = bpy.context.object
    obj.location = (0, 0, -11)
    obj.scale = (1000, 1000, 1)
    
    # add boxes
    for i in range(10):
        add_box(i)

def add_box(i):
    bpy.ops.mesh.primitive_cube_add(location = [randint(-10, 10) for i in range(3)])
    #obj = bpy.context.scene.objects.active
    obj = bpy.context.object
    insert_keyframes(obj, -10, 10)
    add_color(obj, i)

def insert_keyframes(obj, min, max):
    positions = []
    for i in range(10):
        positions.append([randint(-10, 10) for i in range(3)])
        
        frame_num = 0
        for position in positions:
            bpy.context.scene.frame_set(frame_num)
            obj.location = position
            obj.keyframe_insert(data_path = "location", index=-1)
            frame_num += 30
            
def add_color(obj, i):
    mat = bpy.data.materials.new(name = "CubeMaterial" + str(i))
    obj.data.materials.append(mat)
    bpy.context.object.active_material.diffuse_color = (random(), random(), random(), 1.0)
    #bpy.context.object.active_material.specular_color = (random(), random(), random())
    
    #mat.diffuse_intensity = 1.0
#    mat.diffuse_color = (random(), random(), random(), 1.0)

    #mat.specular_intensity = 0.3
#    mat.specular_color = (1.0 ,1.0 ,1.0)

    #mat.emit = 0.0
    #mat.ambient = 0.0
    
            
main()