[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2675] contrib/py/scripts/addons/ io_import_pdb_atomic_blender.py:

Clemens Barth barth at root-1.de
Mon Nov 28 16:49:43 CET 2011


Revision: 2675
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2675
Author:   blendphys
Date:     2011-11-28 15:49:43 +0000 (Mon, 28 Nov 2011)
Log Message:
-----------


Dear all.

Corrections and changes
=======================

- Offsets => A bug I introduced yesterday was removed.
- Separating an atom => A bug was removed, too (wrong offset of the separated
  atom).
- The code for the drawing has been considerably shortened 
- Polishing: many "for i to range(value): do" were replaced by
  "for item in list: do"



Cheers,

Blendphys.

Modified Paths:
--------------
    contrib/py/scripts/addons/io_import_pdb_atomic_blender.py

Modified: contrib/py/scripts/addons/io_import_pdb_atomic_blender.py
===================================================================
--- contrib/py/scripts/addons/io_import_pdb_atomic_blender.py	2011-11-28 09:43:48 UTC (rev 2674)
+++ contrib/py/scripts/addons/io_import_pdb_atomic_blender.py	2011-11-28 15:49:43 UTC (rev 2675)
@@ -7,8 +7,10 @@
 #
 #  Start of project              : 2011-08-31 by Clemens Barth
 #  First publication in Blender  : 2011-11-11
-#  Last modified                 : 2011-11-27
+#  Last modified                 : 2011-11-28
 #
+#  Acknowledgements: Thanks to ideasman, meta_androcto, truman, kilon, 
+#  dairin0d, PKHG, Valter, etc 
 #
 # ##### BEGIN GPL LICENSE BLOCK #####
 #
@@ -30,8 +32,8 @@
 #
 #
 
+ATOM_PDB_VERSION = "0.9"
 
