Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created January 28, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/9f71c319ff8f245df43e to your computer and use it in GitHub Desktop.
Save zeffii/9f71c319ff8f245df43e to your computer and use it in GitHub Desktop.
import bpy
import bmesh
from collections import defaultdict
''' Aliases and variables '''
meshes = bpy.data.meshes
objects = bpy.data.objects
scene = bpy.context.scene
filepath = r'C:\Users\dealga\Documents\GitHub\BioBlender\Test_molecules\02_4IHV_dsDNA.pdb'
scalex = 0.2
scaley = 0.2
scalez = 0.2
''' Handle PDB import '''
def import_pdb(filepath):
atom_dict = defaultdict(list)
def inspect_line_by_line(pdb_file):
for line in pdb_file:
# protein data line: pdl
pdl = line.strip()
if not pdl.startswith('ATOM'):
continue
values = pdl.split()
x, y, z = values[6:9]
xyz = (float(x) * scalex, float(y) * scaley, float(z) * scalez)
atom_type = values[-1]
atom_dict[atom_type].append(xyz)
def pdb_import_intermediate():
with open(filepath) as pdb_file:
inspect_line_by_line(pdb_file)
pdb_import_intermediate()
return atom_dict
atom_dict = import_pdb(filepath)
''' Handle mesh construction '''
for atom_type, verts in atom_dict.items():
# assign locations to a new mesh
mesh = meshes.new('atoms_' + atom_type)
mesh.from_pydata(verts, [], [])
mesh.update()
# create an object to assign the mesh data to
obj = objects.new('atoms_' + atom_type, mesh)
scene.objects.link(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment