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. """ bpy.ops.mesh.primitive_uv_sphere_add(ring_count=div, segments=div) object = bpy.context.object objects = [] for sphere in spheres: copy = object.copy() copy.scale *= sphere.get('radius', 1) copy.location = Vector(sphere.get('pos', [0, 0, 0])) copy.name = sphere.get('name', 'sphere') copy.data = copy.data.copy() material = create_material(color=sphere.get('color', [1, 1, 1])) copy.data.materials.append(material) set_object_layer(object=copy, layer=sphere.get('layer', 0)) objects.append(copy) delete_object(object=object) 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. """ bpy.ops.object.text_add(view_align=True) object = bpy.context.object objects = [] for text in texts: copy = object.copy() copy.scale *= text.get('radius', 1) copy.location = Vector(text.get('pos', [0, 0, 0])) copy.name = text.get('name', 'text') copy.data.body = text.get('text', 'text') copy.data = copy.data.copy() material = create_material(color=text.get('color', [1, 1, 1])) copy.data.materials.append(material) set_object_layer(object=copy, layer=text.get('layer', 0)) objects.append(copy) delete_object(object=object) return _link_objects(objects)
def draw_cylinders(cylinders, div=10, layer=None): # don't set the obvious defaults? bpy.ops.mesh.primitive_cylinder_add(location=[0, 0, 0], radius=1, depth=1, vertices=div) empty = bpy.context.active_object objects = [0] * len(cylinders) for index, data in enumerate(cylinders): sp = data['start'] ep = data['end'] mp = centroid_points([sp, ep]) radius = data.get('radius', 1.0) length = distance_point_point(sp, ep) obj = empty.copy() obj.name = data.get('name', 'cylinder') # get this from geometry package obj.rotation_euler[1] = acos((ep[2] - sp[2]) / length) obj.rotation_euler[2] = atan2(ep[1] - sp[1], ep[0] - sp[0]) obj.location = mp obj.scale = ((radius, radius, length)) rgb = data.get('color') or [1.0, 1.0, 1.0] set_object_color(obj, rgb) objects[index] = obj delete_object(empty) link_objects(objects, layer=layer) 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 draw_cubes(cubes, layer): bpy.ops.mesh.primitive_cube_add(size=1, location=[0, 0, 0]) empty = bpy.context.active_object objects = [0] * len(cubes) for index, data in enumerate(cubes): obj = empty.copy() obj.location = data['pos'] obj.scale *= data.get('size', 1) obj.name = data.get('name', 'cube') rgb = data.get('color') or [1.0, 1.0, 1.0] set_object_color(obj, rgb) objects[index] = obj delete_object(empty) link_objects(objects, layer=layer) return objects
def draw_points(points, layer=None): bpy.ops.object.empty_add(type='SPHERE') empty = bpy.context.active_object objects = [0] * len(points) for index, data in enumerate(points): obj = empty.copy() obj.location = data['pos'] obj.scale *= data.get('radius', 1) obj.name = data.get('name', 'point') rgb = data.get('color') or [1.0, 1.0, 1.0] obj.color = rgb + [1.0] set_object_color(obj, rgb) objects[index] = obj link_objects(objects, layer=layer) delete_object(empty) return objects
def draw_texts(texts, layer=None): bpy.ops.obj.text_add() empty = bpy.context.active_object objects = [0] * len(texts) for index, data in enumerate(texts): obj = empty.copy() obj.location = data['pos'] obj.scale *= data.get('radius', 1) obj.name = data.get('name', 'text') obj.data.body = data.get('text', 'text') rgb = data.get('color') or [1, 1, 1] obj.color = rgb + [1] objects[index] = obj delete_object(empty) link_objects(objects, layer=layer) return objects
def draw_spheres(spheres, div=10, layer=None): bpy.ops.mesh.primitive_uv_sphere_add(location=[0, 0, 0], radius=1.0, segments=div, ring_count=div) empty = bpy.context.active_object objects = [0] * len(spheres) for index, data in enumerate(spheres): obj = empty.copy() obj.location = data['pos'] obj.scale *= data.get('radius', 1.0) obj.name = data.get('name', 'sphere') values = [True] * len(obj.data.polygons) obj.data.polygons.foreach_set("use_smooth", values) rgb = data.get('color') or [1.0, 1.0, 1.0] set_object_color(obj, rgb) objects[index] = obj delete_object(empty) link_objects(objects, layer=layer) return objects
def _boolean_operation(A, B, method): from compas_blender.utilities import draw_mesh from compas_blender.utilities import delete_object from compas_blender.utilities import delete_unused_data A = draw_mesh(* A) B = draw_mesh(* B) boolean = A.modifiers.new(type="BOOLEAN", name=f"A {method} B") boolean.object = B boolean.operation = method bpy.ops.object.modifier_apply({"object": A}, modifier=boolean.name) graph = bpy.context.evaluated_depsgraph_get() C = A.evaluated_get(graph) D = bpy.data.meshes.new_from_object(C) vertices = [list(vertex.co)[:] for vertex in D.vertices] faces = [list(face.vertices)[:] for face in D.polygons] delete_object(A) delete_object(B) delete_unused_data() return vertices, faces
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). """ object = xdraw_mesh(name='pt', vertices=[[0, 0, 0]]) objects = [] for point in points: copy = object.copy() copy.location = Vector(point.get('pos', [0, 0, 0])) copy.name = point.get('name', 'point') copy.data = copy.data.copy() set_object_layer(object=copy, layer=point.get('layer', 0)) objects.append(copy) delete_object(object=object) return _link_objects(objects)
def xdraw_points(points): """ Draw a set of points (empties). Parameters: points (list): {'radius':, 'pos':, 'name':, 'layer':}. Returns: list: Created empty objects. """ bpy.ops.object.empty_add(type='SPHERE', radius=1, location=[0, 0, 0]) object = bpy.context.object objects = [] for point in points: copy = object.copy() copy.scale *= point.get('radius', 1) copy.location = Vector(point.get('pos', [0, 0, 0])) copy.name = point.get('name', 'point') set_object_layer(object=copy, layer=point.get('layer', 0)) objects.append(copy) delete_object(object=object) return _link_objects(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. """ bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=1, vertices=div, location=[0, 0, 0]) object = bpy.context.object objects = [] 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 material = create_material(color=pipe.get('color', [1, 1, 1])) copy.data.materials.append(material) set_object_layer(object=copy, layer=pipe.get('layer', 0)) objects.append(copy) delete_object(object=object) return _link_objects(objects)
def xdraw_cubes(cubes): """ Draw a set of cubes. Parameters: cubes (list): {'radius':, 'pos':, 'color':, 'name':, 'layer':}. Returns: list: Created cube objects. """ bpy.ops.mesh.primitive_cube_add() object = bpy.context.object objects = [] for cube in cubes: copy = object.copy() copy.scale *= cube.get('radius', 1) copy.location = Vector(cube.get('pos', [0, 0, 0])) material = create_material(color=cube.get('color', [1, 1, 1])) copy.data.materials.append(material) copy.name = cube.get('name', 'cube') copy.data = copy.data.copy() set_object_layer(object=copy, layer=cube.get('layer', 0)) objects.append(copy) delete_object(object=object) return _link_objects(objects)