BradyAJohnston / MolecularNodes

Toolbox for molecular animations in Blender, powered by Geometry Nodes.
https://bradyajohnston.github.io/MolecularNodes/
GNU General Public License v3.0
909 stars 85 forks source link

Implement Access and Stream Trajectories with MDAnalysis inside MolecularNode #292

Closed yuxuanzhuang closed 1 year ago

yuxuanzhuang commented 1 year ago

Issue Description:

We are planning to enhance the capabilities of MolecularNodes by integrating the ability to access and stream trajectories using MDAnalysis. This feature would enable users to

Proposed Design:

To implement this feature, we propose the following design in scripting:

start a session

mda_session = mn.mda.MDAnalysisSession(world_scale=0.01)

show atoms

universe = mda.Universe(top, traj)

custom_selections = {'name_ca': 'name CA', 'ion': 'resname SOD CLA'}

# This will create three representations inside Blender
mda_session.show(universe, name='protein', selection='protein', custom_selections=custom_selections)

# Add new representations for new selected atoms
ligand = universe.select_atoms('resname LIG')
mda_session.show(ligand, name='ligand', representation='ball+stick')

# Add representations for dynamically selected selected atoms
around_ligand = universe.select_atoms('protein and byres (around 4 resname LIG)', updating=True)
mda_session.show(around_ligand, name='around_lig', representation='vdw')

# add new trajectory and an offset of the frame can be set
universe2 = mda.Universe(top2, traj2)
mda_session.show(universe2, name='protein', selection='protein', frame_offset=100)

Persistence

# saving
file_path = "blender_and_mda.blend"
bpy.ops.wm.save_as_mainfile(filepath=file_path)

# loading
bpy.ops.wm.open_mainfile(filepath=file_path)
mda_session = bpy.context.scene.mda_session
universe = mda_session.universe

# persistent even when trajectory files are deleted
mda_session.transfer_to_memory(start, step, end)

# or use legacy code with Blender UI

Example:

Expected Caveats:

Tasks (task will not be implemented in this PR):

Future direction

BradyAJohnston commented 1 year ago

Thanks for writing this up! It's exciting to have so much potential in an idea that is already most of the way there. For the caveats, I don't think clashes with the currently implemented animation or selection system inside of Geometry Nodes will be a problem. Already we have seen by just not adding the animation node, everything pretty much works out of the box.

Ping @tubiana I think you would be interested in this. There is already some working examples showing interaction between notebooks and a live Blender session, streaming the MDAnalysis universe.

Example Notebook