-
 bl_info = {
   "name": "PDB Atomic Blender",
   "description": "Loading and manipulating atoms from PDB files",
@@ -66,8 +68,8 @@
 ATOM_PDB_FILENAME = ""
 
 # Some string stuff for the console.
-ATOM_PDB_STRING = "Atomic Blender 0.9\n==================="
-ATOM_PDB_PANELNAME = "PDB - Atomic Blender - v0.9"
+ATOM_PDB_STRING = "Atomic Blender "+ATOM_PDB_VERSION+"\n==================="
+ATOM_PDB_PANELNAME = "PDB - Atomic Blender - v"+ATOM_PDB_VERSION
 
 # This is a list that contains some data of all possible atoms. The structure 
 # is as follows:
@@ -402,12 +404,13 @@
 
     def execute(self, context):
         scn    = bpy.context.scene
-
-        # Get first all important properties from the atom the user
-        # has chosen: color, scale
-        name  = bpy.context.edit_object.name 
+        
+        # Get first all important properties from the atom which the user
+        # has chosen: location, color, scale
+        name = bpy.context.edit_object.name 
+        loc_obj_vec = bpy.context.edit_object.location 
         scale = bpy.context.edit_object.children[0].scale
-        material = bpy.context.edit_object.children[0].active_material
+        material = bpy.context.edit_object.children[0].active_material        
         
         # Separate the vertex from the main mesh and create a new mesh.
         bpy.ops.mesh.separate()
@@ -430,23 +433,27 @@
             if scn.use_atom_pdb_mesh == False:
                 bpy.ops.surface.primitive_nurbs_surface_sphere_add(
                                     view_align=False, enter_editmode=False, 
-                                    location=loc_vec, rotation=(0.0, 0.0, 0.0), 
+                                    location=loc_vec+loc_obj_vec, 
+                                    rotation=(0.0, 0.0, 0.0), 
                                     layers=current_layers)        
             else:
                 bpy.ops.mesh.primitive_uv_sphere_add(
-                                segments=Ball_azimuth, ring_count=Ball_zenith, 
+                                segments=scn.atom_pdb_mesh_azimuth, 
+                                ring_count=scn.atom_pdb_mesh_zenith, 
                                 size=1, view_align=False, enter_editmode=False, 
-                                location=loc_vec, rotation=(0, 0, 0), 
+                                location=loc_vec+loc_obj_vec, 
+                                rotation=(0, 0, 0), 
                                 layers=current_layers)   
         else:                                             
             bpy.ops.mesh.primitive_cube_add(
                                view_align=False, enter_editmode=False, 
-                               location=loc_vec, 
+                               location=loc_vec+loc_obj_vec, 
                                rotation=(0.0, 0.0, 0.0), 
                                layers=current_layers)                     
+                                                                                                                     
                                                                   
         new_atom = bpy.context.scene.objects.active
-        # Scale it.
+        # Scale, material and name it.
         new_atom.scale = scale
         new_atom.active_material = material
         new_atom.name = name + "_sep"
@@ -680,7 +687,7 @@
     else:
         return "N.A."
 
-    dv     = object_2.location - object_1.location
+    dv = object_2.location - object_1.location
     return str(dv.length) 
 
 
@@ -697,7 +704,7 @@
             if bpy.context.scene.layers[i] == True:
                 layers.append(i)
                 
-        # Put all objects, which are in the layers.        
+        # Put all objects, which are in the layers, into a list.        
         change_objects = []        
         for obj in bpy.context.scene.objects:
             for layer in layers:
@@ -933,7 +940,7 @@
     stick_atom1 = []
     stick_atom2 = []
    
-    # Materials
+    # List of materials
     atom_material_list = []
 
 
@@ -1050,8 +1057,7 @@
     # instance (see below).      
     atom_all_types_list = []
     
-    i = 0
-    for name in atom_name:
+    for name, element, color in zip(atom_name, atom_element, atom_color):
         FLAG_FOUND = False
         for atom_type in atom_all_types_list:
             # If the atom name is already in the list, FLAG on 'True'. 
@@ -1062,11 +1068,8 @@
         if FLAG_FOUND == False:
             # Stored are: Atom label (e.g. 'Na'), the corresponding atom
             # name (e.g. 'Sodium') and its color.
-            atom_all_types_list.append([name,
-                                        atom_element[i],
-                                        atom_color[i]])
+            atom_all_types_list.append([name, element, color])
 
-        i += 1
 
 
 
@@ -1078,25 +1081,24 @@
    
     # Create first a new list of materials for each type of atom 
     # (e.g. hydrogen)
-    for atom_type in atom_all_types_list:
-   
+    for atom_type in atom_all_types_list:  
         material = bpy.data.materials.new(atom_type[1])
         material.name = atom_type[0]
         material.diffuse_color = atom_type[2]
         atom_material_list.append(material)
    
     # Now, we go through all atoms and give them a material. For all atoms ...   
-    for i in range(0, Number_of_total_atoms):
+    for name in atom_name:
         # ... and all materials ...
         for material in atom_material_list:
             # ... select the correct material for the current atom via 
             # comparison of names ...
-            if atom_name[i] in material.name:
+            if name in material.name:
                 # ... and give the atom its material properties. 
                 # However, before we check, if it is a vacancy, because then it
                 # gets some additional preparation. The vacancy is represented
                 # by a transparent cube.
-                if atom_name[i] == "Vacancy":
+                if name == "Vacancy":
                     material.transparency_method = 'Z_TRANSPARENCY'
                     material.alpha = 1.3
                     material.raytrace_transparency.fresnel = 1.6
@@ -1217,26 +1219,25 @@
     #
 
 
-
-    # If chosen, the structure is first put into the center of the scene.
+    # It may happen that the structure in a PDB file already has an offset
+    # If chosen, the structure is first put into the center of the scene
+    # (the offset is substracted).
     if put_to_center == True:
 
         sum_vec = Vector((0.0,0.0,0.0)) 
 
         # Sum of all atom coordinates
-        # sum_vec can be done like, sum_vec = sum(atom_xyz_vec, Vector((0, 0, 0))
         sum_vec = sum(atom_xyz_vec, sum_vec)
 
         # Then the average is taken
         sum_vec = sum_vec / Number_of_total_atoms
 
-        # After, for each atom the center of gravity is substracted
-        for atom_vec in atom_xyz_vec:
-            atom_vec = atom_vec - sum_vec
-        
+        # After, for each atom the center of gravity is substracted  
+        atom_xyz_vec = [vec - sum_vec for vec in atom_xyz_vec]
 
 
 
+
     #
     #
     #
@@ -1250,12 +1251,13 @@
     # Take all atoms and ...
     # - adjust their radii,
     # - scale the distances,
-    # - and move the center of the whole ('+= offset_x', in Angstroem)
-    for atom_vec in atom_xyz_vec:
-        atom_vec += offset_vec
-        atom_vec *= Ball_distance_factor
+    # - and apply the offset (chosen by the user in the file dialog window)
+    atom_xyz_vec = [vec + offset_vec for vec in atom_xyz_vec]
+    atom_xyz_vec = [vec * Ball_distance_factor for vec in atom_xyz_vec]
+    
 
 
+
          
 
 
@@ -1279,12 +1281,12 @@
     # ... and the average is taken. This gives the center of the object.
     object_center_vec = sum_vec / Number_of_total_atoms
 
-    # Now, we determine the size. All coordinates are analyzed ...
+    # Now, we determine the size.The farest atom from the object center is 
+    # taken as a measure. The size is used to place well the camera and light
+    # into the scene.    
+    object_size_vec = [vec - object_center_vec for vec in atom_xyz_vec]
     object_size = 0.0
-    
-    # This is needed in order to estimate the size of the object.
-    # The farest atom from the object center is taken as a measure.
-    object_size = max(atom_xyz_vec[i] - object_center_vec)
+    object_size = max(object_size_vec).length
 
 
 
@@ -1299,7 +1301,7 @@
 
     camera_factor = 15.0
 
-    # Here a camera is put into the scene, if chosen.
+    # If chosen a camera is put into the scene.
     if use_camera == True:
 
         # Assume that the object is put into the global origin. Then, the 
@@ -1443,16 +1445,12 @@
 
 
 
-    # Lists of atoms of one type are created. If it is atoms, all theses lists
-    # are put into one single list called 'draw_atom_type_list'. The vacancies
-    # have their extra list 'draw_atom_type_list_vacancy' 
-   
-    # The list containing all lists, which each contains all atoms of one type
-    draw_atom_type_list           = []
-    # The list which contains all vacancies
-    draw_atom_type_list_vacancy   = []
+    # Lists of atoms of one type are created. Example: 
+    # draw_atom_type_list = [ data_hydrogen,data_carbon,data_nitrogen ] 
+    # data_hydrogen = [["Hydrogen", Material_Hydrogen, Vector((x,y,z)), 109], ...]
+     
+    draw_atom_type_list = []
 
-
     # Go through the list which contains all types of atoms. It is the list,
     # which has been created on the top during reading the PDB file. 
     # Example: atom_all_types_list = ["hydrogen", "carbon", ...]
@@ -1463,26 +1461,17 @@
         draw_atom_list = []  
       
         # Go through all atoms ...

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list