cosmol_viewer
A 3D scene container for visualizing molecular or geometric shapes.
This class allows adding, updating, and removing shapes in a 3D scene, as well as modifying scene-level properties like scale and background color.
Supported shape types:
- Sphere
- Stick
- Molecule
- Protein
Shapes can be optionally identified with a string id,
which allows updates and deletion.
Add a shape to the scene without an explicit ID.
Args
- shape: A shape instance (Sphere, Stick, Molecule, or Protein).
Example
scene.add_shape(sphere)
Add a shape to the scene with a specific ID which can be used to update or remove the shape later. If a shape with the same ID exists, this method may fail or behave strictly;
Args
- id: Unique string ID for the shape.
- shape: A shape instance.
Example
scene.add_shape_with_id("bond1", stick)
Replace an existing shape in the scene by its ID.
Args
- id: ID of the shape to update.
- shape: New shape object to replace the existing one.
Example
scene.replace_shape("mol", updated_molecule)
Remove a shape from the scene by its ID.
Args
- id: ID of the shape to remove.
Example
scene.remove_shape("bond1")
Recenter the scene at a given point.
Args
- center: An XYZ array of 3 float values representing the new center.
Example
scene.recenter([0.0, 0.0, 0.0])
Set the global scale factor of the scene. This affects the visual size of all shapes uniformly.
Args
- scale: A positive float scaling factor.
Example
scene.set_scale(1.5)
A container for handling frame-based animations in the viewer.
Create a new Animation container.
Args
- interval: Time in seconds between frames.
- loops: Number of times to loop the animation (-1 for infinite).
- smooth: Whether to interpolate between frames for smoother visualization.
Example
anim = Animation(interval=0.1, loops=-1, smooth=True)
Add a frame (Scene) to the animation.
Args
- frame: A Scene object representing a single frame of the animation.
A viewer that renders 3D scenes in different runtime environments (e.g., Jupyter, Colab, or native GUI).
The Viewer automatically selects a backend:
- Jupyter/Colab → WebAssembly canvas (inline display)
- Python script/terminal → native GUI window (if supported)
Use Viewer.render(scene) to create and display a viewer instance.
Get the current runtime environment.
Returns
- str: One of "Jupyter", "Colab", "PlainScript", or "IPythonTerminal".
Example
env = Viewer.get_environment()
print(env) # e.g., "Jupyter"
Render a 3D scene.
Args
- scene: The scene to render.
- width: The viewport width in pixels (default: 800).
- height: The viewport height in pixels (default: 600).
Returns
- Viewer: The created viewer instance.
Example
from cosmol_viewer import Viewer, Scene, Sphere
scene = Scene()
scene.add_shape(Sphere([0, 0, 0], 1.0))
viewer = Viewer.render(scene)
Play an animation.
Args
- animation: An Animation object containing frames and settings.
- width: The viewport width in pixels.
- height: The viewport height in pixels.
Returns
- Viewer: The created viewer instance playing the animation.
Example
from cosmol_viewer import Viewer, Animation
anim = Animation(0.1, 10, True)
# ... add frames to anim ...
viewer = Viewer.play(anim, 800.0, 600.0)
Update the viewer with a new scene. Works for both Web-based rendering (Jupyter/Colab) and native GUI windows.
⚠️ Note (Jupyter/Colab): Animation updates may be limited by notebook rendering capacity.
Args
- scene: The updated scene.
Example
scene.add_shape(Sphere([1, 1, 1], 0.5))
viewer.update(scene)
A sphere shape in the scene.
Args
- center: [x, y, z] coordinates of the sphere center.
- radius: Radius of the sphere.
Example
sphere = Sphere([0, 0, 0], 1.0).color([1.0, 0.0, 0.0])
color(self, c: tuple[int, int, int]) -> Sphere color(self, c: str) -> Sphere
Set the shape color.
A cylindrical stick (or capsule) connecting two points.
Args
- start: Starting point [x, y, z].
- end: Ending point [x, y, z].
- thickness: Stick radius.
Example
stick = Stick([0,0,0], [1,1,1], 0.1).opacity(0.5)
color(self, c: tuple[int, int, int]) -> Stick color(self, c: str) -> Stick
Set the shape color.
A molecular shape object. Typically created by parsing an SDF format string.
Example
# Load from file content
content = open("structure.sdf", "r").read()
mol = Molecule.from_sdf(content).centered()
color(self, c: tuple[int, int, int]) -> Molecule color(self, c: str) -> Molecule
Set the shape color.
A protein shape object. Typically created by parsing an mmCIF format string.
Example
# Load from file content
content = open("2AMD.cif", "r").read()
prot = Protein.from_mmcif(content).centered().color("\#F9FAFB")
color(self, c: tuple[int, int, int]) -> Protein color(self, c: str) -> Protein
Set the shape color.