From aa29a26392e2c0b02b34cfae9b8063f537d531ec Mon Sep 17 00:00:00 2001 From: Germano Cavalcante <=> Date: Thu, 30 Nov 2023 11:34:39 -0300 Subject: [PATCH] Fix #105028: Error when trying to create texture with VDM Brush Baker The material that is used for baking is kept inside the blend files so users can potentially change it and thus create errors in the addon. It will now be recreated every time a brush is baked to ensure it is unchanged. There was another issue that can happen in language settings other than English. Some nodes were referred to by their name. Now they are searched by type. --- vdm_brush_baker/__init__.py | 5 +-- vdm_brush_baker/bakematerial.py | 80 ++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/vdm_brush_baker/__init__.py b/vdm_brush_baker/__init__.py index 3576214a2..ebea91809 100644 --- a/vdm_brush_baker/__init__.py +++ b/vdm_brush_baker/__init__.py @@ -16,7 +16,7 @@ bl_info = { 'author': 'Robin Hohnsbeen', 'description': 'Bake vector displacement brushes easily from a plane', 'blender': (3, 5, 0), - 'version': (1, 0, 2), + 'version': (1, 0, 3), 'location': 'Sculpt Mode: View3D > Sidebar > Tool Tab', 'warning': '', 'category': 'Baking', @@ -88,7 +88,6 @@ class PT_VDMBaker(bpy.types.Panel): layout.use_property_split = True layout.use_property_decorate = False - layout.operator(create_sculpt_plane.bl_idname, icon='ADD') layout.separator() @@ -187,7 +186,6 @@ class create_vdm_brush(bpy.types.Operator): return {'CANCELLED'} vdm_plane = context.active_object - addon_data = get_addon_data() new_brush_name = addon_data.draft_brush_name reference_brush_name = addon_data.draft_brush_name @@ -214,6 +212,7 @@ class create_vdm_brush(bpy.types.Operator): bpy.ops.object.mode_set(mode='OBJECT') vdm_bake_material = bakematerial.get_vdm_bake_material() + vdm_texture_image = None try: # Prepare baking scene.render.engine = 'CYCLES' diff --git a/vdm_brush_baker/bakematerial.py b/vdm_brush_baker/bakematerial.py index 4e7b7e5b6..85f991369 100644 --- a/vdm_brush_baker/bakematerial.py +++ b/vdm_brush_baker/bakematerial.py @@ -12,56 +12,62 @@ def get_vdm_bake_material(): material: Baking material """ material_name = 'VDM_baking_material' - if material_name not in bpy.data.materials: - new_material = bpy.data.materials.new(name=material_name) + if material_name in bpy.data.materials: + # Recreate material every time to ensure it is unchanged by the user which could lead to issues. + # I would like to keep it directly after bake though so people could look + # at how it is made, if they are interested. + bpy.data.materials.remove(bpy.data.materials[material_name]) - new_material.use_nodes = True - nodes = new_material.node_tree.nodes - nodes.remove(nodes['Principled BSDF']) - material_output = nodes['Material Output'] + new_material = bpy.data.materials.new(name=material_name) - # Create relevant nodes - combine_node = nodes.new('ShaderNodeCombineXYZ') + new_material.use_nodes = True + nodes = new_material.node_tree.nodes + principled_node = next(n for n in new_material.node_tree.nodes if n.type == "BSDF_PRINCIPLED") + nodes.remove(principled_node) + material_output = next(n for n in new_material.node_tree.nodes if n.type == "OUTPUT_MATERIAL") - separate_node1 = nodes.new('ShaderNodeSeparateXYZ') - separate_node2 = nodes.new('ShaderNodeSeparateXYZ') + # Create relevant nodes + combine_node = nodes.new('ShaderNodeCombineXYZ') - vector_subtract_node = nodes.new('ShaderNodeVectorMath') - vector_subtract_node.operation = 'SUBTRACT' + separate_node1 = nodes.new('ShaderNodeSeparateXYZ') + separate_node2 = nodes.new('ShaderNodeSeparateXYZ') - vector_multiply_node = nodes.new('ShaderNodeVectorMath') - vector_multiply_node.operation = 'MULTIPLY' - vector_multiply_node.inputs[1].default_value = [2.0, 2.0, 2.0] + vector_subtract_node = nodes.new('ShaderNodeVectorMath') + vector_subtract_node.operation = 'SUBTRACT' - vector_add_node = nodes.new('ShaderNodeVectorMath') - vector_add_node.operation = 'ADD' - vector_add_node.inputs[1].default_value = [-0.5, -0.5, -0.5] + vector_multiply_node = nodes.new('ShaderNodeVectorMath') + vector_multiply_node.operation = 'MULTIPLY' + vector_multiply_node.inputs[1].default_value = [2.0, 2.0, 2.0] - tex_coord_node = nodes.new('ShaderNodeTexCoord') + vector_add_node = nodes.new('ShaderNodeVectorMath') + vector_add_node.operation = 'ADD' + vector_add_node.inputs[1].default_value = [-0.5, -0.5, -0.5] - image_node = nodes.new('ShaderNodeTexImage') - image_node.name = "VDMTexture" + tex_coord_node = nodes.new('ShaderNodeTexCoord') - # Connect nodes - tree = new_material.node_tree - tree.links.new(combine_node.outputs[0], material_output.inputs[0]) + image_node = nodes.new('ShaderNodeTexImage') + image_node.name = "VDMTexture" - tree.links.new(separate_node1.outputs[0], combine_node.inputs[0]) - tree.links.new(separate_node1.outputs[1], combine_node.inputs[1]) + # Connect nodes + tree = new_material.node_tree + tree.links.new(combine_node.outputs[0], material_output.inputs[0]) - tree.links.new( - vector_subtract_node.outputs[0], separate_node1.inputs[0]) + tree.links.new(separate_node1.outputs[0], combine_node.inputs[0]) + tree.links.new(separate_node1.outputs[1], combine_node.inputs[1]) - tree.links.new( - vector_multiply_node.outputs[0], vector_subtract_node.inputs[1]) + tree.links.new( + vector_subtract_node.outputs[0], separate_node1.inputs[0]) - tree.links.new( - vector_add_node.outputs[0], vector_multiply_node.inputs[0]) + tree.links.new( + vector_multiply_node.outputs[0], vector_subtract_node.inputs[1]) - tree.links.new(tex_coord_node.outputs[2], vector_add_node.inputs[0]) - tree.links.new( - tex_coord_node.outputs[3], vector_subtract_node.inputs[0]) - tree.links.new(tex_coord_node.outputs[3], separate_node2.inputs[0]) - tree.links.new(separate_node2.outputs[2], combine_node.inputs[2]) + tree.links.new( + vector_add_node.outputs[0], vector_multiply_node.inputs[0]) + + tree.links.new(tex_coord_node.outputs[2], vector_add_node.inputs[0]) + tree.links.new( + tex_coord_node.outputs[3], vector_subtract_node.inputs[0]) + tree.links.new(tex_coord_node.outputs[3], separate_node2.inputs[0]) + tree.links.new(separate_node2.outputs[2], combine_node.inputs[2]) return bpy.data.materials[material_name] -- 2.30.2