Skip to content
Snippets Groups Projects
  • Clemens Barth's avatar
    9791bd3a
    The new ‘Atomic Blender PDB/XYZ’ importer. T62804 · 9791bd3a
    Clemens Barth authored
    Comments
    ========
    This is the fusion of the 3 atomic blender addons from Blender 2.79:
    
        1. PDB (I/O addon for .pdb files, was in trunk before)
        2. XYZ (I/O addon for .xyz files, was in contrib before)
        3. Utilities (panel for modifying atomic structures, was in contrib before),
    
    into one single addon called ‘Atomic Blender PDB/XYZ’.
    9791bd3a
    History
    The new ‘Atomic Blender PDB/XYZ’ importer. T62804
    Clemens Barth authored
    Comments
    ========
    This is the fusion of the 3 atomic blender addons from Blender 2.79:
    
        1. PDB (I/O addon for .pdb files, was in trunk before)
        2. XYZ (I/O addon for .xyz files, was in contrib before)
        3. Utilities (panel for modifying atomic structures, was in contrib before),
    
    into one single addon called ‘Atomic Blender PDB/XYZ’.
xyz_export.py 2.67 KiB
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy
from io_mesh_atomic.xyz_import import ELEMENTS_DEFAULT


class AtomsExport(object):
    __slots__ = ('element', 'location')
    def __init__(self, element, location):
        self.element  = element
        self.location = location


def export_xyz(obj_type, filepath_xyz):

    list_atoms = []
    counter = 0
    for obj in bpy.context.selected_objects:

        if "STICK" in obj.name.upper():
            continue

        if obj.type not in {'MESH', 'SURFACE', 'META'}:
            continue

        name = ""
        for element in ELEMENTS_DEFAULT:
            if element[1] in obj.name:
                if element[2] == "Vac":
                    name = "X"
                else:
                    name = element[2]

        if name == "":
            if obj_type == "0":
                name = "?"
            else:
                continue

        if len(obj.children) != 0:
            for vertex in obj.data.vertices:
                location = obj.matrix_world @ vertex.co
                list_atoms.append(AtomsExport(name, location))
                counter += 1
        else:
            if not obj.parent:
                location = obj.location
                list_atoms.append(AtomsExport(name, location))
                counter += 1

    xyz_file_p = open(filepath_xyz, "w")
    xyz_file_p.write("%d\n" % counter)
    xyz_file_p.write("This XYZ file has been created with Blender "
                     "and the addon Atomic Blender - XYZ. "
                     "For more details see the wiki pages of Blender.\n")

    for i, atom in enumerate(list_atoms):
        string = "%3s%15.5f%15.5f%15.5f\n" % (
                                      atom.element,
                                      atom.location[0],
                                      atom.location[1],
                                      atom.location[2])
        xyz_file_p.write(string)

    xyz_file_p.close()

    return True