def xdraw_lines(lines): """ Draw a set of lines. Parameters: lines (list): {'color':, 'start':, 'end':, 'name':, 'width':, 'layer': }. Returns: list: Created line objects. """ objects = [] for line in lines: curve = bpy.data.curves.new(line.get('name', 'line'), type='CURVE') curve.dimensions = '3D' object = bpy.data.objects.new(line.get('name', 'line'), curve) object.location = [0, 0, 0] line_ = curve.splines.new('NURBS') line_.points.add(2) line_.points[0].co = list(line.get('start')) + [1] line_.points[1].co = list(line.get('end')) + [1] line_.order_u = 1 material = create_material(color=line.get('color', [1, 1, 1])) object.data.fill_mode = 'FULL' object.data.bevel_depth = line.get('width', 0.05) object.data.bevel_resolution = 0 object.data.materials.append(material) set_object_layer(object=object, layer=line.get('layer', 0)) objects.append(object) deselect_all_objects() return _link_objects(objects)
def xdraw_texts(texts): """ Draw a set of text objects. Parameters: texts (list): {'radius':, 'pos':, 'color':, 'name':, 'text':, 'layer':}. Returns: list: Created text objects. """ objects = [] bpy.ops.object.text_add(radius=1, view_align=True, location=[0, 0, 0]) object = bpy.context.object for text in texts: copy = object.copy() copy.name = text.get('name', 'text') copy.data.body = text.get('text', 'text') copy.location = Vector(text.get('pos', [0, 0, 0])) copy.scale *= text.get('radius', 1) copy.data = copy.data.copy() copy.data.materials.append(bpy.data.materials[text.get('color', 'white')]) set_objects_layer([copy], text.get('layer', 0)) objects.append(copy) delete_objects([object]) for object in objects: bpy.context.scene.objects.link(object) deselect_all_objects() return objects
def _link_objects(objects): for object in objects: mask = [i for i in object.layers] bpy.context.scene.objects.link(object) object.layers[:] = mask deselect_all_objects() return objects
def xdraw_pipes(pipes, div=8): """ Draw a set of pipes. Parameters: pipes (list): {'radius':, 'start':, 'end':, 'color':, 'name':, 'layer':}. div (int): Divisions around cross-section. Returns: list: Created pipe objects. """ objects = [] bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=1, vertices=div, location=[0, 0, 0]) object = bpy.context.object for pipe in pipes: radius = pipe.get('radius', 1) start = pipe.get('start', [0, 0, 0]) end = pipe.get('end', [0, 0, 1]) L = distance_point_point(start, end) pos = centroid_points([start, end]) copy = object.copy() copy.name = pipe.get('name', 'pipe') copy.rotation_euler[1] = acos((end[2] - start[2]) / L) copy.rotation_euler[2] = atan2(end[1] - start[1], end[0] - start[0]) copy.location = Vector(pos) copy.data = copy.data.copy() copy.scale = ((radius, radius, L)) copy.show_wire = True copy.data.materials.append(bpy.data.materials[pipe.get('color', 'white')]) set_objects_layer([copy], pipe.get('layer', 0)) objects.append(copy) delete_objects([object]) for object in objects: bpy.context.scene.objects.link(object) deselect_all_objects() return objects
def xdraw_spheres(spheres, div=20): """ Draw a set of spheres. Parameters: spheres (dic): {'radius':, 'pos':, 'color':, 'name':, 'layer':}. div (int): Divisions for spheres. Returns: list: Created sphere objects. """ objects = [] bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=[0, 0, 0], ring_count=div, segments=div) object = bpy.context.object for sphere in spheres: copy = object.copy() copy.name = sphere.get('name', 'sphere') copy.location = Vector(sphere.get('pos', [0, 0, 0])) copy.scale *= sphere.get('radius', 1) copy.data = copy.data.copy() copy.data.materials.append(bpy.data.materials[sphere.get('color', 'white')]) set_objects_layer([copy], sphere.get('layer', 0)) objects.append(copy) delete_objects([object]) for object in objects: bpy.context.scene.objects.link(object) deselect_all_objects() return objects
def xdraw_cubes(cubes): """ Draw a set of cubes. Parameters: cubes (list): {'radius':, 'pos':, 'color':, 'name':, 'layer':}. Returns: list: Created cube objects. """ objects = [] bpy.ops.mesh.primitive_cube_add(radius=1, location=[0, 0, 0]) object = bpy.context.object for cube in cubes: copy = object.copy() copy.name = cube.get('name', 'cube') copy.location = Vector(cube.get('pos', [0, 0, 0])) copy.scale *= cube.get('radius', 1) copy.data = copy.data.copy() copy.data.materials.append(bpy.data.materials[cube.get('color', 'white')]) set_objects_layer([copy], cube.get('layer', 0)) objects.append(copy) delete_objects([object]) for object in objects: bpy.context.scene.objects.link(object) deselect_all_objects() return objects
def xdraw_labels(labels): """ Draw a set of text labels. Parameters: labels (dic): {'pos':, 'name':, 'layer':}. Returns: list: Created labels objects (bmeshes). """ objects = xdraw_pointcloud(labels) set_objects_show_name(objects, show=True) deselect_all_objects() return objects
def xdraw_pointcloud(points): """ Draw a set of points using Blender mesh vertices. Parameters: points (dic): {'pos':, 'name':, 'layer':}. Returns: list: Created point objects (bmeshes). """ objects = [] object = draw_bmesh('pt', vertices=[[0, 0, 0]]) for point in points: copy = object.copy() copy.name = point.get('name', 'point') copy.location = Vector(point.get('pos', [0, 0, 0])) copy.data = copy.data.copy() set_objects_layer([copy], point.get('layer', 0)) objects.append(copy) delete_objects([object]) for object in objects: bpy.context.scene.objects.link(object) deselect_all_objects() return objects
def xdraw_points(points): """ Draw a set of points (empties). Parameters: points (list): {'radius':, 'pos':, 'name':, 'layer':}. Returns: list: Created empty objects. """ objects = [] bpy.ops.object.empty_add(type='SPHERE', radius=1, location=[0, 0, 0]) object = bpy.context.object for point in points: copy = object.copy() copy.name = point.get('name', 'point') copy.location = Vector(point.get('pos', [0, 0, 0])) copy.scale *= point.get('radius', 1) set_objects_layer([copy], point.get('layer', 0)) objects.append(copy) delete_objects([object]) for object in objects: bpy.context.scene.objects.link(object) deselect_all_objects() return objects
def draw_network(network, type='mesh', layer=0, show_vertices=0.01, show_edges=0.005, show_faces=True, vertex_name_attr=[], edge_name_attr=[], face_name_attr=[]): """ Draw a representation of a Network datastructure. Parameters: network (obj): Network datastructure. type (str): Draw as 'mesh' or 'lines'. layer (int): Layer to draw Network on. show_vertices (float): Size of vertex cubes when type is 'lines'. show_edges (float): Size of lines when type is 'lines'. show_faces (bool): Draw Network faces. vertex_name_attr (list): Attributes to show in vertex names. edge_name_attr (list): Attributes to show in edge names. face_name_attr (list): Attributes to show in face names. Returns: obj: Created Blender mesh or None. """ keys = list(network.vertices()) vertices = [network.vertex_coordinates(key) for key in keys] k_i = network.key_index() uv_i = network.uv_index() u_v = list(network.edges()) edges = [(k_i[u], k_i[v]) for u, v in u_v] fkeys = list(network.faces()) faces = [network.face[fkey] for fkey in fkeys] if type == 'mesh': return draw_bmesh('network', vertices=vertices, edges=edges, faces=faces) elif type == 'lines': if show_vertices: cubes_vertices = [] for key in keys: vertex = network.vertex[key] xyz = network.vertex_coordinates(key) color = 'black' if vertex.get('is_fixed', None) else 'white' name_dic = {} for attr in vertex_name_attr: name_dic[attr] = vertex.get(attr, 'None') name = json.dumps(name_dic) cubes_vertices.append({ 'radius': show_vertices, 'pos': xyz, 'color': color, 'name': name }) xdraw_cubes(cubes_vertices) if show_edges: lines_edges = [] for u, v in u_v: sp = network.vertex_coordinates(u) ep = network.vertex_coordinates(v) i = uv_i[(u, v)] color = network.edge[u][v].get('color', 'grey') name_dic = {} for attr in edge_name_attr: name_dic[attr] = network.edge[u][v].get(attr, 'None') name = json.dumps(name_dic) lines_edges.append({ 'name': name, 'start': sp, 'end': ep, 'layer': layer, 'color': color, 'width': show_edges }) xdraw_lines(lines_edges) if show_faces: for fkey in fkeys: facedata = network.facedata[fkey] color = facedata.get('color', 'grey') pts = [vertices[i] for i in network.face[fkey]] name_dic = {} for attr in face_name_attr: name_dic[attr] = facedata.get(attr, 'None') name = json.dumps(name_dic) draw_bmesh(name=name, vertices=pts, faces=[list(range(len(pts)))], layer=layer, color=color) deselect_all_objects()