|
| 1 | +import bpy |
| 2 | +import numpy as np |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +file_dir = os.path.dirname(__file__) |
| 7 | +sys.path.append(file_dir) |
| 8 | +file_dir = file_dir.replace('\\', '/').replace('\r', '/r').replace('\n', '/n').\ |
| 9 | + replace('\t', '/t') |
| 10 | + |
| 11 | + |
| 12 | +class Material: |
| 13 | + """ |
| 14 | + Class that represents a material object in Blender. |
| 15 | + """ |
| 16 | + def __init__(self, name, filename='material'): |
| 17 | + self.name = name.lower().capitalize() # name of the material and its texture folder |
| 18 | + self.filename = filename |
| 19 | + self._path = file_dir + '/Textures/{}.blend'.format(self.filename) |
| 20 | + self._add = '\\Material\\' |
| 21 | + self.value = self._load() # loads material into the scene |
| 22 | + self._update_nodes() # loads the textures to the material |
| 23 | + |
| 24 | + def _load(self): |
| 25 | + try: |
| 26 | + return bpy.data.materials[self.name].copy() |
| 27 | + except KeyError: |
| 28 | + try: |
| 29 | + # print(file_dir) |
| 30 | + bpy.ops.wm.append( |
| 31 | + filepath=self._path + self._add + self.filename, |
| 32 | + filename='{}'.format(self.filename), |
| 33 | + directory=self._path + self._add) |
| 34 | + bpy.data.materials[-1].name = self.name |
| 35 | + return bpy.data.materials[self.name] |
| 36 | + except Exception as e: |
| 37 | + print(repr(e)) |
| 38 | + print('Could not import {} from {}'.format(self.name, self._path)) |
| 39 | + raise KeyboardInterrupt() |
| 40 | + |
| 41 | + def _load_maps(self, map_type): |
| 42 | + """ |
| 43 | + Function that uploads texture map into the Material tree nodes. Textures |
| 44 | + are taken from folder Texture/Material where Material corresponds to the |
| 45 | + name of the material. |
| 46 | + :param map_type: type of the map to upload, str, one of 'Diffuse', 'Normal', |
| 47 | + 'Roughness', 'Displacement' |
| 48 | + :return: texture map, bpy image object |
| 49 | + """ |
| 50 | + assert map_type in ['Diffuse', 'Normal', 'Roughness', 'Displacement'], \ |
| 51 | + "Unknown map type, expected one of: 'Diffuse', 'Normal'," \ |
| 52 | + "'Roughness', 'Displacement'" |
| 53 | + try: |
| 54 | + bpy.ops.image.open(filepath=file_dir + '/Textures/{}/{}.png'. |
| 55 | + format(self.name, map_type.capitalize())) |
| 56 | + return bpy.data.images[-2] |
| 57 | + except Exception as e: |
| 58 | + print('Failed to load {} texture of {}'.format(map_type, self.name)) |
| 59 | + print(repr(e)) |
| 60 | + |
| 61 | + def _update_nodes(self): |
| 62 | + """ |
| 63 | + Function that updates the nodes of the material tree with the material |
| 64 | + textures. |
| 65 | + :return: |
| 66 | + """ |
| 67 | + self.value.node_tree.nodes['Diffuse_texture'].image = self._load_maps('Diffuse') |
| 68 | + self.value.node_tree.nodes['Normal_texture'].image = self._load_maps('Normal') |
| 69 | + self.value.node_tree.nodes['Displacement_texture'].image = self._load_maps('Displacement') |
| 70 | + |
| 71 | + |
| 72 | +class MaskMaterial(Material): |
| 73 | + def __init__(self, name='mask', filename='mask', color=(1.0, 1.0, 1.0)): |
| 74 | + self.filename = filename |
| 75 | + self.color = color |
| 76 | + Material.__init__(self, name, filename=self.filename) |
| 77 | + |
| 78 | + def _update_nodes(self): |
| 79 | + self.value.node_tree.nodes['RGB'].outputs[0].default_value[0] = self.color[0] |
| 80 | + self.value.node_tree.nodes['RGB'].outputs[0].default_value[1] = self.color[1] |
| 81 | + self.value.node_tree.nodes['RGB'].outputs[0].default_value[2] = self.color[2] |
| 82 | + |
| 83 | + |
| 84 | +class MaterialFactory: |
| 85 | + """ |
| 86 | + Class that produces materials based on the given name. |
| 87 | + """ |
| 88 | + def __init__(self): |
| 89 | + self.materials = os.listdir('{}/Textures'.format(file_dir)) |
| 90 | + self.materials = [x for x in self.materials if os.path.isdir('{}/Textures/{}'.format(file_dir, x))] |
| 91 | + |
| 92 | + def produce(self, name=None, color=None): |
| 93 | + if name: |
| 94 | + if name == 'mask': |
| 95 | + if not color: |
| 96 | + color = [1.0, 0.0, 0.0] |
| 97 | + return MaskMaterial(name, color) |
| 98 | + else: |
| 99 | + name = name.lower().capitalize() |
| 100 | + assert name in self.materials, "Unknown material {}, not in Textures folder".format(name) |
| 101 | + return Material(name) |
| 102 | + else: |
| 103 | + return Material(np.random.choice(self.materials)) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + |
| 108 | + from generator import * |
| 109 | + |
| 110 | + material = MaterialFactory().produce('CONCrete') |
| 111 | + f = CollectionFactory() |
| 112 | + collection = f.produce(number=1) |
| 113 | + building = ComposedBuilding(collection.collection) |
| 114 | + building.make() |
| 115 | + building.volumes[0].apply(material) |
0 commit comments