From 933b5179c0198897743956f3e9e4db721ad6bb85 Mon Sep 17 00:00:00 2001 From: Damien Picard Date: Thu, 26 Jan 2023 21:35:10 +0100 Subject: [PATCH] Atomic Blender: Get nodes with their types instead of names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using hardcoded names to retrieve nodes can result in errors if the user has enabled UI translation, because the new nodes may not have the expected English names. Instead, we retrieve the nodes using their types (BSDF_PRINCIPLED or OUTPUT_MATERIAL). Ref blender/blender#104145 --- io_mesh_atomic/__init__.py | 2 +- io_mesh_atomic/pdb_import.py | 15 ++++++++----- io_mesh_atomic/utility_panel.py | 39 ++++++++++++++++++++++----------- io_mesh_atomic/xyz_import.py | 6 +++-- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/io_mesh_atomic/__init__.py b/io_mesh_atomic/__init__.py index 9fed6c99d..3c2e08483 100644 --- a/io_mesh_atomic/__init__.py +++ b/io_mesh_atomic/__init__.py @@ -43,7 +43,7 @@ bl_info = { "name": "Atomic Blender PDB/XYZ", "description": "Importing atoms listed in PDB or XYZ files as balls into Blender", "author": "Clemens Barth", - "version": (1, 8), + "version": (1, 8, 1), "blender": (2, 80, 0), "location": "File -> Import -> PDB (.pdb) and File -> Import -> XYZ (.xyz)", "warning": "", diff --git a/io_mesh_atomic/pdb_import.py b/io_mesh_atomic/pdb_import.py index 589776aab..01762a5ac 100644 --- a/io_mesh_atomic/pdb_import.py +++ b/io_mesh_atomic/pdb_import.py @@ -755,7 +755,8 @@ def draw_sticks_dupliverts(all_atoms, if use_sticks_color == False: stick_material = bpy.data.materials.new(ELEMENTS[-1].name) 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 # 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.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 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.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 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.diffuse_color = atom_type[2] 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] material.name = atom_type[0] atom_material_list.append(material) @@ -1350,7 +1354,8 @@ def import_pdb(Ball_type, if atom.name == "Vacancy": # For cycles and eevee. 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['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Roughness'].default_value = 0.05 diff --git a/io_mesh_atomic/utility_panel.py b/io_mesh_atomic/utility_panel.py index 9f6b91fea..dcab8df93 100644 --- a/io_mesh_atomic/utility_panel.py +++ b/io_mesh_atomic/utility_panel.py @@ -493,7 +493,8 @@ def modify_objects(action_type, else: new_material = draw_obj_material('1', atom.active_material) # 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 new_material.name = element.name + "_normal" @@ -580,7 +581,8 @@ def separate_atoms(scn): # Prepare a new 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 if material_type == '0': # Unchanged @@ -591,7 +593,8 @@ def draw_obj_material(material_type, material): # user's work in Blender ... . material_new = bpy.data.materials.new(material.name + "_normal") 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['Metallic'].default_value = 0.0 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 material_new = bpy.data.materials.new(material.name + "_transparent") 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['Metallic'].default_value = 0.0 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 material_new = bpy.data.materials.new(material.name + "_reflecting") 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['Metallic'].default_value = 0.7 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 material_new = bpy.data.materials.new(material.name + "_trans+refl") 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['Metallic'].default_value = 0.5 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. 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 # Create first a cube @@ -952,7 +959,8 @@ def draw_obj_special(atom_shape, atom): # 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 = 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['Metallic'].default_value = 0.7 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 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 = 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['Metallic'].default_value = 0.7 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 material_electron = bpy.data.materials.new(atom.name + "_F+-center") 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['Metallic'].default_value = 0.8 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 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 = 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['Metallic'].default_value = 0.7 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. 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 = 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['Metallic'].default_value = 0.8 mat_P_BSDF.inputs['Specular'].default_value = 0.0 @@ -1263,7 +1275,8 @@ def custom_datafile_change_atom_props(): if FLAG: obj.scale = (e.radii[0],) * 3 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.subsurface_method = e.mat_P_BSDF.Subsurface_method diff --git a/io_mesh_atomic/xyz_import.py b/io_mesh_atomic/xyz_import.py index 6a2bb5a22..3ea284a3c 100644 --- a/io_mesh_atomic/xyz_import.py +++ b/io_mesh_atomic/xyz_import.py @@ -472,7 +472,8 @@ def import_xyz(Ball_type, material = bpy.data.materials.new(atom.name) material.diffuse_color = atom.color 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 material.name = atom.name atom_material_list.append(material) @@ -491,7 +492,8 @@ def import_xyz(Ball_type, if atom.name == "Vacancy": # For cycles and eevee. 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['Specular'].default_value = 0.15 mat_P_BSDF.inputs['Roughness'].default_value = 0.05 -- 2.30.2