Hi,
I tried to export an animation about the camera moving around a 3D object, but it didn’t work.
My object was a 3D .mph data export from Matlab, and I used a Python script to define an interpolated path for the camera.
The animation worked well when I played it in Paraview, but when I tried to export it, only the first frame was saved into the .avi/.mp4 file, and no error information was given.
I can’t find any explanation or solution to this issue. Can you help me with this problem?
Thanks,
FOX
Below is my Python script to define the camera:
from paraview.simple import *
import mathGet the current active view and camera
view = GetActiveView()
camera = view.GetActiveCamera()Get current camera parameters
start_position = camera.GetPosition()
start_focal_point = camera.GetFocalPoint()
start_view_up = camera.GetViewUp()Define rotation parameters
axis = [0, 0, 1] # Rotation axis, e.g., [0, 0, 1] means rotating around the Z-axis
angle_degrees = 360 # Total rotation angle in degrees
num_steps = 30 # Number of keyframes
direction = 1 # Rotation direction: 1 for positive, -1 for negativeCompute rotation center (rotate around the focal point)
center = start_focal_point
Prepare to create the keyframe list
keyframes =
for i in range(num_steps + 1):
# Calculate the current keyframe’s time and angle
key_time = i / num_steps # Keyframe time, uniformly distributed from 0 to 1
angle = math.radians(direction * angle_degrees * key_time)# Compute rotation matrix c = math.cos(angle) s = math.sin(angle) t = 1 - c x, y, z = axis # Normalize the rotation axis norm = math.sqrt(x*x + y*y + z*z) x /= norm y /= norm z /= norm # Rotation matrix elements R = [ [t*x*x + c, t*x*y - s*z, t*x*z + s*y], [t*x*y + s*z, t*y*y + c, t*y*z - s*x], [t*x*z - s*y, t*y*z + s*x, t*z*z + c] ] # Compute new camera position p = [start_position[j] - center[j] for j in range(3)] # Translate to origin p_rotated = [ R[0][0]*p[0] + R[0][1]*p[1] + R[0][2]*p[2], R[1][0]*p[0] + R[1][1]*p[1] + R[1][2]*p[2], R[2][0]*p[0] + R[2][1]*p[1] + R[2][2]*p[2] ] new_position = [p_rotated[j] + center[j] for j in range(3)] # Translate back to original position # Compute new ViewUp direction vu = start_view_up vu_rotated = [ R[0][0]*vu[0] + R[0][1]*vu[1] + R[0][2]*vu[2], R[1][0]*vu[0] + R[1][1]*vu[1] + R[1][2]*vu[2], R[2][0]*vu[0] + R[2][1]*vu[1] + R[2][2]*vu[2] ] # Create keyframe keyframe = CameraKeyFrame() keyframe.KeyTime = key_time # Set keyframe time keyframe.Position = new_position # Set camera position keyframe.FocalPoint = start_focal_point # Keep focal point unchanged keyframe.ViewUp = vu_rotated # Update ViewUp direction keyframes.append(keyframe)
Set up animation scene
animationScene = GetAnimationScene()
animationScene.StartTime = 0
animationScene.EndTime = 1 # Use normalized time (0 to 1)
animationScene.PlayMode = ‘Sequence’ # Ensure play mode is ‘Sequence’
animationScene.NumberOfFrames = num_steps # Set number of framesCreate camera animation track and assign keyframes
cameraAnimationCue = GetCameraTrack(view)
cameraAnimationCue.KeyFrames = keyframesOptional: Play animation or save animation
animationScene.Play()