# Shredder animation for ornament_machine_new_bow.blend. # Run: blender -b ornament_machine_new_bow.blend --python animate_shredder.py [-- --video] # Result is saved as ornament_machine_new_bow_anim.blend (the source file is not touched). # # Timeline (24 fps): # 1-14 drums spin up # 14-120 shards drop in from the hopper, get dragged down by the drums, shrink to grit that piles on the floor # 120-142 drums slow down and stop, grit drains away # 128-180 shards pour out of the output chute onto the tray, bounce and settle import bpy, math, os, random, sys from mathutils import Vector, Matrix, Euler random.seed(12) sc = bpy.context.scene FPS = 24 sc.render.fps = FPS sc.frame_start, sc.frame_end = 1, 180 SPIN_UP, STOP_START, STOP_END = 14, 120, 142 SPEED = 2 * math.pi * 1.1 / FPS # drum speed: 1.1 rev/s src_path = bpy.data.filepath out_path = os.path.splitext(src_path)[0] + "_anim.blend" def bbox(o): cs = [o.matrix_world @ Vector(c) for c in o.bound_box] lo = Vector(map(min, *cs)); hi = Vector(map(max, *cs)) return (lo + hi) / 2, hi - lo def key(ob, path, frame, value=None, interp=None, easing=None): if value is not None: setattr(ob, path, value) ob.keyframe_insert(data_path=path, frame=frame) if interp: fc = ob.animation_data.action # newest keyframes on this path get the requested interpolation for c in fcurves_of(ob): if c.data_path == path: for kp in c.keyframe_points: if abs(kp.co.x - frame) < 0.01: kp.interpolation = interp if easing: kp.easing = easing def fcurves_of(ob): act = ob.animation_data.action if ob.animation_data else None if act is None: return [] try: return list(act.fcurves) except AttributeError: # Blender 5.x slotted/layered actions out = [] for layer in act.layers: for strip in layer.strips: for bag in strip.channelbags: out += list(bag.fcurves) return out # ------------------------------------------------------------------ chamber geometry from the model floor_c, _ = bbox(bpy.data.objects["Rec_Chamber_Floor"]) ceil_c, _ = bbox(bpy.data.objects["Rec_Chamber_Ceiling"]) glass_c, _ = bbox(bpy.data.objects["Rec_Window_Glass"]) tray_c, tray_s = bbox(bpy.data.objects["Rec_Tray_Floor"]) chute_ceil_c, _ = bbox(bpy.data.objects["Rec_Chute_Ceiling"]) chute_back_c, _ = bbox(bpy.data.objects["Rec_Chute_BackWall"]) FLOOR_Z = floor_c.z + 0.08 CEIL_Z = ceil_c.z - 0.08 FRONT_Y = glass_c.y + 0.2 # ------------------------------------------------------------------ drums: pivot empties on each drum axis drum_parts = [o for o in bpy.data.objects if o.type == "MESH" and o.name.startswith("Rec_Drum")] cores = sorted([o for o in drum_parts if o.name.startswith("Rec_Drum_Core")], key=lambda o: bbox(o)[0].x) pivots = [] for i, core in enumerate(cores): c, s = bbox(core) e = bpy.data.objects.new("Rec_Drum_Pivot_%s" % ("L" if i == 0 else "R"), None) e.empty_display_type = "ARROWS"; e.empty_display_size = 0.8 e.location = c sc.collection.objects.link(e) pivots.append((e, c, s)) bpy.context.view_layer.update() DRUM_Y, DRUM_Z = pivots[0][1].y, pivots[0][1].z DRUM_R = max(bbox(o)[1].z for o in drum_parts) / 2 for o in drum_parts: c, _ = bbox(o) e = min(pivots, key=lambda p: abs(p[1].x - c.x))[0] mw = o.matrix_world.copy() o.parent = e o.matrix_parent_inverse = e.matrix_world.inverted() o.matrix_world = mw DRUM_X = [p[1].x for p in pivots] DRUM_HALF = max(p[2].x for p in pivots) / 2 # spin: ease-in start, constant speed, ease-out stop (opposite directions so the pair reads as a crusher) for idx, (e, c, s) in enumerate(pivots): d = 1 if idx == 0 else -1 spin_end = SPEED * (STOP_START - SPIN_UP) + SPEED * SPIN_UP / 2 e.rotation_euler = (0, 0, 0) key(e, "rotation_euler", 1, (0, 0, 0), "QUAD", "EASE_IN") key(e, "rotation_euler", SPIN_UP, (d * SPEED * SPIN_UP / 2, 0, 0), "LINEAR") key(e, "rotation_euler", STOP_START, (d * spin_end, 0, 0), "QUAD", "EASE_OUT") key(e, "rotation_euler", STOP_END, (d * (spin_end + SPEED * (STOP_END - STOP_START) / 2), 0, 0)) # a little vibration while grinding e.location = c key(e, "location", SPIN_UP, c.copy()) key(e, "location", STOP_START, c.copy()) for idx, (e, c, s) in enumerate(pivots): for fc in fcurves_of(e): if fc.data_path == "location" and fc.array_index == 2: nm = fc.modifiers.new("NOISE"); nm.scale = 2.0; nm.strength = 0.05 nm.use_restricted_range = True; nm.frame_start, nm.frame_end = SPIN_UP, STOP_START nm.blend_in, nm.blend_out = 6, 10 # ------------------------------------------------------------------ crystal prototypes floor_crystals = [o for o in bpy.data.objects if o.name.startswith("Rec_CrusherCrystal")] proto = floor_crystals[0] coll = bpy.data.collections.get("Shredder_Anim") or bpy.data.collections.new("Shredder_Anim") if coll.name not in sc.collection.children: sc.collection.children.link(coll) def new_crystal(name, scale): o = bpy.data.objects.new(name, proto.data) coll.objects.link(o) o.scale = (scale, scale, scale) return o def hide_until(o, frame, full_scale): key(o, "scale", 1, (0, 0, 0), "CONSTANT") key(o, "scale", frame - 1, (0, 0, 0), "LINEAR") key(o, "scale", frame + 1, (full_scale,) * 3) def set_origin_to_center(o): """Crystals were built in world space with the origin at 0; re-centre so scale/rotation act on the crystal.""" c, _ = bbox(o) if (o.location - c).length > 0.01 and o.data.users == 1: o.data.transform(Matrix.Translation(-(o.matrix_world.inverted() @ c))) o.location = c return c # the original floor crystals become the grit that builds up while grinding, then drains away proto_center = set_origin_to_center(proto) grit = [] for k, o in enumerate(floor_crystals): if o is not proto: if o.data.users > 1: o.data = o.data.copy() set_origin_to_center(o) grit.append(o) proto_size = bbox(proto)[1].length # from here on new crystals reuse the (now centred) proto mesh # ------------------------------------------------------------------ shards dropping in and being ground N_SHARDS = 18 for i in range(N_SHARDS): s = SPIN_UP + 4 + i * 5.6 x = random.choice(DRUM_X) + random.uniform(-DRUM_HALF * 0.7, DRUM_HALF * 0.7) sc0 = random.uniform(1.1, 1.5) o = new_crystal("Shard_%02d" % i, sc0) o.location = (x, DRUM_Y + DRUM_R * 0.6, CEIL_Z - 0.15) hide_until(o, int(s), sc0) path = [ # frame offset, position, scale factor (0, (x, DRUM_Y + DRUM_R * 0.55, CEIL_Z - 0.15), 1.0), (5, (x, DRUM_Y - DRUM_R * 0.3, DRUM_Z + DRUM_R + 0.08), 0.95), # lands on top of the drum (11, (x + random.uniform(-0.1, 0.1), DRUM_Y - DRUM_R - 0.22, DRUM_Z + DRUM_R * 0.3), 0.6), # dragged over the front (16, (x, DRUM_Y - DRUM_R - 0.25, DRUM_Z - DRUM_R * 0.5), 0.3), # crushed (20, (x, DRUM_Y - DRUM_R * 0.6, FLOOR_Z + 0.1), 0.0), # gone to grit ] for off, pos, f in path: key(o, "location", int(s) + off, Vector(pos)) key(o, "scale", int(s) + off, (sc0 * f,) * 3) rot0 = Euler([random.uniform(0, 6.28) for _ in range(3)]) key(o, "rotation_euler", int(s), rot0) key(o, "rotation_euler", int(s) + 20, Euler([a + random.uniform(4, 9) for a in rot0])) # grit appears bit by bit on the floor, then drains away after the drums stop for k, o in enumerate(grit): full = o.scale[0] t = SPIN_UP + 28 + k * (STOP_START - SPIN_UP - 34) / max(1, len(grit)) hide_until(o, int(t), full) key(o, "scale", STOP_END - 4, (full,) * 3) key(o, "scale", STOP_END + 4 + k % 4, (0, 0, 0)) # chamber light pulses while grinding lt = bpy.data.objects.get("Rec_Chamber_Light") if lt: base = lt.data.energy for f, e in ((1, base), (SPIN_UP, base * 1.8), (STOP_START, base * 1.8), (STOP_END, base)): lt.data.energy = e; lt.data.keyframe_insert("energy", frame=f) act = lt.data.animation_data.action for fc in fcurves_of(lt.data): nm = fc.modifiers.new("NOISE"); nm.scale = 3; nm.strength = base * 0.5 nm.use_restricted_range = True; nm.frame_start, nm.frame_end = SPIN_UP, STOP_START # ------------------------------------------------------------------ shards pour out onto the tray and settle N_OUT = 20 tray_top = tray_c.z + tray_s.z / 2 for i in range(N_OUT): t0 = 128 + i * 2.2 sc0 = random.uniform(1.2, 1.7) o = new_crystal("Output_Shard_%02d" % i, sc0) start = Vector((tray_c.x + random.uniform(-1.0, 1.0), chute_back_c.y - 0.5, chute_ceil_c.z - 0.45)) # pile: lower-front of the sloped tray first, then stacking up gx = tray_c.x + random.uniform(-tray_s.x * 0.36, tray_s.x * 0.36) gy = tray_c.y + random.uniform(-tray_s.y * 0.3, tray_s.y * 0.15) # tray slopes: lower toward the inside; follow the floor height at that y slope = (tray_s.z - 0.28) / max(tray_s.y, 0.01) gz = tray_top + 0.25 + (i // 7) * 0.22 + (gy - tray_c.y) * slope end = Vector((gx, gy, gz)) o.location = start hide_until(o, int(t0), sc0) key(o, "location", int(t0), start, "QUAD", "EASE_IN") # free fall key(o, "location", int(t0) + 8, end, "QUAD", "EASE_OUT") key(o, "location", int(t0) + 11, end + Vector((random.uniform(-0.1, 0.1), -0.08, 0.3)), "QUAD", "EASE_IN") # bounce key(o, "location", int(t0) + 14, end) r0 = Euler([random.uniform(0, 6.28) for _ in range(3)]) key(o, "rotation_euler", int(t0), r0) key(o, "rotation_euler", int(t0) + 14, Euler([a + random.uniform(2, 5) for a in r0])) # ------------------------------------------------------------------ shredder close-up camera (main camera kept) cam_data = bpy.data.cameras.new("Shredder_Cam") cam = bpy.data.objects.new("Shredder_Cam", cam_data) sc.collection.objects.link(cam) target = Vector((tray_c.x, glass_c.y, (tray_c.z + ceil_c.z) / 2)) cam.location = target + Vector((5.0, -17.0, 3.5)) cam.rotation_euler = (target - cam.location).to_track_quat("-Z", "Y").to_euler() cam_data.lens = 50 main_cam = sc.camera bpy.ops.wm.save_as_mainfile(filepath=out_path) # check frames (+ optional preview video) from the close-up camera render_dir = os.path.join(os.path.dirname(src_path), "shredder_anim") os.makedirs(render_dir, exist_ok=True) sc.camera = cam sc.render.resolution_x = sc.render.resolution_y = 900 for f in (() if "--video" in sys.argv else (8, 45, 75, 130, 150, 178)): sc.frame_set(f) sc.render.filepath = os.path.join(render_dir, "frame_%03d.png" % f) bpy.ops.render.render(write_still=True) if "--video" in sys.argv: sc.render.resolution_x = sc.render.resolution_y = 720 if hasattr(sc.render.image_settings, "media_type"): # Blender 5.x: pick video output first sc.render.image_settings.media_type = "VIDEO" sc.render.image_settings.file_format = "FFMPEG" sc.render.ffmpeg.format = "MPEG4" sc.render.ffmpeg.codec = "H264" sc.render.ffmpeg.constant_rate_factor = "MEDIUM" sc.render.filepath = os.path.join(render_dir, "shredder_preview.mp4") bpy.ops.render.render(animation=True) sc.camera = main_cam print("SAVED", out_path, "drums", len(drum_parts), "shards", N_SHARDS, "grit", len(grit), "out", N_OUT)