Created
May 20, 2020 10:57
-
-
Save shspage/dcfb0f1e9167438a732c4a8c62d28d59 to your computer and use it in GitHub Desktop.
blenderで形状をアニメーションさせる簡単な例。Run Script してからTimelineで再生 (blender 2.82a)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from mathutils import Vector, Matrix | |
import math | |
class MyScene: | |
_freq = 3 | |
_cube_count = 20 | |
_scale = 0.1 | |
_radius = 2 | |
_rotation_x = math.radians(4) | |
_rotation_z = math.radians(1) | |
def __init__(self): | |
self.cube = None | |
self.cubes = [] | |
self.resetScene() | |
def resetScene(self): | |
for obj in bpy.data.objects: | |
if obj.name.startswith("Cube"): | |
bpy.data.objects.remove(obj) | |
M = MyScene | |
bpy.ops.mesh.primitive_cube_add() | |
self.cube = bpy.context.object | |
self.cube.scale *= M._scale | |
# Move the object. Its origin stays in place. | |
self.cube.data.transform(Matrix.Translation(Vector((0,0, M._radius / M._scale)))) | |
self.cube.hide_set(True) | |
def my_handler(self, *args): | |
scene = args[0] | |
M = MyScene | |
fc = scene.frame_current | |
if fc % M._freq == 0 and len(self.cubes) < M._cube_count: | |
cb = self.cube.copy() | |
cb.hide_set(False) | |
bpy.data.collections["Collection"].objects.link(cb) | |
self.cubes.append(cb) | |
for cb in self.cubes: | |
cb.rotation_euler.x += M._rotation_x | |
cb.rotation_euler.z += M._rotation_z | |
bpy.app.handlers.frame_change_pre.clear() | |
bpy.context.scene.frame_set(0) | |
myscene = MyScene() | |
bpy.app.handlers.frame_change_pre.append(myscene.my_handler) | |
print("----") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment