3 def get_linked_node_and_socket(node_tree, socket):
4 for l in node_tree.links:
5 if socket==l.to_socket:
6 return (l.from_node, l.from_socket)
7 elif socket==l.from_socket:
8 return (l.to_node, l.to_socket)
11 def check_group(node_tree, group, func):
12 output = group.node_tree.nodes["Group Output"]
13 from_node, _ = get_linked_node_and_socket(group.node_tree, output.inputs[0])
15 from_node, _ = func(group.node_tree, from_node)
16 if from_node and from_node.type=='GROUP_INPUT':
17 return get_linked_node_and_socket(node_tree, group.inputs[0])
20 def check_invert_green(node_tree, node):
21 if node.type=='GROUP':
22 return check_group(node_tree, node, check_invert_green)
23 elif node.type!='COMBRGB':
26 green, g_sock = get_linked_node_and_socket(node_tree, node.inputs["G"])
27 if not green or green.type!='MATH' or green.operation!='SUBTRACT':
29 green, g_sock = get_linked_node_and_socket(node_tree, green.inputs[1])
31 red, r_sock = get_linked_node_and_socket(node_tree, node.inputs["R"])
32 blue, b_sock = get_linked_node_and_socket(node_tree, node.inputs["B"])
33 if not red or red.type!='SEPRGB' or blue!=red or green!=red:
36 return get_linked_node_and_socket(node_tree, red.inputs["Image"])
38 class MaterialProperty:
39 def __init__(self, keyword, tex_keyword, value):
40 self.keyword = keyword
41 self.tex_keyword = tex_keyword
45 self.invert_green = False
47 def set_from_input(self, node_tree, input_socket, alpha_socket=None):
49 if type(self.value)==tuple:
51 self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
53 self.value = input_socket.default_value[:len(self.value)]
55 self.value = input_socket.default_value
58 from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
62 if from_node.type=='NORMAL_MAP':
63 from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
64 invert, _ = check_invert_green(node_tree, from_node)
67 self.invert_green = True
69 elif from_node.type=='RGBTOBW':
70 from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
73 alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
74 if alpha_from and alpha_from!=from_node:
75 raise Exception("Separate textures for color and alpha are not supported")
77 if from_node.type=='TEX_IMAGE':
78 self.texture = from_node
80 self.tex_usage = usage
82 self.tex_usage = 'RGBA'
83 elif type(self.value)==tuple:
84 self.tex_usage = 'RGB'
86 self.tex_usage = 'GRAY'
88 raise Exception("Unsupported property input node type "+from_node.type)
91 def __init__(self, material):
92 self.name = material.name
96 self.render_mode = material.render_mode
97 self.technique = material.technique
98 self.shader = material.shader
100 if self.render_mode=='EXTERNAL' and not self.technique:
101 raise Exception("Missing technique with external rendering mode")
102 elif self.render_mode=='CUSTOM' and not self.shader:
103 raise Exception("Missing shader with custom rendering mode")
106 for n in material.node_tree.nodes:
107 if n.type=='OUTPUT_MATERIAL':
112 raise Exception("No material output node found")
114 surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
116 if self.render_mode=='BUILTIN':
117 raise Exception("Empty material can't use builtin rendering mode")
119 elif surface_node.type=='BSDF_PRINCIPLED':
122 base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
123 metalness = self.create_property("metalness", 0.0)
124 roughness = self.create_property("roughness", 0.5)
125 normal = self.create_property("normal_map")
126 emission = self.create_property("emission", (0.0, 0.0, 0.0))
128 base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
129 metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
130 roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
131 normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
132 emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
133 elif surface_node.type=='EMISSION':
136 color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
138 color.set_from_input(material.node_tree, surface_node.inputs["Color"])
140 raise Exception("Unsupported surface node type "+surface_node.type)
142 sampler_settings = None
143 for p in self.properties:
145 settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
146 if sampler_settings is None:
147 sampler_settings = settings
148 elif settings!=sampler_settings:
149 raise Exception("Conflicting sampler settings in material textures")
151 def create_property(self, *args):
154 prop = MaterialProperty(None, args[0], None)
156 prop = MaterialProperty(args[0], args[0]+"_map", args[1])
158 prop = MaterialProperty(*args)
159 self.properties.append(prop)
164 def __init__(self, materials):
165 self.render_mode = materials[0].render_mode
166 if self.render_mode=='EXTERNAL':
167 raise Exception("Material atlas with external render mode does not make sense")
169 self.shader = materials[0].shader
171 self.name = "material_atlas_"+os.path.splitext(self.shader)[0]
173 self.name = "material_atlas"
174 self.materials = materials
175 self.material_names = [m.name for m in self.materials]
176 for m in self.materials:
177 if m.render_mode!=self.render_mode:
178 raise Exception("Conflicting render modes in MaterialAtlas constructor")
179 if self.render_mode=='CUSTOM' and m.shader!=self.shader:
180 raise Exception("Conflicting shaders in MaterialAtlas constructor")
182 count = len(self.materials)
184 while size*size*2<count:
187 self.size = (size, size)
189 self.size = (size*2, size)
191 from .util import get_colormap
193 cm = get_colormap(True)
194 self.base_color_data = ""
195 for m in map(Material, self.materials):
196 if any(p.texture for p in m.properties):
197 raise Exception("Texturing is incompatible with material atlas")
198 base_color = [int(cm(c)*255) for c in m.base_color.value]
199 self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color)
200 self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
202 def get_material_uv(self, material):
203 index = self.material_names.index(material.name)
204 x = index%self.size[0]
205 y = index//self.size[0]
206 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
208 def create_material_atlas(context, material):
209 if not material.material_atlas:
210 raise Exception("Material is not part of a material atlas")
212 shader = material.shader
214 for m in context.blend_data.materials:
215 if m.material_atlas and m.shader==shader:
218 mat_map = MaterialAtlas(materials)