Skip to content
Snippets Groups Projects
utility_panel.py 49.4 KiB
Newer Older
  • Learn to ignore specific revisions
  •             cube.parent = atom.parent
                cube.hide_set(True)
                electron1.hide_set(True)
                electron2.hide_set(True)
                lamp1.hide_set(True)
                lamp2.hide_set(True)
    
        # Deselect everything
        bpy.ops.object.select_all(action='DESELECT')
        # Make the old atom visible.
        atom.hide_set(True)
        # Select the old atom.
        atom.select_set(True)
        # Remove the parent if necessary.
        atom.parent = None
        # Unlink the old object from the collection.
        coll_atom.objects.unlink(atom)
        # Delete the old atom
        bpy.ops.object.delete()
    
        return new_atom
    
    
    # Initialization of the list 'ELEMENTS'.
    def read_elements():
    
        del ELEMENTS[:]
    
        for item in ELEMENTS_DEFAULT:
    
            # All three radii into a list
            radii = [item[4],item[5],item[6]]
            # The handling of the ionic radii will be done later. So far, it is an
            # empty list.
            radii_ionic = item[7:]
    
            li = ElementProp(item[0],item[1],item[2],item[3],
                                         radii,radii_ionic)
            ELEMENTS.append(li)
    
    
    # Custom data file: changing color and radii by using the list 'ELEMENTS'.
    def custom_datafile_change_atom_props():
    
        for atom in bpy.context.selected_objects:
            if len(atom.children) != 0:
                child = atom.children[0]
                if child.type in {'SURFACE', 'MESH', 'META'}:
                    for element in ELEMENTS:
                        if element.name in atom.name:
                            child.scale = (element.radii[0],) * 3
                            child.active_material.diffuse_color = element.color
            else:
                if atom.type in {'SURFACE', 'MESH', 'META'}:
                    for element in ELEMENTS:
                        if element.name in atom.name:
                            atom.scale = (element.radii[0],) * 3
                            atom.active_material.diffuse_color = element.color
    
    
    # Reading a custom data file and modifying the list 'ELEMENTS'.
    def custom_datafile(path_datafile):
    
        if path_datafile == "":
            return False
    
        path_datafile = bpy.path.abspath(path_datafile)
    
        if os.path.isfile(path_datafile) == False:
            return False
    
        # The whole list gets deleted! We build it new.
        del ELEMENTS[:]
    
        # Read the data file, which contains all data
        # (atom name, radii, colors, etc.)
        data_file_p = open(path_datafile, "r")
    
        for line in data_file_p:
    
            if "Atom" in line:
    
                line = data_file_p.readline()
                # Number
                line = data_file_p.readline()
                number = line[19:-1]
                # Name
                line = data_file_p.readline()
                name = line[19:-1]
                # Short name
                line = data_file_p.readline()
                short_name = line[19:-1]
                # Color
                line = data_file_p.readline()
                color_value = line[19:-1].split(',')
                color = [float(color_value[0]),
                         float(color_value[1]),
                         float(color_value[2]),
                         float(color_value[3])]
                # Used radius
                line = data_file_p.readline()
                radius_used = float(line[19:-1])
                # Atomic radius
                line = data_file_p.readline()
                radius_atomic = float(line[19:-1])
                # Van der Waals radius
                line = data_file_p.readline()
                radius_vdW = float(line[19:-1])
                radii = [radius_used,radius_atomic,radius_vdW]
                radii_ionic = []
    
                element = ElementProp(number,name,short_name,color,
                                                  radii, radii_ionic)
    
                ELEMENTS.append(element)
    
        data_file_p.close()
    
        return True