Styles

Examples of using Styles

Different styles or representations can be added to Trajectories.

Setup Molecular Nodes

import molecularnodes as mn
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import DCD, PSF, TPR, XTC

u = mda.Universe(PSF, DCD)
canvas = mn.Canvas()

Add Trajectory

A style can be specified when adding the trajectory to Blender as follows:

t = mn.Trajectory(u).add_style(mn.StyleRibbon(quality=4, backbone_radius = 0.5))
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(
    style='spheres',
    color='common',
    selection=None,
    material=None,
    name=None,
)
  • 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 of nodes.StyleBallAndStick, nodes.StyleCartoon, nodes.StyleRibbon, nodes.StyleSpheres, nodes.StyleSticks and nodes.StyleSurface.
  • 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 or common
    • 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
  • 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
  • name param to give a name to the style
# add spheres style to resid 1 and 129
t.add_style(
    mn.StyleSpheres(geometry="Instance", subdivisions=4),
    selection="resid 1 129",
)
<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(
    style="surface", selection="resid 100:150", material="MN Transparent Outline"
)
<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
t.add_style(mn.StyleBallAndStick(quality=4), selection=u.select_atoms("resid 180:200"))
<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
t.add_style(style="surface", selection="resid 40:50", color=(0.162, 0.624, 0.196, 0.5))
<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
s = mn.nodes.styles.StyleSticks()
# set radius to 0.1
s.radius = 0.1
# add style with a style instance
t.add_style(style=s, selection="resid 40:50")
<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
s = t.styles[0]
s
<molecularnodes.nodes.geometry.DynamicStyleInterface_Ribbon at 0x7510539aa610>
# access the last style - sticks resid 40:50
s = t.styles[-1]
# set the sticks radius to 0.3
s.sticks_radius = 0.3
# 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
t.styles[-1].remove()
t.styles[-1].remove()
# frame trajectory and render
canvas.frame_object(t)
canvas.snapshot()