Opening Multiple pdb files via Blender script #327
Replies: 1 comment
-
Hi Michael, You can certainly load a whole bunch of molecules via the python API. Be warned that the aspect of using MN via scripting I wouldn't consider to be stable, and the API will likely change soon as I try to improve it to work better. You can achieve what you are after with the script below: import MolecularNodes as mn
# in upcoming versions it will belowercase as below
# import molecularnodes as mn
from pathlib import Path
# replace with path to your directory
directory_path = Path('/Users/brady/Desktop/pdb_files/')
pdb_files = directory_path.glob('*.pdb')
for file in pdb_files:
# load the file using molecularnodes into the scene. First argument is file, second is name.
mn.load.molecule_local(file, file.stem) The documentation for the API component is lacking at the moment as it is also in flux, but the function I am using is here if you would like to look at the available options: MolecularNodes/molecularnodes/load.py Lines 160 to 168 in 6c43b26 Again, this is almost guaranteed to break in upcoming versions as I figure out how to better design the API, but this should get you what you are after at the moment. |
Beta Was this translation helpful? Give feedback.
-
Hello all,
I was wondering, would it be possible to open multiple .pdb files from a local folder with a python script within Blender, and name them appropriately? I do know that within Scene Properties I can go to Molecular Nodes, Local File, select the path & pdb file, then load the structure into Blender.
However I would like to open all .pdb files within a local folder and name the molecules after their .pdb file name using a script I can run within Blender. I had a go at trying to do this using chatgpt, resulting with this code:
################################################
import bpy
#Set the directory containing the PDB files
directory = "C:\Users\Desktop\ALotOfPDBStructures"
#Get a list of PDB files in the directory
pdb_files = [f for f in os.listdir(directory) if f.endswith(".pdb")]
#Set the context to 'SCENE'
bpy.context.space_data.context = 'SCENE'
#Loop through the PDB files and import each one
for pdb_file in pdb_files:
#Set the import method to 2
bpy.ops.mn.import_method_selection(MN_interface_value=2)
################################################
But then I realised my background in scripting is watching the Matrix, so this code failed, with the following error:
################################################
Python: Traceback (most recent call last):
File "\Text", line 10, in
AttributeError: 'SpaceTextEditor' object has no attribute 'context'
################################################
I tried to trick the system so I generated one multi-frame pdb file made up of all the individual pdb structures within my folder, but those structures are not the same, as in they are not a different conformation of the same protein, just alot of different structures not related to each other, hence the atom number between models did not match, and this didnt work.
I am sure I am overcomplicating this, but any help would be appreciated. Again, great add-on!
Michael
Beta Was this translation helpful? Give feedback.
All reactions