Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active August 6, 2026 13:55
Show Gist options
  • Select an option

  • Save uwezi/de800cf3bee9769db89e1b9edf2cd9a3 to your computer and use it in GitHub Desktop.

Select an option

Save uwezi/de800cf3bee9769db89e1b9edf2cd9a3 to your computer and use it in GitHub Desktop.
[3D objects face camera] Lets objects automatically face the camera. #manim #3D #threedscene #updater
from manim import *
from scipy.spatial.transform import Rotation as Rot
class look_to_camera(ThreeDScene):
def construct(self):
ax = ThreeDAxes()
self.add(ax)
obj = VGroup(
Text("Hello world!"),
Line(ORIGIN, OUT, stroke_width=0, stroke_opacity=0),
Line(ORIGIN, RIGHT, stroke_width=0, stroke_opacity=0)
)
def obj_updater(mobj):
cam_rot_imatrix = np.linalg.inv(self.camera.generate_rotation_matrix())
cam_out = np.matmul(cam_rot_imatrix, OUT)
align_out, rssd = Rot.align_vectors(cam_out, obj[-2].get_end()-obj[-2].get_start())
obj.apply_matrix(align_out.as_matrix())
cam_right = np.matmul(cam_rot_imatrix, RIGHT)
align_right, rssd = Rot.align_vectors(cam_right, obj[-1].get_end()-obj[-1].get_start())
obj.apply_matrix(align_right.as_matrix())
obj.add_updater(obj_updater)
self.add(obj)
self.wait()
self.move_camera(
phi = 75 * DEGREES, theta = 45 * DEGREES,
run_time=3
)
self.wait()
self.play(
Rotate(obj[0], PI, axis=obj[-2].get_end()-obj[-2].get_start()),
run_time=3
)
self.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment