1 # SPDX-FileCopyrightText: 2023 Robin Hohnsbeen
3 # SPDX-License-Identifier: GPL-3.0-or-later
8 def get_vdm_bake_material():
9 """Creates a material that is used to bake the displacement from a plane against its UVs.
12 material: Baking material
14 material_name
= 'VDM_baking_material'
15 if material_name
in bpy
.data
.materials
:
16 # Recreate material every time to ensure it is unchanged by the user which could lead to issues.
17 # I would like to keep it directly after bake though so people could look
18 # at how it is made, if they are interested.
19 bpy
.data
.materials
.remove(bpy
.data
.materials
[material_name
])
21 new_material
= bpy
.data
.materials
.new(name
=material_name
)
23 new_material
.use_nodes
= True
24 nodes
= new_material
.node_tree
.nodes
25 principled_node
= next(n
for n
in new_material
.node_tree
.nodes
if n
.type == "BSDF_PRINCIPLED")
26 nodes
.remove(principled_node
)
27 material_output
= next(n
for n
in new_material
.node_tree
.nodes
if n
.type == "OUTPUT_MATERIAL")
29 # Create relevant nodes
30 combine_node
= nodes
.new('ShaderNodeCombineXYZ')
32 separate_node1
= nodes
.new('ShaderNodeSeparateXYZ')
33 separate_node2
= nodes
.new('ShaderNodeSeparateXYZ')
35 vector_subtract_node
= nodes
.new('ShaderNodeVectorMath')
36 vector_subtract_node
.operation
= 'SUBTRACT'
38 vector_multiply_node
= nodes
.new('ShaderNodeVectorMath')
39 vector_multiply_node
.operation
= 'MULTIPLY'
40 vector_multiply_node
.inputs
[1].default_value
= [2.0, 2.0, 2.0]
42 vector_add_node
= nodes
.new('ShaderNodeVectorMath')
43 vector_add_node
.operation
= 'ADD'
44 vector_add_node
.inputs
[1].default_value
= [-0.5, -0.5, -0.5]
46 tex_coord_node
= nodes
.new('ShaderNodeTexCoord')
48 image_node
= nodes
.new('ShaderNodeTexImage')
49 image_node
.name
= "VDMTexture"
52 tree
= new_material
.node_tree
53 tree
.links
.new(combine_node
.outputs
[0], material_output
.inputs
[0])
55 tree
.links
.new(separate_node1
.outputs
[0], combine_node
.inputs
[0])
56 tree
.links
.new(separate_node1
.outputs
[1], combine_node
.inputs
[1])
59 vector_subtract_node
.outputs
[0], separate_node1
.inputs
[0])
62 vector_multiply_node
.outputs
[0], vector_subtract_node
.inputs
[1])
65 vector_add_node
.outputs
[0], vector_multiply_node
.inputs
[0])
67 tree
.links
.new(tex_coord_node
.outputs
[2], vector_add_node
.inputs
[0])
69 tex_coord_node
.outputs
[3], vector_subtract_node
.inputs
[0])
70 tree
.links
.new(tex_coord_node
.outputs
[3], separate_node2
.inputs
[0])
71 tree
.links
.new(separate_node2
.outputs
[2], combine_node
.inputs
[2])
73 return bpy
.data
.materials
[material_name
]