Cleanup: simplify file name incrementing logic
[blender-addons.git] / io_mesh_atomic / pdb_export.py
blob949ee5e200aca137b836acc9a940d34b392f2253
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import bpy
20 from io_mesh_atomic.pdb_import import ELEMENTS_DEFAULT
22 class AtomPropExport(object):
23 __slots__ = ('element', 'location')
24 def __init__(self, element, location):
25 self.element = element
26 self.location = location
29 def export_pdb(obj_type, filepath_pdb):
31 list_atoms = []
32 for obj in bpy.context.selected_objects:
34 if "STICK" in obj.name.upper():
35 continue
37 if obj.type not in {'MESH', 'SURFACE', 'META'}:
38 continue
40 name = ""
41 for element in ELEMENTS_DEFAULT:
42 if element[1] in obj.name:
43 if element[2] == "Vac":
44 name = "X"
45 else:
46 name = element[2]
48 if name == "":
49 if obj_type == "0":
50 name = "?"
51 else:
52 continue
54 if len(obj.children) != 0:
55 for vertex in obj.data.vertices:
56 location = obj.matrix_world @ vertex.co
57 list_atoms.append(AtomPropExport(name, location))
58 else:
59 if not obj.parent:
60 location = obj.location
61 list_atoms.append(AtomPropExport(name, location))
63 pdb_file_p = open(filepath_pdb, "w")
64 pdb_file_p.write("REMARK This pdb file has been created with Blender "
65 "and the addon Atomic Blender - PDB\n"
66 "REMARK For more details see the wiki pages of Blender.\n"
67 "REMARK\n"
68 "REMARK\n")
70 for i, atom in enumerate(list_atoms):
71 string = "ATOM %6d%3s%24.3f%8.3f%8.3f%6.2f%6.2f%12s\n" % (
72 i, atom.element,
73 atom.location[0],
74 atom.location[1],
75 atom.location[2],
76 1.00, 1.00, atom.element)
77 pdb_file_p.write(string)
79 pdb_file_p.close()
81 return True