-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
68 lines (54 loc) · 2.43 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import bpy
def add_camera(location: tuple[float, float, float], rotation: tuple[float, float, float]) -> bpy.types.Object:
'''
Adds a camera to the scene with the given location and rotation.
Parameters:
- location: The location of the camera in 3D space (x, y, z).
- rotation: The rotation of the camera as Euler angles (x, y, z).
Returns:
- The newly created camera object.
'''
camera = bpy.data.cameras.new("camera")
camera_obj = bpy.data.objects.new("camera", camera)
camera_obj.location = location
camera_obj.rotation_euler = rotation
bpy.context.collection.objects.link(camera_obj)
return camera_obj
def attach_camera_to_curve(camera: bpy.types.Object, curve: bpy.types.Object, path_duration: int):
'''
Attaches a camera to a curve to create a path animation.
Parameters:
- camera: The camera object to attach to the curve.
- curve: The curve object that defines the camera path.
- path_duration: The duration of the path animation in frames.
Returns:
- None
'''
bpy.context.view_layer.objects.active = camera
bpy.ops.object.constraint_add(type='FOLLOW_PATH')
camera.constraints["Follow Path"].target = curve
camera.constraints["Follow Path"].use_fixed_location = True
camera.constraints["Follow Path"].forward_axis = 'FORWARD_X'
camera.constraints["Follow Path"].up_axis = 'UP_Z'
# Add animation to make camera follow path
camera.constraints["Follow Path"].offset_factor = 0.0
camera.constraints["Follow Path"].keyframe_insert(data_path="offset_factor", frame=1)
camera.constraints["Follow Path"].offset_factor = 1.0
camera.constraints["Follow Path"].keyframe_insert(data_path="offset_factor", frame=path_duration)
bpy.context.view_layer.update()
def get_video(file_path: str, camera: bpy.types.Object, path_duration: int):
'''
Renders a video using the specified camera and path duration.
Parameters:
- file_path: The path where the rendered video will be saved.
- camera: The camera object used for rendering the video.
- path_duration: The duration of the video in frames.
Returns:
- None
'''
bpy.context.scene.camera = camera
bpy.context.scene.frame_end = path_duration
bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.context.scene.render.ffmpeg.format = 'MPEG4'
bpy.context.scene.render.filepath = file_path
bpy.ops.render.render(animation=True)