import molecularnodes as mn
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import DCD, PSF, TPR, XTC
= mda.Universe(PSF, DCD)
u = mn.Canvas() canvas
Styles
Examples of using Styles
Different styles or representations can be added to Trajectories.
Setup Molecular Nodes
Add Trajectory
A style can be specified when adding the trajectory to Blender as follows:
= mn.Trajectory(u).add_style(mn.StyleRibbon(quality=4, backbone_radius = 0.5))
t
canvas.frame_object(t) canvas.snapshot()
Add Styles
Styles can be added to different selections of the universe after it has been added to Blender using the add_style()
API.
Trajectory.add_style(='spheres',
style='common',
color=None,
selection=None,
material=None,
name )
style
param- This param can be a string or an instance of
nodes.StyleBase
class. - When passed as a string, the value can be one of
ball_and_stick
,cartoon
,ribbon
,spheres
,sticks
,surface
, etc. - When passed as a
nodes.StyleBase
instance, the value can be an instance ofnodes.StyleBallAndStick
,nodes.StyleCartoon
,nodes.StyleRibbon
,nodes.StyleSpheres
,nodes.StyleSticks
andnodes.StyleSurface
.
- This param can be a string or an instance of
color
param- This param can be a string or a tuple with rgba values
- When passed as a string, the value can be one of
default
orcommon
- When passed as a tuple a regular rgba tuple with four float values can be used
selection
param- This param can be an MDAnalysis selection phrase or an
AtomGroup
of the universe
- This param can be an MDAnalysis selection phrase or an
material
param- This param can be a string or an instance of a material instance created using the
mn.material
API. - When passed as a string, the value can be one of the default materials like
MN Default
,MN Flat Outline
,MN Squishy
,MN Transparent Outline
,MN Ambient Occlusion
or any other material name
- This param can be a string or an instance of a material instance created using the
name
param to give a name to the style
# add spheres style to resid 1 and 129
t.add_style(="Instance", subdivisions=4),
mn.StyleSpheres(geometry="resid 1 129",
selection )
<Trajectory, `universe`: <Universe with 3341 atoms>, `object`: <bpy_struct, Object("NewUniverseObject") at 0x57af6ea8b018>
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()
# add surface style for resids between 100 and 150 with transparent material
t.add_style(="surface", selection="resid 100:150", material="MN Transparent Outline"
style )
<Trajectory, `universe`: <Universe with 3341 atoms>, `object`: <bpy_struct, Object("NewUniverseObject") at 0x57af6ea8b018>
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()
# add a ball and style for resids 180:200
=4), selection=u.select_atoms("resid 180:200")) t.add_style(mn.StyleBallAndStick(quality
<Trajectory, `universe`: <Universe with 3341 atoms>, `object`: <bpy_struct, Object("NewUniverseObject") at 0x57af6ea8b018>
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()
# add surface style for resids 40:50 with transparent uniform color
="surface", selection="resid 40:50", color=(0.162, 0.624, 0.196, 0.5)) t.add_style(style
<Trajectory, `universe`: <Universe with 3341 atoms>, `object`: <bpy_struct, Object("NewUniverseObject") at 0x57af6ea8b018>
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()
# create a sticks style instance
= mn.nodes.styles.StyleSticks()
s # set radius to 0.1
= 0.1
s.radius # add style with a style instance
=s, selection="resid 40:50") t.add_style(style
<Trajectory, `universe`: <Universe with 3341 atoms>, `object`: <bpy_struct, Object("NewUniverseObject") at 0x57af6ea8b018>
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()
Access Styles
Styles added to a trajectory can be accessed using the styles interface. This interface allows access to all the style specific parameters (Geometry Node inputs).
for s in t.styles:
print(s)
<molecularnodes.nodes.geometry.DynamicStyleInterface_Ribbon object at 0x751053b1b650>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Spheres object at 0x751053b020d0>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Surface object at 0x7510539ab910>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Ball and Stick object at 0x75111aaf1b50>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Surface.001 object at 0x75108f8d4810>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Sticks object at 0x751053b09b50>
# access the first style - cartoon of the trajectory
= t.styles[0]
s s
<molecularnodes.nodes.geometry.DynamicStyleInterface_Ribbon at 0x7510539aa610>
# access the last style - sticks resid 40:50
= t.styles[-1]
s # set the sticks radius to 0.3
= 0.3 s.sticks_radius
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()
Remove Styles
Styles can be removed using the remove
method of the style interface.
# remove last two styles
-1].remove()
t.styles[-1].remove() t.styles[
# frame trajectory and render
canvas.frame_object(t) canvas.snapshot()