import molecularnodes as mn
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import DCD, PSF, TPR, XTC
# create a canvas object
= mn.Canvas() canvas
Canvas
The Canvas
object can be used to configure different render settings, camera, frame objects and views to render images and animations. The snapshot
and animation
methods can be used to render images and animations respectively.
Setup Molecular Nodes
Add Trajectories
= mda.Universe(PSF, DCD)
u1 = mda.Universe(TPR, XTC)
u2
# add universes to Blender
= mn.Trajectory(u1).add_style("cartoon")
t1 = mn.Trajectory(u2) t2
Add Styles and Annotations to Trajectories
# styles
# trajectory 1
=mn.StyleBallAndStick(quality=4), selection="resid 184")
t1.add_style(style
t1.add_style(=mn.StyleSpheres(subdivisions=4, geometry="Instance"),
style="resid 1 129",
selection
)
# trajectory 2
="ribbon", color=(0.162, 0.624, 0.196, 0.5))
t2.add_style(style="resid 141", style="ball_and_stick")
t2.add_style(selection
# annotations
# trajectory 1
t1.annotations.add_com_distance(="resid 1", selection2="resid 129", text1="resid 1", text2="resid 129"
selection1
)="name CA and resid 184", show_resid=True)
t1.annotations.add_atom_info(selection# trajectory 2
="name CA and resid 141", show_resid=True) t2.annotations.add_atom_info(selection
<molecularnodes.annotations.manager.AtomInfo_interface at 0x7b191f9f4810>
Add Density component
from pathlib import Path
= Path("../../") / "tests/data/emd_24805.map.gz"
d1_path
# load density file
= mn.entities.density.io.load(
d1 =d1_path,
file_path="density_iso_surface",
style=True,
overwrite )
Frame Objects
The frame_object
method can be used to frame entity objects. By default, the current camera viewpoint is used to frame objects. An optional viewpoint
param can be used to set the viewpoint to one of [default
, front
, back
, left
, right
, top
, bottom
].
# frame trajectory 1 from front and render
="front")
canvas.frame_object(t1, viewpoint canvas.snapshot()
# frame trajectory 2 from front and render
="front")
canvas.frame_object(t2, viewpoint canvas.snapshot()
# frame density object from right and render
="right")
canvas.frame_object(d1, viewpoint canvas.snapshot()
Frame Views
The frame_view
method can be used to frame one or more views. Views can be specific selections within entities or across entities. Multiple views can be combined with a +
sign before passing to the frame_view
method. An optional viewpoint
param can be passed to specify the viewpoint like before.
All entities have a get_view
method that returns the entity object by default. The get_view
method of trajectories can return views based on one or more selection strings or AtomGroups
and also take the specific trajectory frame into consideration.
# frame resid 184 in trajectory 1 from the front
= t1.get_view(selection="resid 184")
v184 ="front")
canvas.frame_view(v184, viewpoint canvas.snapshot()
# frame resid 1 and 129 in trajectory 1 at frame 97 from the front
"resid 1 129", frame=97), viewpoint="front")
canvas.frame_view(t1.get_view( canvas.snapshot()
# frame resid 184 from trajectory 1 and resid 141 from trajectory 2
canvas.frame_view("resid 184") + t2.get_view("resid 141"), viewpoint="front"
t1.get_view(
) canvas.snapshot()
# frame trajectory 2 and density component from left
+ d1.get_view(), viewpoint="left")
canvas.frame_view(t2.get_view() canvas.snapshot()
Camera Zoom in or out
Camera can be zoomed in our out using the camera lens
settings of the Canvas
camera object once framed.
# frame density object from front and render - default
="front")
canvas.frame_object(d1, viewpoint canvas.snapshot()
# zoom in
= 150
canvas.camera.lens canvas.snapshot()
# zoom out
= 35
canvas.camera.lens canvas.snapshot()
# reset camera lens to default (50mm)
= 50 canvas.camera.lens
Camera Rotation
Camera can be rotated to an arbitrary angle using the camera rotation
method of the Canvas
camera object before framing the object or view. Blender uses a default XYZ
Euler for rotations along the X, Y and Z axes. The rotation
method takes angles in degrees.
# print current rotation
canvas.camera.rotation
(89.999995674289, -0.0, 0.0)
# rotate 45 degrees to left from the front view
# frame density object - note that viewpoint is not specified
90, 0, -45))
canvas.frame_view(d1, ( canvas.snapshot()
# frame density object and render
90, 0, 45))
canvas.frame_view(d1, ( canvas.snapshot()
Animations
The animation
method can be used to render animations. An optional frame_start
and frame_end
can be specified to limit animations to a custom range. The render_scale
parameter can be used to scale the renders as a percentage of the render resolution.
# frame resid 1 and 129 in trajectory 1 at frame 97 from the front
"resid 1 129", frame=97), viewpoint="front")
canvas.frame_view(t1.get_view(# add a universe info annotation to trajectory 1
t1.annotations.add_universe_info()
<molecularnodes.annotations.manager.UniverseInfo_interface at 0x7b1925926490>
# render animation of scene from frame 10 to 50 at 50% sccale
=10, frame_end=50, render_scale=50) canvas.animation(frame_start