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_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 draw_line(start=[0, 0, 0], end=[1, 1, 1], width=0.05, centroid=True, name='line', color=[1, 1, 1], layer=None): mp = centroid_points([start, end]) if centroid else [0, 0, 0] curve = bpy.data.curves.new(name, type='CURVE') curve.dimensions = '3D' object = bpy.data.objects.new(name, curve) object.location = mp spline = curve.splines.new('NURBS') spline.points.add(2) spline.points[0].co = list(subtract_vectors(start, mp)) + [1] spline.points[1].co = list(subtract_vectors(end, mp)) + [1] spline.order_u = 1 object.data.fill_mode = 'FULL' object.data.bevel_depth = width object.data.bevel_resolution = 0 object.data.resolution_u = 20 object.data.materials.append(create_material(color=color)) bpy.context.collection.objects.link(object) if layer: set_objects_layer(objects=[object], layer=layer) return object
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_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_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_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 _link_objects(objects, copy=None, layer=None): for object in objects: bpy.context.collection.objects.link(object) if copy: delete_object(object=copy) if layer: set_objects_layer(objects=objects, layer=layer) return objects
def xdraw_pointcloud(points, layer=None): objects = [0] * len(points) for c, data in enumerate(points): object = xdraw_mesh(name=data.get('name', 'pt'), vertices=[[0, 0, 0]]) object.location = data['pos'] objects[c] = object if layer: set_objects_layer(objects=objects, layer=layer) return objects
def draw_text(radius=1, pos=[0, 0, 0], text='text', layer=None, color=[1, 1, 1]): bpy.ops.object.text_add(view_align=False) object = bpy.context.object object.scale *= radius object.location = pos object.data.body = text object.data.materials.append(create_material(color=color)) if layer: set_objects_layer(objects=[object], layer=layer) return object
def draw_cylinder(start, end, radius=1, color=[1, 1, 1], layer=None, div=10, name='cylinder'): bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=1, vertices=div, location=[0, 0, 0]) L = distance_point_point(start, end) pos = centroid_points([start, end]) object = bpy.context.object object.name = name object.rotation_euler[1] = acos((end[2] - start[2]) / L) object.rotation_euler[2] = atan2(end[1] - start[1], end[0] - start[0]) object.location = pos object.scale = ((radius, radius, L)) object.data.materials.append(create_material(color=color)) if layer: set_objects_layer(objects=[object], layer=layer) return object
def draw_text(radius=1, pos=[0, 0, 0], text='text', layer=None, color=[1, 1, 1]): bpy.ops.object.text_add() object = bpy.context.object object.scale *= radius object.location = pos object.data.body = text object.color = color + [1] if layer: set_objects_layer(objects=[object], layer=layer) return object
def xdraw_mesh(vertices, edges=None, faces=None, name='mesh', color=[1, 1, 1], centroid=True, layer=None, **kwargs): edges = [] if not edges else edges faces = [] if not faces else faces mp = centroid_points(vertices) if centroid else [0, 0, 0] vertices = [subtract_vectors(vertex, mp) for vertex in vertices] mesh = bpy.data.meshes.new(name) mesh.from_pydata(vertices, edges, faces) mesh.update(calc_edges=True) object = bpy.data.objects.new(name, mesh) object.show_wire = True object.data.materials.append(create_material(color=color)) object.location = mp bpy.context.collection.objects.link(object) if layer: set_objects_layer(objects=[object], layer=layer) return object
def draw_cuboid(Lx=1, Ly=1, Lz=1, location=[0, 0, 0], layer=0, wire=True): """ Draw a cuboid. Parameters: Lx (float): Length in x. Ly (float): Length in y. Lz (float): Length in z. location (list): Centroid location [x, y, z]. layer (int): Layer number. colour (str): Material colour. wire (bool): Show wires for faces. Returns: obj: Created cube object. """ bpy.ops.mesh.primitive_cube_add(radius=1, location=location) cube = bpy.context.object cube.dimensions = [Lx, Ly, Lz] cube.show_wire = wire set_objects_layer([cube], layer) cube.select = False return cube
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 link_objects(objects, layer=None): for obj in objects: bpy.context.collection.objects.link(obj) if layer: set_objects_layer(objects, layer)