]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/material.py
Support exporting "empty" materials with only a technique or shader
[libs/gl.git] / blender / io_mspgl / material.py
1 import os
2
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)
9         return (None, None)
10
11 class MaterialProperty:
12         def __init__(self, value):
13                 self.value = value
14                 self.texture = None
15                 self.tex_usage = None
16
17         def set_from_input(self, node_tree, input_socket, alpha_socket=None):
18                 if type(self.value)==tuple:
19                         if alpha_socket:
20                                 self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
21                         else:
22                                 self.value = input_socket.default_value[:len(self.value)]
23                 elif self.value is not None:
24                         self.value = input_socket.default_value
25
26                 from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
27                 alpha_from = None
28                 if from_node:
29                         if from_node.type=='NORMAL_MAP':
30                                 from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"])
31
32                         if alpha_socket:
33                                 alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket)
34                                 if alpha_from and alpha_from!=from_node:
35                                         raise Exception("Separate textures for color and alpha are not supported")
36
37                         if from_node.type=='TEX_IMAGE':
38                                 self.texture = from_node
39                                 if alpha_from:
40                                         self.tex_usage = 'RGBA'
41                                 elif type(self.value)==tuple:
42                                         self.tex_usage = 'RGB'
43                                 else:
44                                         self.tex_usage = 'GRAY'
45                         else:
46                                 raise Exception("Unsupported property input node type "+from_node.type)
47
48 class Material:
49         def __init__(self, material):
50                 self.name = material.name
51                 self.type = None
52                 self.properties = {}
53
54                 self.render_mode = material.render_mode
55                 self.technique = material.technique
56                 self.shader = material.shader
57
58                 if self.render_mode=='EXTERNAL' and not self.technique:
59                         raise Exception("Missing technique with external rendering mode")
60                 elif self.render_mode=='CUSTOM' and not self.shader:
61                         raise Exception("Missing shader with custom rendering mode")
62
63                 out_node = None
64                 for n in material.node_tree.nodes:
65                         if n.type=='OUTPUT_MATERIAL':
66                                 out_node = n
67                                 break
68
69                 if not out_node:
70                         raise Exception("No material output node found")
71
72                 surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
73                 if not surface_node:
74                         if self.render_mode=='BUILTIN':
75                                 raise Exception("Empty material can't use builtin rendering mode")
76                         return
77                 elif surface_node.type!='BSDF_PRINCIPLED':
78                         raise Exception("Unsupported surface node type "+surface_node.type)
79
80                 self.type = "pbr"
81
82                 base_color = self.properties["base_color"] = MaterialProperty((0.8, 0.8, 0.8, 1.0))
83                 metalness = self.properties["metalness"] = MaterialProperty(0.0)
84                 roughness = self.properties["roughness"] = MaterialProperty(0.5)
85                 normal = self.properties["normal"] = MaterialProperty(None)
86                 emission = self.properties["emission"] = MaterialProperty((0.0, 0.0, 0.0))
87
88                 base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
89                 metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
90                 roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
91                 normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
92                 emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
93
94                 sampler_settings = None
95                 for p in self.properties.values():
96                         if p.texture:
97                                 settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
98                                 if sampler_settings is None:
99                                         sampler_settings = settings
100                                 elif settings!=sampler_settings:
101                                         raise Exception("Conflicting sampler settings in material textures")
102
103
104 class MaterialMap:
105         def __init__(self, materials):
106                 self.render_mode = materials[0].render_mode
107                 if self.render_mode=='EXTERNAL':
108                         raise Exception("Material map with external render mode does not make sense")
109
110                 self.shader = materials[0].shader
111                 if self.shader:
112                         self.name = "material_map_"+os.path.splitext(self.shader)[0]
113                 else:
114                         self.name = "material_map"
115                 self.materials = materials
116                 self.material_names = [m.name for m in self.materials]
117                 for m in self.materials:
118                         if m.render_mode!=self.render_mode:
119                                 raise Exception("Conflicting render modes in MaterialMap constructor")
120                         if self.render_mode=='CUSTOM' and m.shader!=self.shader:
121                                 raise Exception("Conflicting shaders in MaterialMap constructor")
122
123                 count = len(self.materials)
124                 size = 1
125                 while size*size*2<count:
126                         size *= 2
127                 if size*size>=count:
128                         self.size = (size, size)
129                 else:
130                         self.size = (size*2, size)
131
132                 from .util import get_colormap
133
134                 cm = get_colormap(True)
135                 self.base_color_data = ""
136                 for m in map(Material, self.materials):
137                         if any(p.texture for p in m.properties):
138                                 raise Exception("Texturing is incompatible with material map")
139                         base_color = [int(cm(c)*255) for c in m.base_color.value]
140                         self.base_color_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*base_color)
141                 self.base_color_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
142
143         def get_material_uv(self, material):
144                 index = self.material_names.index(material.name)
145                 x = index%self.size[0]
146                 y = index//self.size[0]
147                 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
148
149 def create_material_map(context, material):
150         if not material.material_map:
151                 raise Exception("Material is not part of a material map")
152
153         shader = material.shader
154         materials = []
155         for m in context.blend_data.materials:
156                 if m.material_map and m.shader==shader:
157                         materials.append(m)
158
159         mat_map = MaterialMap(materials)
160
161         return mat_map