3 def compute_render_method_hash(material):
5 for m in material.render_methods:
8 descr += "{}={}".format(m.tag, m.shader)
11 def check_group(node_tree, group, func):
12 from .util import get_linked_node_and_socket
14 output = group.node_tree.nodes["Group Output"]
15 from_node, _ = get_linked_node_and_socket(group.node_tree, output.inputs[0])
17 from_node, _ = func(group.node_tree, from_node)
18 if from_node and from_node.type=='GROUP_INPUT':
19 return get_linked_node_and_socket(node_tree, group.inputs[0])
22 def check_invert_green(node_tree, node):
23 if node.type=='GROUP':
24 return check_group(node_tree, node, check_invert_green)
25 elif node.type!='COMBRGB':
28 from .util import get_linked_node_and_socket
30 green, g_sock = get_linked_node_and_socket(node_tree, node.inputs["G"])
31 if not green or green.type!='MATH' or green.operation!='SUBTRACT':
33 green, g_sock = get_linked_node_and_socket(node_tree, green.inputs[1])
35 red, r_sock = get_linked_node_and_socket(node_tree, node.inputs["R"])
36 blue, b_sock = get_linked_node_and_socket(node_tree, node.inputs["B"])
37 if not red or red.type!='SEPRGB' or blue!=red or green!=red:
40 return get_linked_node_and_socket(node_tree, red.inputs["Image"])
42 class MaterialProperty:
43 def __init__(self, keyword, tex_keyword, value):
44 self.keyword = keyword
45 self.tex_keyword = tex_keyword
49 self.invert_green = False
51 def set_from_input(self, node_tree, input_socket, alpha_socket=None):
53 if type(self.value)==tuple:
55 self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
57 self.value = input_socket.default_value[:len(self.value)]
59 self.value = input_socket.default_value
62 from .util import get_linked_node_and_socket
64 from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
68 if from_node.type=='NORMAL_MAP':
69 from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
70 invert, _ = check_invert_green(node_tree, from_node)
73 self.invert_green = True
75 elif from_node.type=='RGBTOBW':
76 from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
79 alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
80 if alpha_from and alpha_from!=from_node:
81 raise Exception("Separate textures for color and alpha are not supported")
83 if from_node.type=='TEX_IMAGE':
84 self.texture = from_node
86 self.tex_usage = usage
88 self.tex_usage = 'RGBA'
89 elif type(self.value)==tuple:
90 self.tex_usage = 'RGB'
92 self.tex_usage = 'GRAY'
94 raise Exception("Unsupported property input node type "+from_node.type)
97 def __init__(self, material):
98 self.name = material.name
102 self.render_mode = material.render_mode
103 self.technique = material.technique
104 self.render_methods = material.render_methods[:]
105 self.receive_shadows = material.receive_shadows
106 self.cast_shadows = (material.shadow_method!='NONE')
107 self.image_based_lighting = material.image_based_lighting
109 if self.render_mode=='EXTERNAL' and not self.technique:
110 raise Exception("Invalid configuration on material {}: No technique for external rendering".format(self.name))
111 elif self.render_mode=='CUSTOM' and not self.render_methods:
112 raise Exception("Invalid configuration on material {}: No render methods for custom rendering".format(self.name))
114 out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None)
116 raise Exception("No output node found on material {}".format(self.name))
118 from .util import get_linked_node_and_socket
120 surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
122 if self.render_mode=='BUILTIN':
123 raise Exception("Invalid configuration on material {}: Empty material with builtin rendering".format(self.name))
125 elif surface_node.type=='BSDF_PRINCIPLED':
128 base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
129 metalness = self.create_property("metalness", 0.0)
130 roughness = self.create_property("roughness", 0.5)
131 normal = self.create_property("normal_map")
132 emission = self.create_property("emission", (0.0, 0.0, 0.0))
134 base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
135 metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
136 roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
137 normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
138 emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
139 elif surface_node.type=='EMISSION':
142 color = self.create_property("color", "texture", (1.0, 1.0, 1.0, 1.0))
144 color.set_from_input(material.node_tree, surface_node.inputs["Color"])
146 raise Exception("Unsupported surface node type {} on material {}".format(surface_node.type, self.name))
148 sampler_settings = None
149 for p in self.properties:
151 settings = (p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
152 if sampler_settings is None:
153 sampler_settings = settings
154 elif settings!=sampler_settings:
155 raise Exception("Material {} has conflicting texture sampler settings".format(self.name))
157 def create_property(self, *args):
160 prop = MaterialProperty(None, args[0], None)
162 prop = MaterialProperty(args[0], args[0]+"_map", args[1])
164 prop = MaterialProperty(*args)
165 self.properties.append(prop)
170 def __init__(self, materials):
171 self.render_mode = materials[0].render_mode
172 if self.render_mode=='EXTERNAL':
173 raise Exception("Material atlas with external render mode does not make sense")
175 if self.render_mode=='CUSTOM':
176 self.render_methods = materials[0].render_methods
178 self.render_methods = None
179 if self.render_methods:
180 self.name = "material_atlas_"+os.path.splitext(self.render_methods[0].shader)[0]
182 self.name = "material_atlas"
183 self.receive_shadows = materials[0].receive_shadows
184 self.cast_shadows = (materials[0].shadow_method!='NONE')
185 self.materials = materials
186 self.material_names = [m.name for m in self.materials]
188 method_hash = compute_render_method_hash(self)
189 for m in self.materials:
190 if m.render_mode!=self.render_mode:
191 raise Exception("Conflicting render modes in MaterialAtlas constructor")
192 if self.render_mode=='CUSTOM' and compute_render_method_hash(m)!=method_hash:
193 raise Exception("Conflicting shaders in MaterialAtlas constructor")
194 if m.receive_shadows!=self.receive_shadows or m.shadow_method!=materials[0].shadow_method:
195 raise Exception("Conflicting shadow settings in MaterialAtlas constructor")
197 count = len(self.materials)
199 while size*size*2<count:
202 self.size = (size, size)
204 self.size = (size*2, size)
206 from .util import get_colormap
208 cm = get_colormap(True)
209 self.base_color_data = ""
210 for m in map(Material, self.materials):
211 if any(p.texture for p in m.properties):
212 raise Exception("Texturing is incompatible with material atlas")
213 base_color = [int(cm(c)*255) for c in m.base_color.value]
214 self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color)
215 self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
217 def get_material_uv(self, material):
218 index = self.material_names.index(material.name)
219 x = index%self.size[0]
220 y = index//self.size[0]
221 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
223 def create_material_atlas(context, material):
224 if not material.material_atlas:
225 raise Exception("Material is not part of a material atlas")
227 method_hash = compute_render_method_hash(material)
229 for m in context.blend_data.materials:
230 if m.material_atlas and compute_render_method_hash(m)==method_hash:
233 return MaterialAtlas(materials)