Claro! Aqui vai outro exemplo mais avançado e diferente:
Cena com uma bola animando ao longo de um caminho Bezier 3D, com deformação do caminho (modificador “Build”), animação da bola escalando e colorindo, e luzes coloridas dinâmicas.
O que este script faz:
- Cria uma curva 3D (Bezier) que será mostrada gradualmente com modificador Build (para parecer que o caminho está sendo “desenhado”).
- Cria uma esfera (bola) que percorre o caminho enquanto escala e muda de cor.
- Anima a curva com Build para “aparecer” junto com o movimento da bola.
- Cria duas luzes coloridas que mudam de intensidade durante a animação.
- Ajusta câmera para visão geral da cena.
- Configura timeline para loopar em 150 frames.
Código completo
import bpy
import math
from mathutils import Vector
# Limpar cena
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 150
scene.frame_current = 1
# Criar curva Bezier 3D com formato em "S"
bpy.ops.curve.primitive_bezier_curve_add()
curve_obj = bpy.context.object
curve_obj.name = "CaminhoCurva"
curve = curve_obj.data
# Configurar 4 pontos para forma S em 3D
bpy.ops.object.editmode_toggle()
bp = curve.splines[0].bezier_points
bp[0].co = Vector((-6, 0, 0))
bp[0].handle_left_type = 'AUTO'
bp[0].handle_right_type = 'AUTO'
bp[1].co = Vector((-2, 4, 3))
bp[1].handle_left_type = 'AUTO'
bp[1].handle_right_type = 'AUTO'
# Adicionar mais pontos para curva mais longa
bp.add(count=2)
bp[2].co = Vector((2, -4, -3))
bp[2].handle_left_type = 'AUTO'
bp[2].handle_right_type = 'AUTO'
bp[3].co = Vector((6, 0, 0))
bp[3].handle_left_type = 'AUTO'
bp[3].handle_right_type = 'AUTO'
curve.splines[0].use_cyclic_u = False
bpy.ops.object.editmode_toggle()
bpy.ops.object.transform_apply(scale=True)
# Adicionar modificador Build para desenhar caminho gradualmente
build_mod = curve_obj.modifiers.new(name="Build", type='BUILD')
build_mod.frame_start = scene.frame_start
build_mod.frame_duration = scene.frame_end - scene.frame_start + 1
build_mod.use_reverse = False
# Criar bola no começo do caminho
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.6, location=bp[0].co)
ball = bpy.context.object
ball.name = "BolaAnimada"
# Material inicial azul escuro
mat = bpy.data.materials.new("MatBola")
mat.use_nodes = True
nodes = mat.node_tree.nodes
principled = nodes.get("Principled BSDF")
# Cor inicial azul escuro
principled.inputs['Base Color'].default_value = (0, 0, 0.4, 1)
ball.data.materials.append(mat)
# Constraint Follow Path
follow_path = ball.constraints.new(type='FOLLOW_PATH')
follow_path.target = curve_obj
follow_path.use_curve_follow = True
follow_path.forward_axis = 'FORWARD_X'
follow_path.up_axis = 'UP_Z'
follow_path.use_fixed_location = True
# Animação do movimento do bola ao longo da curva
start, end = scene.frame_start, scene.frame_end
total_frames = end - start + 1
curve_obj.data.path_duration = total_frames
# Movimento da bola ao longo do caminho
follow_path.offset_factor = 0.0
follow_path.keyframe_insert(data_path="offset_factor", frame=start)
follow_path.offset_factor = 1.0
follow_path.keyframe_insert(data_path="offset_factor", frame=end)
# Interpolação linear para movimento constante
for fc in ball.animation_data.action.fcurves:
for kp in fc.keyframe_points:
kp.interpolation = 'LINEAR'
# Escalar bola durante o percurso (de 0.5x para 1.5x e volta)
ball.scale = (0.5, 0.5, 0.5)
ball.keyframe_insert(data_path="scale", frame=start)
ball.scale = (1.5, 1.5, 1.5)
ball.keyframe_insert(data_path="scale", frame=start + total_frames // 2)
ball.scale = (0.5, 0.5, 0.5)
ball.keyframe_insert(data_path="scale", frame=end)
# Interpolação Linear para escala
if not ball.animation_data:
ball.animation_data_create()
if not ball.animation_data.action:
ball.animation_data.action = bpy.data.actions.new(name="BallScaleAction")
for fc in ball.animation_data.action.fcurves:
if "scale" in fc.data_path:
for kp in fc.keyframe_points:
kp.interpolation = 'LINEAR'
# Animar cor do material da bola ao longo do tempo (azul escuro -> azul claro -> azul escuro)
color_ramp = [
(0.0, (0.0, 0.0, 0.4, 1)),
(0.5, (0.3, 0.6, 1.0, 1)),
(1.0, (0.0, 0.0, 0.4, 1))
]
color_input = principled.inputs['Base Color']
for i, (pos, color) in enumerate(color_ramp):
frame = start + pos * total_frames
# Criar keyframe para cada canal R,G,B,A
for j in range(4):
color_input.default_value[j] = color[j]
color_input.keyframe_insert(data_path="default_value", frame=frame)
# Interpolação linear para cor (na curva do material)
mat.animation_data.action.fcurves[0].keyframe_points.foreach_set('interpolation', ['LINEAR'] * len(mat.animation_data.action.fcurves[0].keyframe_points))
# --- Luzes coloridas animadas ---
# Luz vermelha pulsante
bpy.ops.object.light_add(type='POINT', location=(4, 4, 6))
light_red = bpy.context.object
light_red.name = "LuzVermelha"
light_red.data.color = (1, 0, 0)
light_red.data.energy = 100
light_red.data.keyframe_insert(data_path="energy", frame=start)
light_red.data.energy = 300
light_red.data.keyframe_insert(data_path="energy", frame=start + total_frames // 2)
light_red.data.energy = 100
light_red.data.keyframe_insert(data_path="energy", frame=end)
for fc in light_red.data.animation_data.action.fcurves:
for kp in fc.keyframe_points:
kp.interpolation = 'SINE'
# Luz azul pulsante oposta
bpy.ops.object.light_add(type='POINT', location=(-4, -4, 6))
light_blue = bpy.context.object
light_blue.name = "LuzAzul"
light_blue.data.color = (0, 0, 1)
light_blue.data.energy = 100
light_blue.data.keyframe_insert(data_path="energy", frame=start)
light_blue.data.energy = 300
light_blue.data.keyframe_insert(data_path="energy", frame=start + total_frames // 2)
light_blue.data.energy = 100
light_blue.data.keyframe_insert(data_path="energy", frame=end)
for fc in light_blue.data.animation_data.action.fcurves:
for kp in fc.keyframe_points:
kp.interpolation = 'SINE'
# --- Câmera ---
bpy.ops.object.camera_add(location=(0, -20, 10), rotation=(math.radians(65), 0, 0))
camera = bpy.context.object
camera.name = "Camera"
# Configurar workspace para usar esta câmera
scene.camera = camera
print("Cena avançada criada! Bola animada com escala, cor, curva build e luzes pulsantes.")
Como usar
- Copie e cole no editor de texto do Blender.
- Execute o script.
- Pressione “Play” na timeline para ver animação da bola percorrendo o caminho que é desenhado gradualmente, com mudança de escala e cor, e luzes pulsantes.
Detalhes
- O modificador Build desenha a curva progressivamente.
- A bola segue o caminho e gira corretamente por usar
use_curve_follow.
- A bola também escala e muda cor suavemente.
- As luzes vermelha e azul têm intensidade suave variando em senóide para efeito dinâmico.
- Câmera fixa para melhor enquadramento.
Se desejar posso ajudar com mais exemplos, efeitos ou estilos diferentes! Quer mais algum tipo específico?