Skip to content
Snippets Groups Projects
utility_panel.py 64.9 KiB
Newer Older
  • Learn to ignore specific revisions
  •             cube.parent = atom.parent
                cube.hide_set(True)
                electron.hide_set(True)
                lamp.hide_set(True)
    
        # F0 center
        if atom_shape == '3':
    
            # New material for this cube
    
            material_new = bpy.data.materials.new(atom.name + "_F2+_vac")
            material_new.use_nodes = True
            mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF']
            mat_P_BSDF.inputs['Base Color'].default_value = [0.8, 0.0, 0.0, 1.0]
            mat_P_BSDF.inputs['Metallic'].default_value = 0.7
            mat_P_BSDF.inputs['Specular'].default_value = 0.0
            mat_P_BSDF.inputs['Roughness'].default_value = 0.65
            mat_P_BSDF.inputs['Clearcoat Roughness'].default_value = 0.0
            mat_P_BSDF.inputs['IOR'].default_value = 1.45
            mat_P_BSDF.inputs['Transmission'].default_value = 0.6
            mat_P_BSDF.inputs['Transmission Roughness'].default_value = 0.5
            mat_P_BSDF.inputs['Alpha'].default_value = 0.6
            # Some additional stuff for eevee.
            material_new.blend_method = 'HASHED'
            material_new.shadow_method = 'HASHED'
            material_new.use_backface_culling = False
            cube.active_material = material_new
    
            # Create now two electrons ... .
    
            scale = atom.scale / 10.0
            bpy.ops.surface.primitive_nurbs_surface_sphere_add(
    
                                            enter_editmode=False,
                                            location=(scale[0]*1.5,0.0,0.0),
                                            rotation=(0.0, 0.0, 0.0))
            electron1 = bpy.context.view_layer.objects.active
            electron1.scale = scale
    
            electron1.name = atom.name + "_F0_electron_1"
    
            electron1.parent = cube
            bpy.ops.surface.primitive_nurbs_surface_sphere_add(
    
                                            enter_editmode=False,
                                            location=(-scale[0]*1.5,0.0,0.0),
                                            rotation=(0.0, 0.0, 0.0))
            electron2 = bpy.context.view_layer.objects.active
            electron2.scale = scale
    
            electron2.name = atom.name + "_F0_electron_2"
    
            electron2.parent = cube
    
            # Create a new material for the two electrons.
    
            material_electron = bpy.data.materials.new(atom.name + "_F0-center")
    
            material_electron.use_nodes = True
            mat_P_BSDF = material_electron.node_tree.nodes['Principled BSDF']
            mat_P_BSDF.inputs['Base Color'].default_value = [0.0, 0.0, 0.8, 1.0]
            mat_P_BSDF.inputs['Metallic'].default_value = 0.8
            mat_P_BSDF.inputs['Specular'].default_value = 0.0
            mat_P_BSDF.inputs['Roughness'].default_value = 0.3
            mat_P_BSDF.inputs['Clearcoat Roughness'].default_value = 0.0
            mat_P_BSDF.inputs['IOR'].default_value = 1.45
            mat_P_BSDF.inputs['Transmission'].default_value = 0.6
            mat_P_BSDF.inputs['Transmission Roughness'].default_value = 0.5
            mat_P_BSDF.inputs['Alpha'].default_value = 1.0
            # Some additional stuff for eevee.
    
            material_electron.blend_method = 'OPAQUE'
    
            material_electron.shadow_method = 'OPAQUE'
            material_electron.use_backface_culling = False
            # We assign the materials to the two electrons.
    
            electron1.active_material = material_electron
            electron2.active_material = material_electron
    
    
            # Put two point lamps inside the electrons.
            lamp1_data = bpy.data.lights.new(name=atom.name + "_F0_lamp_1", type="POINT")
    
            lamp1_data.distance = atom.scale[0] * 2.0
    
            lamp1_data.energy = 20000.0
            lamp1_data.color = (0.8, 0.0, 0.0)
    
            lamp1 = bpy.data.objects.new(atom.name + "_F0_lamp", lamp1_data)
            lamp1.location = Vector((scale[0]*1.5, 0.0, 0.0))
            bpy.context.collection.objects.link(lamp1)
            lamp1.parent = cube
    
            lamp2_data = bpy.data.lights.new(name=atom.name + "_F0_lamp_2", type="POINT")
    
            lamp2_data.distance = atom.scale[0] * 2.0
    
            lamp2_data.energy = 20000.0
            lamp2_data.color = (0.8, 0.0, 0.0)
    
            lamp2 = bpy.data.objects.new(atom.name + "_F0_lamp", lamp2_data)
            lamp2.location = Vector((-scale[0]*1.5, 0.0, 0.0))
            bpy.context.collection.objects.link(lamp2)
            lamp2.parent = cube
    
            # The new 'atom' is the F0 defect complex + lamps
            new_atom = cube
    
            # Note the collection where all the new objects were placed into.
            # We use only one object, the cube
            coll_ori = get_collection_object(cube)
    
            # If it is not the same collection then ...
            if coll_ori != coll_new:
                # Put all new objects into the collection of 'atom' and ...
                coll_new.objects.link(cube)
                coll_new.objects.link(electron1)
                coll_new.objects.link(electron2)
                coll_new.objects.link(lamp1)
                coll_new.objects.link(lamp2)
                # ... unlink them from their original collection.
                coll_ori.objects.unlink(cube)
                coll_ori.objects.unlink(electron1)
                coll_ori.objects.unlink(electron2)
                coll_ori.objects.unlink(lamp1)
                coll_ori.objects.unlink(lamp2)
    
            coll_new.name = atom.name + "_F0_center"
    
            if atom.parent != None:
                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:
    
            else:
                if atom.type in {'SURFACE', 'MESH', 'META'}:
                    for element in ELEMENTS:
                        if element.name in atom.name:
    
                            obj = atom
                            e = element
                            FLAG = True
    
            if FLAG:
                obj.scale = (e.radii[0],) * 3
                mat = obj.active_material
                mat_P_BSDF = mat.node_tree.nodes['Principled BSDF']
    
                mat_P_BSDF.inputs['Base Color'].default_value = e.color
                mat_P_BSDF.subsurface_method = e.mat_P_BSDF.Subsurface_method
                mat_P_BSDF.distribution = e.mat_P_BSDF.Distribution
                mat_P_BSDF.inputs['Subsurface'].default_value = e.mat_P_BSDF.Subsurface
                mat_P_BSDF.inputs['Subsurface Color'].default_value = e.mat_P_BSDF.Subsurface_color
                mat_P_BSDF.inputs['Subsurface Radius'].default_value = e.mat_P_BSDF.Subsurface_radius
                mat_P_BSDF.inputs['Metallic'].default_value = e.mat_P_BSDF.Metallic
                mat_P_BSDF.inputs['Specular'].default_value = e.mat_P_BSDF.Specular
                mat_P_BSDF.inputs['Specular Tint'].default_value = e.mat_P_BSDF.Specular_tilt
                mat_P_BSDF.inputs['Roughness'].default_value = e.mat_P_BSDF.Roughness
                mat_P_BSDF.inputs['Anisotropic'].default_value = e.mat_P_BSDF.Anisotropic
                mat_P_BSDF.inputs['Anisotropic Rotation'].default_value = e.mat_P_BSDF.Anisotropic_rotation
                mat_P_BSDF.inputs['Sheen'].default_value = e.mat_P_BSDF.Sheen
                mat_P_BSDF.inputs['Sheen Tint'].default_value = e.mat_P_BSDF.Sheen_tint
                mat_P_BSDF.inputs['Clearcoat'].default_value = e.mat_P_BSDF.Clearcoat
                mat_P_BSDF.inputs['Clearcoat Roughness'].default_value = e.mat_P_BSDF.Clearcoat_rough
                mat_P_BSDF.inputs['IOR'].default_value = e.mat_P_BSDF.IOR
                mat_P_BSDF.inputs['Transmission'].default_value = e.mat_P_BSDF.Trans
                mat_P_BSDF.inputs['Transmission Roughness'].default_value = e.mat_P_BSDF.Trans_rough
                mat_P_BSDF.inputs['Emission'].default_value = e.mat_P_BSDF.Emission
                mat_P_BSDF.inputs['Emission Strength'].default_value = e.mat_P_BSDF.Emission_strength
                mat_P_BSDF.inputs['Alpha'].default_value = e.mat_P_BSDF.Alpha
    
                mat.use_backface_culling = e.mat_Eevee.use_backface
                mat.blend_method = e.mat_Eevee.blend_method
                mat.shadow_method = e.mat_Eevee.shadow_method
                mat.alpha_threshold = e.mat_Eevee.clip_threshold
                mat.use_screen_refraction = e.mat_Eevee.use_screen_refraction
                mat.refraction_depth = e.mat_Eevee.refraction_depth
                mat.use_sss_translucency = e.mat_Eevee.use_sss_translucency
                mat.pass_index = e.mat_Eevee.pass_index
    
                FLAG = False
    
    
    
    # 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:
    
    
                list_radii_ionic = []
                while True:
    
                    if len(line) in [0,1]:
                        break
    
                    # Number
                    if "Number                       :" in line:
                        pos = line.rfind(':') + 1
                        number = line[pos:].strip()
                    # Name
                    if "Name                         :" in line:
                        pos = line.rfind(':') + 1
                        name = line[pos:].strip()
                    # Short name
                    if "Short name                   :" in line:
                        pos = line.rfind(':') + 1
                        short_name = line[pos:].strip()
                    # Color
                    if "Color                        :" in line:
                        pos = line.rfind(':') + 1
                        color_value = line[pos:].strip().split(',')
                        color = [float(color_value[0]),
                                 float(color_value[1]),
                                 float(color_value[2]),
                                 float(color_value[3])]
                    # Used radius
                    if "Radius used                  :" in line:
                        pos = line.rfind(':') + 1
                        radius_used = float(line[pos:].strip())
                    # Covalent radius
                    if "Radius, covalent             :" in line:
                        pos = line.rfind(':') + 1
                        radius_covalent = float(line[pos:].strip())
                    # Atomic radius
                    if "Radius, atomic               :" in line:
                        pos = line.rfind(':') + 1
                        radius_atomic = float(line[pos:].strip())
                    if "Charge state                 :" in line:
                        pos = line.rfind(':') + 1
                        charge_state = float(line[pos:].strip())
                        line = data_file_p.readline()
                        pos = line.rfind(':') + 1
                        radius_ionic = float(line[pos:].strip())
                        list_radii_ionic.append(charge_state)
                        list_radii_ionic.append(radius_ionic)
    
                    # Some Principled BSDF properties
    
    
                    if "P BSDF Subsurface method     :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_subsurface_method = line[pos:].strip()
                    if "P BSDF Distribution          :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_distribution = line[pos:].strip()
                    if "P BSDF Subsurface            :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_subsurface = float(line[pos:].strip())
                    if "P BSDF Subsurface Color      :" in line:
                        pos = line.rfind(':') + 1
                        color_value = line[pos:].strip().split(',')
                        P_BSDF_subsurface_color = [float(color_value[0]),
                                                   float(color_value[1]),
                                                   float(color_value[2]),
                                                   float(color_value[3])]
                    if "P BSDF Subsurface Radius     :" in line:
                        pos = line.rfind(':') + 1
                        radii_values = line[pos:].strip().split(',')
                        P_BSDF_subsurface_radius = [float(color_value[0]),
                                                    float(color_value[1]),
                                                    float(color_value[2])]
                    if "P BSDF Metallic              :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_metallic = float(line[pos:].strip())
                    if "P BSDF Specular              :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_specular = float(line[pos:].strip())
                    if "P BSDF Specular Tilt         :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_specular_tilt = float(line[pos:].strip())
                    if "P BSDF Roughness             :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_roughness = float(line[pos:].strip())
                    if "P BSDF Anisotropic           :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_anisotropic = float(line[pos:].strip())
                    if "P BSDF Anisotropic Rotation  :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_anisotropic_rotation = float(line[pos:].strip())
                    if "P BSDF Sheen                 : " in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_sheen = float(line[pos:].strip())
                    if "P BSDF Sheen Tint            : " in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_sheen_tint = float(line[pos:].strip())
                    if "P BSDF Clearcoat             :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_clearcoat = float(line[pos:].strip())
                    if "P BSDF Clearcoat Rough       :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_clearcoat_roughness = float(line[pos:].strip())
                    if "P BSDF IOR                   :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_IOR = float(line[pos:].strip())
                    if "P BSDF Trans                 :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_transparency = float(line[pos:].strip())
                    if "P BSDF Trans Roughness       :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_transparency_roughness = float(line[pos:].strip())
                    if "P BSDF Emisssion             : " in line:
                        pos = line.rfind(':') + 1
                        color_value = line[pos:].strip().split(',')
                        P_BSDF_emission = [float(color_value[0]),
                                           float(color_value[1]),
                                           float(color_value[2]),
                                           float(color_value[3])]
                    if "P BSDF Emission Strength     :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_emission_strength = float(line[pos:].strip())
                    if "P BSDF Alpha                 :" in line:
                        pos = line.rfind(':') + 1
                        P_BSDF_alpha = float(line[pos:].strip())
    
                    if "Eevee Use Backface Culling   :" in line:
                        pos = line.rfind(':') + 1
                        line = line[pos:].strip()
                        if line.lower() in ("yes", "true", "1"):
                            Eevee_use_backface = True
                        else:
                            Eevee_use_backface = False
                    if "Eevee Blend Method           :" in line:
                        pos = line.rfind(':') + 1
                        Eevee_blend_method = line[pos:].strip()
                    if "Eevee Shadow Method          :" in line:
                        pos = line.rfind(':') + 1
                        Eevee_shadow_method = line[pos:].strip()
                    if "Eevee Clip Threshold         :" in line:
                        pos = line.rfind(':') + 1
                        Eevee_clip_threshold = float(line[pos:].strip())
                    if "Eevee Use Screen Refraction  :" in line:
                        pos = line.rfind(':') + 1
                        line = line[pos:].strip()
                        if line.lower() in ("yes", "true", "1"):
                            Eevee_use_screen_refraction = True
                        else:
                            Eevee_use_screen_refraction = False
                    if "Eevee Refraction depth       : " in line:
                        pos = line.rfind(':') + 1
                        Eevee_refraction_depth = float(line[pos:].strip())
                    if "Eevee Use SSS Translucency   :" in line:
                        pos = line.rfind(':') + 1
                        line = line[pos:].strip()
                        if line.lower() in ("yes", "true", "1"):
                            Eevee_use_sss_translucency = True
                        else:
                            Eevee_use_sss_translucency = False
                    if "Eevee Pass Index             :" in line:
                        pos = line.rfind(':') + 1
                        Eevee_pass_index = int(line[pos:].strip())
    
    
                    line = data_file_p.readline()
    
                list_radii = [radius_used, radius_covalent, radius_atomic]
    
                Eevee_material = EeveeProp(Eevee_use_backface,
                                           Eevee_blend_method,
                                           Eevee_shadow_method,
                                           Eevee_clip_threshold,
                                           Eevee_use_screen_refraction,
                                           Eevee_refraction_depth,
                                           Eevee_use_sss_translucency,
                                           Eevee_pass_index)
    
                P_BSDF_material = PBSDFProp(P_BSDF_subsurface_method,
                                            P_BSDF_distribution,
                                            P_BSDF_subsurface,
                                            P_BSDF_subsurface_color,
                                            P_BSDF_subsurface_radius,
                                            P_BSDF_metallic,
                                            P_BSDF_specular,
                                            P_BSDF_specular_tilt,
                                            P_BSDF_roughness,
                                            P_BSDF_anisotropic,
                                            P_BSDF_anisotropic_rotation,
                                            P_BSDF_sheen,
                                            P_BSDF_sheen_tint,
                                            P_BSDF_clearcoat,
                                            P_BSDF_clearcoat_roughness,
                                            P_BSDF_IOR,
                                            P_BSDF_transparency,
                                            P_BSDF_transparency_roughness,
                                            P_BSDF_emission,
                                            P_BSDF_emission_strength,
                                            P_BSDF_alpha)
    
                element = ElementProp(number,
                                      name,
                                      short_name,
                                      color,
                                      list_radii,
                                      list_radii_ionic,
                                      P_BSDF_material,
                                      Eevee_material)
    
    
                ELEMENTS.append(element)
    
        data_file_p.close()
    
        return True