def __del__(self): """Quand le génotype est suprimmé, on vire aussi son phénotype s'il existe.""" if self.generated is not None: import bpy from StoneEdgeGeneration import bpyutils bpyutils.bpydeselect() for child in bpy.data.objects[self.generated].children: child.select = True bpy.ops.object.delete() bpy.data.objects[self.generated].select = True bpy.ops.object.delete()
def compute_individual(location, data): import bpy import StoneEdgeGeneration.bpyutils as bpyutils """Génère un cristal""" values = json.loads(data) genotype = values['genotype'] print('compute individuals') bpy.context.scene.cursor_location = [0, 0, 0] bpy.ops.object.add(type='EMPTY') bpy.context.object.name = 'Rock' + '%03d' % + GenericGenetic.bobject_unique_id() print("\ncompute " + bpy.context.object.name) parent_obj = bpy.context.object for idx, subrock in enumerate(genotype): bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, size=1,view_align=False,location=(0, 0, 0), enter_editmode=False) bpy.context.object.parent = parent_obj bpy.context.object.name = 'Sub' + parent_obj.name + '-' + str(idx) object_ref = bpy.context.object for cut in subrock['cuts']: plane_co = bpyutils.spherical_to_xyz(cut[0], cut[1],cut[2]) plane_no = list(plane_co) plane_no_magnitude = math.sqrt(sum([x ** 2 for x in plane_no])) plane_no = [x / plane_no_magnitude for x in plane_no] bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.select_all(action='SELECT') bpy.ops.mesh.bisect(plane_co=plane_co,plane_no=plane_no,use_fill=True, clear_inner=False, clear_outer=True, xstart=-100, xend=100,ystart=-100, yend=100) bpy.ops.object.mode_set(mode='OBJECT') object_ref.scale = (subrock['scale'][0], subrock['scale'][1], subrock['scale'][2]) bpy.context.object.rotation_euler[0] = subrock['orientation'][1] bpy.context.object.rotation_euler[2] = subrock['orientation'][0] object_ref.location = ( (subrock['scale'][2] + subrock['offset']) * math.sin(subrock['orientation'][0]) * math.sin(subrock['orientation'][1]), (subrock['scale'][2] + subrock['offset']) * -math.cos(subrock['orientation'][0]) * math.sin(subrock['orientation'][1]), (subrock['scale'][2] + subrock['offset']) * math.cos(subrock['orientation'][1]) ) parent_obj.location = location bpyutils.bpydeselect()
def compute_individual(location, data): import bpy import time from scipy import misc from StoneEdgeGeneration.Terrain.HeightMap import heightmap3 from StoneEdgeGeneration import bpyutils start = time.time() values = json.loads(data) genotype = values['genotype'] voronoifile = values['voronoifile'] voronoiheight = misc.imread(values['voronoiheightfile']) / 100.0 - 0.2 end = time.time() print("loading: ", end - start, "s") bpy.context.scene.cursor_location = [0, 0, 0] bpy.ops.mesh.primitive_plane_add(radius=10) bpy.context.object.name = 'Terrain' + '%03d' % +GenericGenetic.bobject_unique_id( ) print("\ncompute " + bpy.context.object.name + " " + str(genotype['vertcount']) + " vertices") obj = bpy.context.object mesh = obj.data start = time.time() bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') obj.select = True bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.dissolve_limited(angle_limit=0.8) count = int(0.5 * (genotype['vertcount'] + (len(mesh.vertices) - 1)) / len(mesh.vertices)) if count >= 0: bpy.ops.mesh.subdivide(number_cuts=count) bpy.ops.mesh.normals_make_consistent() bpy.ops.uv.unwrap(method='ANGLE_BASED', margin=0.001) bpy.ops.object.mode_set(mode='OBJECT') end = time.time() print("make object : ", end - start, "s") start = time.time() originalVertices = np.empty((3, len(mesh.vertices), 3)) for p in mesh.loops: if (originalVertices[0, p.vertex_index] == mesh.vertices[ p.vertex_index].co).all(): continue originalVertices[0, p.vertex_index] = mesh.vertices[p.vertex_index].co originalVertices[1, p.vertex_index] = mesh.vertices[ p.vertex_index].normal u = mesh.uv_layers.active.data[p.index].uv[0] v = mesh.uv_layers.active.data[p.index].uv[1] originalVertices[2, p.vertex_index, :2] = (np.array( (u, 1 - v)) * voronoiheight.shape) originalVertices[2, p.vertex_index, 2] = voronoiheight[ int(originalVertices[2, p.vertex_index, 1]), int(originalVertices[2, p.vertex_index, 0])] end = time.time() print("get data : ", end - start, "s") start = time.time() map = heightmap3(100, 100, 5, originalVertices[0, :, 0], originalVertices[0, :, 1], originalVertices[0, :, 2], coefMap1=genotype['coef_map_one'], coefMap2=genotype['coef_map_two'], smooth=genotype['height_smooth'], octaves=genotype['height_octaves'], lacunarity=genotype['height_lacunarity'], freq=genotype['height_freq'], freq2=genotype['height_freq'], mean=genotype['height_mean'], scale=genotype['height_scale'], randomtype=genotype['randomtype'], seed=genotype['seed']) for i in range(0, len(mesh.vertices)): vertice = mesh.vertices[i] vertice.co = originalVertices[0, i] +\ originalVertices[1, i] * (genotype['height_size'] * (0.3+0.5*map[i]) * originalVertices[2, i, 2]) end = time.time() print("make height map : ", end - start, "s") bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.vertices_smooth(factor=0.5, repeat=1) if genotype['height_smooth']: bpy.ops.mesh.vertices_smooth(factor=0.5, repeat=2) bpy.ops.object.mode_set(mode='OBJECT') mat = bpy.data.materials.new("map_mat") mat.ambient = 0.4 mesh.materials.append(mat) tex = bpy.data.textures.new("tex", 'IMAGE') slot = mat.texture_slots.add() slot.texture = tex try: img = bpy.data.images.load(voronoifile) except: print("Cannot load image %s" % voronoifile) return tex.image = img bpyutils.bpydeselect()