-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender_material.py
215 lines (183 loc) · 7.9 KB
/
blender_material.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import bpy
# mat = bpy.data.materials.new(name = "FullColorMat")
# mat.use_nodes = True
#initialize defaultMat node group
def Full_color_texture(mat, albedo_texture_file, tactile_texture_file):
defaultmat = mat.node_tree
# Clean node tree
for node in defaultmat.nodes:
defaultmat.nodes.remove(node)
# Initialize nodes
material_output = defaultmat.nodes.new("ShaderNodeOutputMaterial")
material_output.name = "Material Output"
material_output.is_active_output = True
# Image Texture for Albedo
image_texture = defaultmat.nodes.new("ShaderNodeTexImage")
image_texture.name = "Image Texture"
image_texture.extension = 'REPEAT'
image_texture.interpolation = 'Linear'
image_texture.image = bpy.data.images.load(albedo_texture_file)
# Principled BSDF
principled_bsdf = defaultmat.nodes.new("ShaderNodeBsdfPrincipled")
principled_bsdf.name = "Principled BSDF"
principled_bsdf.inputs['Roughness'].default_value = 0.5
# Normal Map Setup - Updated for Blender 4.0+
normal_map = defaultmat.nodes.new("ShaderNodeNormalMap")
normal_map.name = "Normal Map"
normal_map.space = 'TANGENT'
# In Blender 4.0+, strength is handled differently
normal_map.inputs['Strength'].default_value = 1.0
# Tactile Image Texture (Normal Map)
tactile_image = defaultmat.nodes.new("ShaderNodeTexImage")
tactile_image.name = "Tactile Image"
tactile_image.extension = 'REPEAT'
tactile_image.interpolation = 'Linear'
if tactile_texture_file is not None:
tactile_image.image = bpy.data.images.load(tactile_texture_file)
# Important change for Blender 4.0+: Explicitly set to Non-Color
tactile_image.image.colorspace_settings.name = 'Non-Color'
# Add UV Map node for proper tangent space handling
uv_map = defaultmat.nodes.new('ShaderNodeUVMap')
uv_map.uv_map = "UVMap" # Use your UV map name here
else:
normal_map.inputs['Strength'].default_value = 0.0
# Node locations
material_output.location = (430, 300)
image_texture.location = (-480, 300)
principled_bsdf.location = (150, 300)
normal_map.location = (-80, -225)
tactile_image.location = (-488, -130)
if tactile_texture_file is not None:
uv_map.location = (-680, -130)
# Create links
defaultmat.links.new(image_texture.outputs['Color'], principled_bsdf.inputs['Base Color'])
defaultmat.links.new(principled_bsdf.outputs['BSDF'], material_output.inputs['Surface'])
defaultmat.links.new(normal_map.outputs['Normal'], principled_bsdf.inputs['Normal'])
defaultmat.links.new(tactile_image.outputs['Color'], normal_map.inputs['Color'])
if tactile_texture_file is not None:
defaultmat.links.new(uv_map.outputs['UV'], tactile_image.inputs['Vector'])
return defaultmat
# defaultmat = defaultmat_node_group()
# mat = bpy.data.materials.new(name = "normal_render")
# mat.use_nodes = True
#initialize normal_render node group
def normal_render_texture(mat, tactile_texture_file):
normal_render = mat.node_tree
#start with a clean node tree
for node in normal_render.nodes:
normal_render.nodes.remove(node)
#initialize normal_render nodes
#node Normal Map
normal_map = normal_render.nodes.new("ShaderNodeNormalMap")
normal_map.name = "Normal Map"
normal_map.space = 'TANGENT'
normal_map.uv_map = ""
#Strength
normal_map.inputs[0].default_value = 1.0
#node Image Texture.001
image_texture_001 = normal_render.nodes.new("ShaderNodeTexImage")
image_texture_001.label = "tactile"
image_texture_001.name = "Image Texture.001"
image_texture_001.extension = 'REPEAT'
image_texture_001.image_user.frame_current = 1
image_texture_001.image_user.frame_duration = 1
image_texture_001.image_user.frame_offset = -1
image_texture_001.image_user.frame_start = 1
image_texture_001.image_user.tile = 0
image_texture_001.image_user.use_auto_refresh = False
image_texture_001.image_user.use_cyclic = False
image_texture_001.interpolation = 'Linear'
image_texture_001.projection = 'FLAT'
image_texture_001.projection_blend = 0.0
if tactile_texture_file is not None:
image_texture_001.image = bpy.data.images.load(tactile_texture_file)
image_texture_001.image.colorspace_settings.name = 'Non-Color'
else:
normal_map.inputs[0].default_value = 0.0
#Vector
image_texture_001.inputs[0].default_value = (0.0, 0.0, 0.0)
#node Gamma
gamma = normal_render.nodes.new("ShaderNodeGamma")
gamma.name = "Gamma"
#Gamma
gamma.inputs[1].default_value = 2.200000047683716
#node Material Output
material_output = normal_render.nodes.new("ShaderNodeOutputMaterial")
material_output.name = "Material Output"
material_output.is_active_output = True
material_output.target = 'ALL'
#Displacement
material_output.inputs[2].default_value = (0.0, 0.0, 0.0)
#Thickness
material_output.inputs[3].default_value = 0.0
#node Vector Transform
vector_transform = normal_render.nodes.new("ShaderNodeVectorTransform")
vector_transform.name = "Vector Transform"
vector_transform.convert_from = 'WORLD'
vector_transform.convert_to = 'CAMERA'
vector_transform.vector_type = 'NORMAL'
#node Mapping
mapping = normal_render.nodes.new("ShaderNodeMapping")
mapping.name = "Mapping"
mapping.vector_type = 'TEXTURE'
#Location
mapping.inputs[1].default_value = (-1.0, -1.0, 1.0)
#Rotation
mapping.inputs[2].default_value = (0.0, 0.0, 0.0)
#Scale
mapping.inputs[3].default_value = (2.0, 2.0, -2.0)
#Set locations
image_texture_001.location = (-658.6141967773438, 372.88507080078125)
gamma.location = (420.29791259765625, 367.7361145019531)
material_output.location = (624.8310546875, 362.5605773925781)
vector_transform.location = (-104.94386291503906, 330.3135070800781)
mapping.location = (145.96263122558594, 450.2762451171875)
normal_map.location = (-337.320068359375, 296.4747619628906)
#Set dimensions
image_texture_001.width, image_texture_001.height = 240.0, 100.0
gamma.width, gamma.height = 140.0, 100.0
material_output.width, material_output.height = 140.0, 100.0
vector_transform.width, vector_transform.height = 140.0, 100.0
mapping.width, mapping.height = 140.0, 100.0
normal_map.width, normal_map.height = 150.0, 100.0
#initialize normal_render links
#image_texture_001.Color -> normal_map.Color
normal_render.links.new(image_texture_001.outputs[0], normal_map.inputs[1])
#normal_map.Normal -> vector_transform.Vector
normal_render.links.new(normal_map.outputs[0], vector_transform.inputs[0])
#gamma.Color -> material_output.Surface
normal_render.links.new(gamma.outputs[0], material_output.inputs[0])
#vector_transform.Vector -> mapping.Vector
normal_render.links.new(vector_transform.outputs[0], mapping.inputs[0])
#mapping.Vector -> gamma.Color
normal_render.links.new(mapping.outputs[0], gamma.inputs[0])
return normal_render
def albedo_render_texture(mat, albedo_texture_file):
albedo_render = mat.node_tree
# Start with a clean node tree
for node in albedo_render.nodes:
albedo_render.nodes.remove(node)
# Initialize nodes
#node Albedo Map
image_texture = albedo_render.nodes.new("ShaderNodeTexImage")
image_texture.label = "albedo"
image_texture.name = "Image Texture"
image_texture.extension = 'REPEAT'
image_texture.interpolation = 'Linear'
image_texture.image = bpy.data.images.load(albedo_texture_file)
#node Emission
emission = albedo_render.nodes.new("ShaderNodeEmission")
emission.name = "Emission"
emission.inputs['Strength'].default_value = 1.0
#node Material Output
material_output = albedo_render.nodes.new("ShaderNodeOutputMaterial")
material_output.name = "Material Output"
material_output.is_active_output = True
# Node locations
image_texture.location = (-480, 300)
emission.location = (150, 300)
material_output.location = (430, 300)
# Create links
albedo_render.links.new(image_texture.outputs['Color'], emission.inputs['Color'])
albedo_render.links.new(emission.outputs['Emission'], material_output.inputs['Surface'])
return albedo_render