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 class MaterialProperty:
12 def __init__(self, keyword, tex_keyword, value):
13 self.keyword = keyword
14 self.tex_keyword = tex_keyword
19 def set_from_input(self, node_tree, input_socket, alpha_socket=None):
21 if type(self.value)==tuple:
23 self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
25 self.value = input_socket.default_value[:len(self.value)]
27 self.value = input_socket.default_value
30 from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
33 if from_node.type=='NORMAL_MAP':
34 from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
37 alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
38 if alpha_from and alpha_from!=from_node:
39 raise Exception("Separate textures for color and alpha are not supported")
41 if from_node.type=='TEX_IMAGE':
42 self.texture = from_node
44 self.tex_usage = 'RGBA'
45 elif type(self.value)==tuple:
46 self.tex_usage = 'RGB'
48 self.tex_usage = 'GRAY'
50 raise Exception("Unsupported property input node type "+from_node.type)
53 def __init__(self, material):
54 self.name = material.name
58 self.render_mode = material.render_mode
59 self.technique = material.technique
60 self.shader = material.shader
62 if self.render_mode=='EXTERNAL' and not self.technique:
63 raise Exception("Missing technique with external rendering mode")
64 elif self.render_mode=='CUSTOM' and not self.shader:
65 raise Exception("Missing shader with custom rendering mode")
68 for n in material.node_tree.nodes:
69 if n.type=='OUTPUT_MATERIAL':
74 raise Exception("No material output node found")
76 surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
78 if self.render_mode=='BUILTIN':
79 raise Exception("Empty material can't use builtin rendering mode")
81 elif surface_node.type=='BSDF_PRINCIPLED':
84 base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0))
85 metalness = self.create_property("metalness", 0.0)
86 roughness = self.create_property("roughness", 0.5)
87 normal = self.create_property("normal")
88 emission = self.create_property("emission", (0.0, 0.0, 0.0))
90 base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
91 metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
92 roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
93 normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
94 emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
96 raise Exception("Unsupported surface node type "+surface_node.type)
98 sampler_settings = None
99 for p in self.properties:
101 settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
102 if sampler_settings is None:
103 sampler_settings = settings
104 elif settings!=sampler_settings:
105 raise Exception("Conflicting sampler settings in material textures")
107 def create_property(self, *args):
110 prop = MaterialProperty(None, args[0], None)
112 prop = MaterialProperty(args[0], args[0]+"_map", args[1])
114 prop = MaterialProperty(*args)
115 self.properties.append(prop)
120 def __init__(self, materials):
121 self.render_mode = materials[0].render_mode
122 if self.render_mode=='EXTERNAL':
123 raise Exception("Material map with external render mode does not make sense")
125 self.shader = materials[0].shader
127 self.name = "material_map_"+os.path.splitext(self.shader)[0]
129 self.name = "material_map"
130 self.materials = materials
131 self.material_names = [m.name for m in self.materials]
132 for m in self.materials:
133 if m.render_mode!=self.render_mode:
134 raise Exception("Conflicting render modes in MaterialMap constructor")
135 if self.render_mode=='CUSTOM' and m.shader!=self.shader:
136 raise Exception("Conflicting shaders in MaterialMap constructor")
138 count = len(self.materials)
140 while size*size*2<count:
143 self.size = (size, size)
145 self.size = (size*2, size)
147 from .util import get_colormap
149 cm = get_colormap(True)
150 self.base_color_data = ""
151 for m in map(Material, self.materials):
152 if any(p.texture for p in m.properties):
153 raise Exception("Texturing is incompatible with material map")
154 base_color = [int(cm(c)*255) for c in m.base_color.value]
155 self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color)
156 self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
158 def get_material_uv(self, material):
159 index = self.material_names.index(material.name)
160 x = index%self.size[0]
161 y = index//self.size[0]
162 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
164 def create_material_map(context, material):
165 if not material.material_map:
166 raise Exception("Material is not part of a material map")
168 shader = material.shader
170 for m in context.blend_data.materials:
171 if m.material_map and m.shader==shader:
174 mat_map = MaterialMap(materials)