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 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 object.data.fill_mode = 'FULL' object.data.bevel_depth = line.get('width', 0.05) object.data.bevel_resolution = 0 object.data.materials.append(bpy.data.materials[line.get( 'color', 'white')]) set_objects_layer([object], line.get('layer', 0)) objects.append(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 draw_bmesh(name, vertices=[], edges=[], faces=[], layer=0, color='grey', wire=True): """ Draws a Blender mesh in the given layer. Parameters: name (str): Blender mesh name. vertices (list): Vertices [x, y, z]. edges (list): Edges [vert1, vert2]. faces (list): Faces [vert1, vert2, ...]. layer (int): Layer number. color (str): Material color. wire (bool): Show wires for faces. Returns: obj: Created Blender mesh object. """ mesh = bpy.data.meshes.new(name) mesh.from_pydata(vertices, edges, faces) mesh.update(calc_edges=True) bmesh = bpy.data.objects.new(name, mesh) bpy.context.scene.objects.link(bmesh) bmesh.show_wire = wire bmesh.data.materials.append(bpy.data.materials[color]) set_objects_layer([bmesh], layer) bmesh.select = False return bmesh
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.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_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