forked from JoseConseco/GoB
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnodes.py
More file actions
110 lines (94 loc) · 4.73 KB
/
nodes.py
File metadata and controls
110 lines (94 loc) · 4.73 KB
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from . import utils
def create_base_nodes(mat):
""" Create the base nodes for the material. """
mat.use_nodes = True
nodes = mat.node_tree.nodes
output_node = nodes.get('Material Output')
shader_node = nodes.get('Principled BSDF')
#create main shader node if it does not exist to attach the texture nodes to
if not output_node:
output_node = nodes.new('ShaderNodeOutputMaterial')
output_node.location = 400, 400
if not shader_node:
shader_node = nodes.new('ShaderNodeBsdfPrincipled')
shader_node.location = 0, 400
mat.node_tree.links.new(output_node.inputs[0], shader_node.outputs[0])
return nodes,output_node,shader_node
def material_fromm_texture(mat, diff_texture=None, norm_texture=None, disp_texture=None):
""" Create the texture nodes and connect them to the shader node.
"""
nodes, output_node, shader_node = create_base_nodes(mat)
node_cache = [node.label for node in nodes]
# create the Diffiuse Color nodes
#image_node = nodes.get('Image Texture') # ShaderNodeTexImage Image Texture
diff_texture_node = None
if 'Diffuse Color Map' not in node_cache:
diff_texture_node = nodes.new('ShaderNodeTexImage')
diff_texture_node.location = -700, 500
diff_texture_node.image = diff_texture
diff_texture_node.label = 'Diffuse Color Map'
if diff_texture:
diff_texture_node.image.colorspace_settings.name = utils.prefs().import_diffuse_colorspace
mat.node_tree.links.new(shader_node.inputs[0], diff_texture_node.outputs[0])
# create the Normal Map nodes
norm_node = nodes.get('Normal Map') # ShaderNodeNormalMap Normal Map
if not norm_node:
norm_node = nodes.new('ShaderNodeNormalMap')
norm_node.location = -300, -100
if bpy.app.version < (3,1,0):
mat.node_tree.links.new(shader_node.inputs[20], norm_node.outputs[0])
else:
mat.node_tree.links.new(shader_node.inputs[22], norm_node.outputs[0])
norm_texture_node = None
if 'Normal Map' not in node_cache:
norm_texture_node = nodes.new('ShaderNodeTexImage')
norm_texture_node.location = -700, -100
norm_texture_node.image = norm_texture
norm_texture_node.label = 'Normal Map'
if norm_texture:
norm_texture_node.image.colorspace_settings.name = utils.prefs().import_normal_colorspace
mat.node_tree.links.new(norm_node.inputs[1], norm_texture_node.outputs[0])
# create the Displacement nodes
disp_node = nodes.get('Displacement') # ShaderNodeDisplacement Displacement
if not disp_node:
disp_node = nodes.new('ShaderNodeDisplacement')
disp_node.location = -300, 200
mat.node_tree.links.new(output_node.inputs[2], disp_node.outputs[0])
disp_texture_node = None
if 'Displacement Map' not in node_cache:
disp_texture_node = nodes.new('ShaderNodeTexImage')
disp_texture_node.location = -700, 200
disp_texture_node.image = disp_texture
disp_texture_node.label = 'Displacement Map'
if disp_texture:
disp_texture_node.image.colorspace_settings.name = utils.prefs().import_displace_colorspace
mat.node_tree.links.new(disp_node.inputs[0], disp_texture_node.outputs[0])
def materail_from_polypaint(mat):
""" Create a vertex color node and connect it to the shader node.
"""
nodes, output_node, shader_node = create_base_nodes(mat)
vcol_node = None
vcol_node = [nodes.get(node.name) for node in nodes if node.bl_idname == 'ShaderNodeVertexColor' and utils.prefs().import_polypaint_name in node.layer_name]
if not vcol_node:
vcol_node = nodes.new('ShaderNodeVertexColor')
vcol_node.location = -300, 200
vcol_node.layer_name = utils.prefs().import_polypaint_name
mat.node_tree.links.new(shader_node.inputs[0], vcol_node.outputs[0])