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()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:
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,
)styleparam- This param can be a string or an instance of
nodes.StyleBaseclass. - 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.StyleBaseinstance, the value can be an instance ofnodes.StyleBallAndStick,nodes.StyleCartoon,nodes.StyleRibbon,nodes.StyleSpheres,nodes.StyleSticksandnodes.StyleSurface.
- This param can be a string or an instance of
colorparam- This param can be a string or a tuple with rgba values
- When passed as a string, the value can be one of
defaultorcommon - When passed as a tuple a regular rgba tuple with four float values can be used
selectionparam- This param can be an MDAnalysis selection phrase or an
AtomGroupof the universe
- This param can be an MDAnalysis selection phrase or an
materialparam- This param can be a string or an instance of a material instance created using the
mn.materialAPI. - 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 Occlusionor any other material name
- This param can be a string or an instance of a material instance created using the
nameparam 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 0x57fc24f20798>
# 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 0x57fc24f20798>
# 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 0x57fc24f20798>
# 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 0x57fc24f20798>
# 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 0x57fc24f20798>
# 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 0x79469b4c6dd0>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Spheres object at 0x79469b5b7850>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Surface object at 0x79469b5d3d10>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Ball and Stick object at 0x79469b5b5c10>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Surface.001 object at 0x794769e3d7d0>
<molecularnodes.nodes.geometry.DynamicStyleInterface_Sticks object at 0x79469b5d2390>
# access the first style - cartoon of the trajectory
s = t.styles[0]
s<molecularnodes.nodes.geometry.DynamicStyleInterface_Ribbon at 0x794769e3d4d0>
# 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()