Atomic Blender: Get nodes with their types instead of names #104403

Merged
Clemens Barth merged 1 commits from pioverfour/blender-addons:dp_fix_new_mat_issue_atomic into main 2023-02-09 23:02:29 +01:00
4 changed files with 41 additions and 21 deletions

View File

@ -43,7 +43,7 @@ bl_info = {
"name": "Atomic Blender PDB/XYZ", "name": "Atomic Blender PDB/XYZ",
"description": "Importing atoms listed in PDB or XYZ files as balls into Blender", "description": "Importing atoms listed in PDB or XYZ files as balls into Blender",
"author": "Clemens Barth", "author": "Clemens Barth",
"version": (1, 8), "version": (1, 8, 1),
"blender": (2, 80, 0), "blender": (2, 80, 0),
"location": "File -> Import -> PDB (.pdb) and File -> Import -> XYZ (.xyz)", "location": "File -> Import -> PDB (.pdb) and File -> Import -> XYZ (.xyz)",
"warning": "", "warning": "",

View File

@ -755,7 +755,8 @@ def draw_sticks_dupliverts(all_atoms,
if use_sticks_color == False: if use_sticks_color == False:
stick_material = bpy.data.materials.new(ELEMENTS[-1].name) stick_material = bpy.data.materials.new(ELEMENTS[-1].name)
stick_material.use_nodes = True stick_material.use_nodes = True
mat_P_BSDF = stick_material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in stick_material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = ELEMENTS[-1].color mat_P_BSDF.inputs['Base Color'].default_value = ELEMENTS[-1].color
# Sort the sticks and put them into a new list such that ... # Sort the sticks and put them into a new list such that ...
@ -1048,7 +1049,8 @@ def draw_sticks_skin(all_atoms,
stick_material = bpy.data.materials.new(ELEMENTS[-1].name) stick_material = bpy.data.materials.new(ELEMENTS[-1].name)
stick_material.use_nodes = True stick_material.use_nodes = True
mat_P_BSDF = stick_material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in stick_material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = ELEMENTS[-1].color mat_P_BSDF.inputs['Base Color'].default_value = ELEMENTS[-1].color
new_stick_mesh.active_material = stick_material new_stick_mesh.active_material = stick_material
@ -1105,7 +1107,8 @@ def draw_sticks_normal(all_atoms,
stick_material = bpy.data.materials.new(ELEMENTS[-1].name) stick_material = bpy.data.materials.new(ELEMENTS[-1].name)
stick_material.use_nodes = True stick_material.use_nodes = True
mat_P_BSDF = stick_material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in stick_material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = ELEMENTS[-1].color mat_P_BSDF.inputs['Base Color'].default_value = ELEMENTS[-1].color
up_axis = Vector([0.0, 0.0, 1.0]) up_axis = Vector([0.0, 0.0, 1.0])
@ -1332,7 +1335,8 @@ def import_pdb(Ball_type,
material = bpy.data.materials.new(atom_type[1]) material = bpy.data.materials.new(atom_type[1])
material.diffuse_color = atom_type[2] material.diffuse_color = atom_type[2]
material.use_nodes = True material.use_nodes = True
mat_P_BSDF = material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = atom_type[2] mat_P_BSDF.inputs['Base Color'].default_value = atom_type[2]
material.name = atom_type[0] material.name = atom_type[0]
atom_material_list.append(material) atom_material_list.append(material)
@ -1350,7 +1354,8 @@ def import_pdb(Ball_type,
if atom.name == "Vacancy": if atom.name == "Vacancy":
# For cycles and eevee. # For cycles and eevee.
material.use_nodes = True material.use_nodes = True
mat_P_BSDF = material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Metallic'].default_value = 0.1 mat_P_BSDF.inputs['Metallic'].default_value = 0.1
mat_P_BSDF.inputs['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Specular'].default_value = 0.15
mat_P_BSDF.inputs['Roughness'].default_value = 0.05 mat_P_BSDF.inputs['Roughness'].default_value = 0.05

View File

@ -493,7 +493,8 @@ def modify_objects(action_type,
else: else:
new_material = draw_obj_material('1', atom.active_material) new_material = draw_obj_material('1', atom.active_material)
# Assign now the correct color. # Assign now the correct color.
mat_P_BSDF = new_material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in new_material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = element.color mat_P_BSDF.inputs['Base Color'].default_value = element.color
new_material.name = element.name + "_normal" new_material.name = element.name + "_normal"
@ -580,7 +581,8 @@ def separate_atoms(scn):
# Prepare a new material # Prepare a new material
def draw_obj_material(material_type, material): def draw_obj_material(material_type, material):
mat_P_BSDF_default = material.node_tree.nodes['Principled BSDF'] mat_P_BSDF_default = next(n for n in material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
default_color = mat_P_BSDF_default.inputs['Base Color'].default_value default_color = mat_P_BSDF_default.inputs['Base Color'].default_value
if material_type == '0': # Unchanged if material_type == '0': # Unchanged
@ -591,7 +593,8 @@ def draw_obj_material(material_type, material):
# user's work in Blender ... . # user's work in Blender ... .
material_new = bpy.data.materials.new(material.name + "_normal") material_new = bpy.data.materials.new(material.name + "_normal")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = default_color mat_P_BSDF.inputs['Base Color'].default_value = default_color
mat_P_BSDF.inputs['Metallic'].default_value = 0.0 mat_P_BSDF.inputs['Metallic'].default_value = 0.0
mat_P_BSDF.inputs['Specular'].default_value = 0.5 mat_P_BSDF.inputs['Specular'].default_value = 0.5
@ -607,7 +610,8 @@ def draw_obj_material(material_type, material):
if material_type == '2': # Transparent if material_type == '2': # Transparent
material_new = bpy.data.materials.new(material.name + "_transparent") material_new = bpy.data.materials.new(material.name + "_transparent")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = default_color mat_P_BSDF.inputs['Base Color'].default_value = default_color
mat_P_BSDF.inputs['Metallic'].default_value = 0.0 mat_P_BSDF.inputs['Metallic'].default_value = 0.0
mat_P_BSDF.inputs['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Specular'].default_value = 0.15
@ -624,7 +628,8 @@ def draw_obj_material(material_type, material):
if material_type == '3': # Reflecting if material_type == '3': # Reflecting
material_new = bpy.data.materials.new(material.name + "_reflecting") material_new = bpy.data.materials.new(material.name + "_reflecting")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = default_color mat_P_BSDF.inputs['Base Color'].default_value = default_color
mat_P_BSDF.inputs['Metallic'].default_value = 0.7 mat_P_BSDF.inputs['Metallic'].default_value = 0.7
mat_P_BSDF.inputs['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Specular'].default_value = 0.15
@ -640,7 +645,8 @@ def draw_obj_material(material_type, material):
if material_type == '4': # Transparent + reflecting if material_type == '4': # Transparent + reflecting
material_new = bpy.data.materials.new(material.name + "_trans+refl") material_new = bpy.data.materials.new(material.name + "_trans+refl")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = default_color mat_P_BSDF.inputs['Base Color'].default_value = default_color
mat_P_BSDF.inputs['Metallic'].default_value = 0.5 mat_P_BSDF.inputs['Metallic'].default_value = 0.5
mat_P_BSDF.inputs['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Specular'].default_value = 0.15
@ -933,7 +939,8 @@ def draw_obj_special(atom_shape, atom):
# Get the color of the selected atom. # Get the color of the selected atom.
material = atom.active_material material = atom.active_material
mat_P_BSDF_default = material.node_tree.nodes['Principled BSDF'] mat_P_BSDF_default = next(n for n in material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
default_color = mat_P_BSDF_default.inputs['Base Color'].default_value default_color = mat_P_BSDF_default.inputs['Base Color'].default_value
# Create first a cube # Create first a cube
@ -952,7 +959,8 @@ def draw_obj_special(atom_shape, atom):
# New material for this cube # New material for this cube
material_new = bpy.data.materials.new(atom.name + "_F2+_vac") material_new = bpy.data.materials.new(atom.name + "_F2+_vac")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = default_color mat_P_BSDF.inputs['Base Color'].default_value = default_color
mat_P_BSDF.inputs['Metallic'].default_value = 0.7 mat_P_BSDF.inputs['Metallic'].default_value = 0.7
mat_P_BSDF.inputs['Specular'].default_value = 0.0 mat_P_BSDF.inputs['Specular'].default_value = 0.0
@ -1008,7 +1016,8 @@ def draw_obj_special(atom_shape, atom):
# New material for this cube # New material for this cube
material_new = bpy.data.materials.new(atom.name + "_F2+_vac") material_new = bpy.data.materials.new(atom.name + "_F2+_vac")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = [0.0, 0.0, 0.8, 1.0] mat_P_BSDF.inputs['Base Color'].default_value = [0.0, 0.0, 0.8, 1.0]
mat_P_BSDF.inputs['Metallic'].default_value = 0.7 mat_P_BSDF.inputs['Metallic'].default_value = 0.7
mat_P_BSDF.inputs['Specular'].default_value = 0.0 mat_P_BSDF.inputs['Specular'].default_value = 0.0
@ -1038,7 +1047,8 @@ def draw_obj_special(atom_shape, atom):
# New material for the electron # New material for the electron
material_electron = bpy.data.materials.new(atom.name + "_F+-center") material_electron = bpy.data.materials.new(atom.name + "_F+-center")
material_electron.use_nodes = True material_electron.use_nodes = True
mat_P_BSDF = material_electron.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_electron.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = [0.0, 0.0, 0.8, 1.0] 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['Metallic'].default_value = 0.8
mat_P_BSDF.inputs['Specular'].default_value = 0.0 mat_P_BSDF.inputs['Specular'].default_value = 0.0
@ -1097,7 +1107,8 @@ def draw_obj_special(atom_shape, atom):
# New material for this cube # New material for this cube
material_new = bpy.data.materials.new(atom.name + "_F2+_vac") material_new = bpy.data.materials.new(atom.name + "_F2+_vac")
material_new.use_nodes = True material_new.use_nodes = True
mat_P_BSDF = material_new.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_new.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = [0.8, 0.0, 0.0, 1.0] 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['Metallic'].default_value = 0.7
mat_P_BSDF.inputs['Specular'].default_value = 0.0 mat_P_BSDF.inputs['Specular'].default_value = 0.0
@ -1136,7 +1147,8 @@ def draw_obj_special(atom_shape, atom):
# Create a new material for the two electrons. # Create a new material for the two electrons.
material_electron = bpy.data.materials.new(atom.name + "_F0-center") material_electron = bpy.data.materials.new(atom.name + "_F0-center")
material_electron.use_nodes = True material_electron.use_nodes = True
mat_P_BSDF = material_electron.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material_electron.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = [0.0, 0.0, 0.8, 1.0] 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['Metallic'].default_value = 0.8
mat_P_BSDF.inputs['Specular'].default_value = 0.0 mat_P_BSDF.inputs['Specular'].default_value = 0.0
@ -1263,7 +1275,8 @@ def custom_datafile_change_atom_props():
if FLAG: if FLAG:
obj.scale = (e.radii[0],) * 3 obj.scale = (e.radii[0],) * 3
mat = obj.active_material mat = obj.active_material
mat_P_BSDF = mat.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in mat.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = e.color mat_P_BSDF.inputs['Base Color'].default_value = e.color
mat_P_BSDF.subsurface_method = e.mat_P_BSDF.Subsurface_method mat_P_BSDF.subsurface_method = e.mat_P_BSDF.Subsurface_method

View File

@ -472,7 +472,8 @@ def import_xyz(Ball_type,
material = bpy.data.materials.new(atom.name) material = bpy.data.materials.new(atom.name)
material.diffuse_color = atom.color material.diffuse_color = atom.color
material.use_nodes = True material.use_nodes = True
mat_P_BSDF = material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Base Color'].default_value = atom.color mat_P_BSDF.inputs['Base Color'].default_value = atom.color
material.name = atom.name material.name = atom.name
atom_material_list.append(material) atom_material_list.append(material)
@ -491,7 +492,8 @@ def import_xyz(Ball_type,
if atom.name == "Vacancy": if atom.name == "Vacancy":
# For cycles and eevee. # For cycles and eevee.
material.use_nodes = True material.use_nodes = True
mat_P_BSDF = material.node_tree.nodes['Principled BSDF'] mat_P_BSDF = next(n for n in material.node_tree.nodes
if n.type == "BSDF_PRINCIPLED")
mat_P_BSDF.inputs['Metallic'].default_value = 0.1 mat_P_BSDF.inputs['Metallic'].default_value = 0.1
mat_P_BSDF.inputs['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Specular'].default_value = 0.15
mat_P_BSDF.inputs['Roughness'].default_value = 0.05 mat_P_BSDF.inputs['Roughness'].default_value = 0.05