RSS

メッシュのアニメーション

メッシュのアニメーション。Pythonスクリプトで生成。Blender 2.81a使用。レンダラはEevee。

ビデオはこちら。

Pythonスクリプト

import bpy
import math

meshName = 'Mesh'
objName = 'Object'
mesh = bpy.data.meshes.new(meshName)
obj = bpy.data.objects.new(objName, mesh)
bpy.context.scene.collection.objects.link(obj)

resX = 30
resY = 30

vertices=[]
        
for ix in range(resX):
    for iy in range(resY):
        v = (ix, iy, 2*math.sin(ix*iy*5e-2))
        vertices.append(v)
        

faces=[]

for ix in range(resX - 1):
    for iy in range(resY - 1):
#        face = (ix*resY + iy, (ix+1)*resY + iy, (ix+1)*resY + iy +1, ix*resY + iy + 1)
        face = (ix*resY + iy, (ix+1)*resY + iy, ix*resY + iy + 1)
        faces.append(face)
        face = (ix*resY + iy + 1, (ix+1)*resY + iy, (ix+1)*resY + iy +1)
        faces.append(face)

mesh.from_pydata(vertices, [], faces)
mesh.update()

# set smooth
for f in mesh.polygons:
    f.use_smooth = True

# subdivision surface
bpy.context.view_layer.objects.active = obj
bpy.ops.object.modifier_add(type='SUBSURF')
#bpy.context.object.modifiers['Subsurf'].levels = 4
bpy.context.object.modifiers['Subdivision'].levels = 4

#bpy.context.view_layer.objects.active = bpy.data.objects[objName]
#bpy.ops.object.editmode_toggle()


#bpy.ops.object.mode_set(mode='EDIT')
#bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"mirror":False}, TRANSFORM_OT_translate={"value":(0, 0, -3), "constraint_axis":(False, False, True), "constraint_orientation":'NORMAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False})
#bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value": (0, 0, 1), "constraint_axis": (True, True, True)})

# animation
num_verts = resX*resY 
num_frames = 300

obj_verts = obj.data.vertices

curr_frame = bpy.context.scene.frame_start
for f in range(num_frames):
    bpy.context.scene.frame_set(curr_frame)
    t = f*0.1
    for ix in range(resX):
        for iy in range(resY):
            v = (ix, iy, 2*math.sin(t + ix*iy*5e-2))
            i = ix*resY + iy
            obj_verts_i = obj_verts[i]
            obj_verts_i.co = v
            obj_verts_i.keyframe_insert(data_path='co')
    curr_frame += 1