def add_object(self, context, name): print(self, vars(self), self.bl_rna) scale_x = self.scale.x scale_y = self.scale.y bm = bmesh.new() # TODO come up with a nicer way to do this. space = getattr(self, self.space.lower(), None) if not space: print("Something horrible happened") return {"CANCELLED"} rings = space.space.points_rings() def map(p): x, y, z = p.xyz _x = self.scale.x * eval(self.fx) _y = self.scale.y * eval(self.fy) _z = self.scale.z * eval(self.fz) return Vector((_x, _y, _z)) verts0 = [bm.verts.new(map(p)) for p in rings[0]] verts0.append(verts0[0]) for ring in range(1, len(rings)): verts1 = [bm.verts.new(map(p)) for p in rings[ring]] verts1.append(verts1[0]) # make a ring faces = [bm.faces.new((verts0[i], verts1[i], verts1[i + 1], verts0[i + 1])) for i in range(len(verts0) - 1)] verts0 = verts1 mesh = bpy.data.meshes.new(name) bm.to_mesh(mesh) object_data_add(context, mesh, operator=self) bm.free()
def execute(self, context): args = {"mesh" : self.boundingMesh, "seed" : self.seed, "points" : self.attractionPoints, "iters" : self.iterations, "length" : self.branchLength, "length2" : self.branchLengthEnd, "angle" : self.angleLimit, "influence" : self.influenceRadius, "kill" : self.killRadius, "rootLoc" : self.rootLocation, "radius" : self.radius, "faces" : self.branchFaces} verts_loc, faces = add_tree(args) mesh = bpy.data.meshes.new("Tree") bm = bmesh.new() for v_co in verts_loc: bm.verts.new(v_co) for f_idx in faces: bm.faces.new([bm.verts[i] for i in f_idx]) bm.to_mesh(mesh) mesh.update() # Add the mesh as an object into the scene with this utility module from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def execute(self, context): mesh = bpy.data.meshes.new("Vertex") mesh.vertices.add(1) from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=None) return {'FINISHED'}
def execute(self, context): if self.use_abso == True: extra_helper = (self.abso_major_rad - self.abso_minor_rad) * 0.5 self.major_radius = self.abso_minor_rad + extra_helper self.minor_radius = extra_helper verts_loc, faces = add_torus(self.major_radius, self.minor_radius, self.major_segments, self.minor_segments) mesh = bpy.data.meshes.new("Torus") mesh.vertices.add(len(verts_loc) // 3) mesh.faces.add(len(faces) // 4) mesh.vertices.foreach_set("co", verts_loc) mesh.faces.foreach_set("vertices_raw", faces) mesh.update() from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def execute(self, context): verts_loc, faces = add_vert(self.width, self.height, self.depth, ) mesh = bpy.data.meshes.new("Object_Origin") bm = bmesh.new() for v_co in verts_loc: bm.verts.new(v_co) for f_idx in faces: bm.faces.new([bm.verts[i] for i in f_idx]) bm.to_mesh(mesh) mesh.update() object_utils.object_data_add(context, mesh, operator=self) bpy.ops.object.mode_set(mode = 'EDIT') bpy.ops.mesh.select_all() bpy.ops.mesh.delete(type='VERT') mesh.update() return {'FINISHED'}
def execute(self, context): grid_scale = object_utils.object_add_grid_scale(context) if self.use_abso is True: extra_helper = (self.abso_major_rad - self.abso_minor_rad) * 0.5 self.major_radius = self.abso_minor_rad + extra_helper self.minor_radius = extra_helper verts_loc, faces = add_torus(self.major_radius * grid_scale, self.minor_radius * grid_scale, self.major_segments, self.minor_segments) mesh = bpy.data.meshes.new(data_("Torus")) mesh.vertices.add(len(verts_loc) // 3) nbr_loops = len(faces) nbr_polys = nbr_loops // 4 mesh.loops.add(nbr_loops) mesh.polygons.add(nbr_polys) mesh.vertices.foreach_set("co", verts_loc) mesh.polygons.foreach_set("loop_start", range(0, nbr_loops, 4)) mesh.polygons.foreach_set("loop_total", (4,) * nbr_polys) mesh.loops.foreach_set("vertex_index", faces) mesh.update() object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def build_box(self, context): width = self.scale.x height = self.scale.y depth = self.scale.z verts = [(+1.0 * width, +1.0 * height, -1.0 * depth), (+1.0 * width, -1.0 * height, -1.0 * depth), (-1.0 * width, -1.0 * height, -1.0 * depth), (-1.0 * width, +1.0 * height, -1.0 * depth), (+1.0 * width, +1.0 * height, +1.0 * depth), (+1.0 * width, -1.0 * height, +1.0 * depth), (-1.0 * width, -1.0 * height, +1.0 * depth), (-1.0 * width, +1.0 * height, +1.0 * depth), ] edges = [] faces = [(0, 1, 2, 3), (4, 7, 6, 5), (0, 4, 5, 1), (1, 5, 6, 2), (2, 6, 7, 3), (4, 0, 3, 7), ] mesh = bpy.data.meshes.new(name="_bbox") mesh.from_pydata(verts, edges, faces) # add the mesh as an object into the scene with this utility module from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self)
def execute(self, context): mesh = bpy.data.meshes.new("qrcode") bm = self.create_qrcode_mesh() bm.to_mesh(mesh) bm.free() object_utils.object_data_add(context, mesh) return {'FINISHED'}
def execute(self, context): verts_loc, faces = add_box(self.width, self.height, self.depth, ) mesh = bpy.data.meshes.new("Box") bm = bmesh.new() for v_co in verts_loc: bm.verts.new(v_co) for f_idx in faces: bm.faces.new([bm.verts[i] for i in f_idx]) bm.to_mesh(mesh) mesh.update() # add the mesh as an object into the scene with this utility module from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def create_image_plane(self, context, material): img = material.texture_slots[0].texture.image px, py = img.size # can't load data if px == 0 or py == 0: px = py = 1 x = px / py y = 1.0 if self.use_dimension: x = (px * (1.0 / self.factor)) * 0.5 y = (py * (1.0 / self.factor)) * 0.5 verts = ((-x, -y, 0.0), (+x, -y, 0.0), (+x, +y, 0.0), (-x, +y, 0.0), ) faces = ((0, 1, 2, 3), ) mesh_data = bpy.data.meshes.new(img.name) mesh_data.from_pydata(verts, [], faces) mesh_data.update() object_data_add(context, mesh_data, operator=self) plane = context.scene.objects.active plane.data.uv_textures.new() plane.data.materials.append(material) plane.data.uv_textures[0].data[0].image = img material.game_settings.use_backface_culling = False material.game_settings.alpha_blend = 'ALPHA' return plane
def execute(self, context): A = 6.283185307179586476925286766559 / 3 verts = [(sin(A * 1), 0.0, cos(A * 1)), (sin(A * 2), 0.0, cos(A * 2)), (sin(A * 3), 0.0, cos(A * 3)), ] faces = [(0, 1, 2)] mesh = bpy.data.meshes.new("Cube") bm = bmesh.new() for v_co in verts: bm.verts.new(v_co) for f_idx in faces: bm.faces.new([bm.verts[i] for i in f_idx]) bm.to_mesh(mesh) mesh.update() object_utils.object_data_add(context, mesh) return{'FINISHED'}
def execute(self, context): mesh = bpy.data.meshes.new("Vert") mesh.vertices.add(1) from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=None) bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.delete(type='VERT') return {'FINISHED'}
def do_import(context, props, filepath): in_file = open(filepath, "r") line = in_file.readline() Nobjects = int(line) object_names = [] for i in range(0, Nobjects): line = in_file.readline() object_names.append(line.strip()) global_verts = [] offset = 0 for i_object in range(0, Nobjects): line = in_file.readline() words = line.split() Nverts = int(words[0]) Nfaces = int(words[1]) local_verts = [] for i_vert in range(0, Nverts): line = in_file.readline() words = line.split() x = float(words[0]) y = float(words[1]) z = float(words[2]) local_verts.append(Vector((x, y, z))) global_verts.append(Vector((x, y, z))) faces = [] print("Nfaces=", Nfaces) for i_face in range(0, Nfaces): line = in_file.readline() words = line.split() if len(words) < 3: return Nverts_in_face = int(words[0]) if len(words) != 1 + Nverts_in_face: return face_verts = [] for i_face_vert in range(0, Nverts_in_face): idx = int(words[i_face_vert + 1]) - offset face_verts.append(idx) faces.append(face_verts) mesh = bpy.data.meshes.new(object_names[i_object]) mesh.from_pydata(local_verts, [], faces) mesh.update() from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=None) # BPyAddMesh.add_mesh_simple(object_names[i_object], local_verts, [], faces) offset += Nverts in_file.close() return True
def add_object(self, context): verts = [Vector((0,0,0))] edges = [] faces = [] mesh = bpy.data.meshes.new(name="New Object Point") mesh.from_pydata(verts, edges, faces) # useful for development when the mesh may be invalid. # mesh.validate(verbose=True) object_data_add(context, mesh, operator=self)
def execute(self, context): mesh = bpy.data.meshes.new(name='honeycomb') comb = honeycomb_geometry(self.rows, self.cols, self.diam, self.edge) verts, faces = comb.generate() mesh.from_pydata(vertices = verts, edges = [], faces = faces) mesh.update() object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def copy_object(self, obj): """Creates a deep copy of the object. Parameters: obj: Blender object. Returns: Blender object. """ # Copy the mesh. mesh = obj.data new_mesh = self.copy_mesh(mesh) # Create the new object. object_data_add(bpy.context, new_mesh, operator=None) new_obj = bpy.context.active_object # Copy the transformation. #new_obj.location = obj.location #new_obj.rotation_quaternion = obj.rotation_quaternion #new_obj.scale = obj.scale new_obj.matrix_world = obj.matrix_world # Copy the properties. def copyprop(prop): try: v = obj[prop] new_obj[prop] = v except: pass copyprop("export") #copyprop("file") copyprop("render") copyprop("shape") # Copy the vertex groups. for i,old_group in enumerate(obj.vertex_groups): new_group = new_obj.vertex_groups.new(name=old_group.name) # Copy the vertex weights. for i,old_vert in enumerate(mesh.vertices): new_vert = new_mesh.vertices[i] for j,old_elem in enumerate(old_vert.groups): if old_elem.group >= len(obj.vertex_groups): print("WARNING: Vertex group %d/%d out of bounds for vertex %d" % (old_elem.group, len(obj.vertex_groups), i)) else: old_group = obj.vertex_groups[old_elem.group] new_group = new_obj.vertex_groups[old_elem.group] new_group.add([i], old_elem.weight, 'REPLACE') return new_obj
def _create_object(self): object_data_add(bpy.context, self.new_mesh, operator=None) self.new_obj = bpy.context.active_object def copyprop(prop): try: v = self.old_obj[prop] self.new_obj[prop] = v except: pass copyprop("export") copyprop("file") copyprop("render") copyprop("shape")
def execute(self, context): ob = context.active_object new_mesh = bpy.data.meshes.new(ob.name + "Dual") bm = bmesh.new() loops = dict() for face in ob.data.polygons: vertex = bm.verts.new(face.center) for a, b, c in triplets(face.vertices): print(a, b, c) loops[c, b] = vertex, (a, b) print(sorted((l, r) for (l, (v, r)) in loops.items())) while loops: first, (vertex, link) = loops.popitem() vertices = [vertex] while link in loops: vertex, link = loops.pop(link) vertices.append(vertex) if len(vertices) > 2: bm.faces.new(vertices) bm.to_mesh(new_mesh) new_mesh.update() if self.planarization > 0: orig_coords = {vertex: vertex.co for vertex in new_mesh.vertices} for iteration in range(self.planarization): normals = get_smoothened_normals(new_mesh.polygons) planarize(new_mesh.vertices, orig_coords, new_mesh.polygons, normals, 1/2**iteration) # add the mesh as an object into the scene new_ob = object_utils.object_data_add(context, new_mesh) new_ob.object.location = ob.location new_ob.object.rotation_euler = ob.rotation_euler return {'FINISHED'}
def execute(self, context): vert = (0, 0, 0) mesh = bpy.data.meshes.new("Object_From_Vertex") bm = bmesh.new() bm.verts.new(vert) bm.to_mesh(mesh) mesh.update() # add the mesh as an object into the scene with this utility module from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) #select the vertex for easy extrude context.active_object.data.vertices[0].select = True return {'FINISHED'}
def replace_mesh(context, name, verts, edges = [], faces = [], related_obj = None): # Remove the old object, if it exists. scn = context.scene for obj in scn.objects: if obj.name == name or obj.name.startswith(name + '.'): scn.objects.unlink(obj) bpy.data.objects.remove(obj) for mesh in bpy.data.meshes: if mesh.name == name or mesh.name.startswith(name + '.'): bpy.data.meshes.remove(mesh) # Generate a new object with the given vertices mesh = bpy.data.meshes.new(name) mesh.from_pydata(verts, edges, faces) mesh.update() no = object_utils.object_data_add(context, mesh, operator=None).object # Move the object to the same space as the original object if related_obj is not None: no.matrix_world = related_obj.matrix_world # Make sure to keep the frame object selected so that we can change the # parameters again. for obj in bpy.context.selected_objects: obj.select = False related_obj.select = True return no
def draw_curve(props, context): if props.spiral_type == 'ARCH': verts = make_spiral(props, context) if props.spiral_type == 'LOG': verts = make_spiral(props, context) if props.spiral_type == 'SPHERE': verts = make_spiral_spheric(props, context) if props.spiral_type == 'TORUS': verts = make_spiral_torus(props, context) if props.spiral_type == 'EXO': verts = make_exo_spiral(props, context) if props.spiral_type == 'CORNU': verts = make_cornu_spiral(props, context) curve_data = bpy.data.curves.new(name='Spiral', type='CURVE') curve_data.dimensions = '3D' spline = curve_data.splines.new(type=props.curve_type) ''' if props.curve_type == 0: spline = curve_data.splines.new(type='POLY') elif props.curve_type == 1: spline = curve_data.splines.new(type='NURBS') ''' spline.points.add(len(verts)*0.25-1) # Add only one quarter of points as elements in verts, because verts looks like: "x,y,z,?,x,y,z,?,x,..." spline.points.foreach_set('co', verts) new_obj = object_data_add(context, curve_data)
def execute(self, context): sponger = MengerSponge(self.level) vertices, faces = sponger.create(self.radius * 2, self.radius * 2) del sponger mesh = bpy.data.meshes.new(name='Sponge') mesh.from_pydata(vertices, [], faces) uvs = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)] mesh.uv_textures.new() for i, uvloop in enumerate(mesh.uv_layers.active.data): uvloop.uv = uvs[i%4] from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def execute(self, context): workobjs = [o.name for o in bpy.context.selected_objects] faces = [(0, 1, 2, 3), (4, 7, 6, 5), (0, 4, 5, 1), (1, 5, 6, 2), (2, 6, 7, 3), (4, 0, 3, 7), ] for objname in workobjs: obj = bpy.data.objects[objname] mesh = bpy.data.meshes.new("BoundingBox") bm = bmesh.new() for v_co in obj.bound_box: bm.verts.new(v_co) for f_idx in faces: bm.verts.ensure_lookup_table() bm.faces.new([bm.verts[i] for i in f_idx]) bm.to_mesh(mesh) mesh.update() self.location = obj.location self.rotation = obj.rotation_euler bbox = object_utils.object_data_add(context, mesh, operator=self) # does a bounding box need to display more than the bounds?? bbox.object.draw_type = 'BOUNDS' bbox.object.scale = obj.scale bbox.object.hide_render = True return {'FINISHED'}
def create_mesh_object(context, verts, edges, faces, name): # Create new mesh mesh = bpy.data.meshes.new(name) # Make a mesh from a list of verts/edges/faces. mesh.from_pydata(verts, [], faces) # Update mesh geometry after adding stuff. mesh.update() return object_utils.object_data_add(context, mesh, operator=None)
def execute(self, context): if self.arc_div <=0 and self.lin_div <= 0: self.report({'ERROR'}, 'Either Arc Divisions or Linear Divisions must be greater than zero!') return {'CANCELLED'} if not self.no_limit: if self.vert_count > self.sanity_check_verts: self.report({'ERROR'}, 'More than '+str(self.sanity_check_verts)+' vertices! Check "No Limit" to proceed') return {'CANCELLED'} verts, faces = round_cube(self.radius, self.arc_div, self.lin_div, self.size, self.div_type, self.odd_axis_align) mesh = bpy.data.meshes.new('Roundcube') mesh.from_pydata(verts,[],faces) object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def create_mesh_object(context, verts, edges, faces, name): mesh = bpy.data.meshes.new(name) mesh.from_pydata(verts, edges, faces) mesh.update() from bpy_extras import object_utils return object_utils.object_data_add(context, mesh, operator=None)
def execute(self, context): if self.preset == 'SPHERE': self.radius = 1.0 self.size = (0.,0.,0.) self.arc_div = max(self.arc_div, 4) self.lin_div = 0. self.div_type = 'CORNERS' elif self.preset == 'CAPSULE': self.radius = 0.5 self.size = (0.,0.,3.) self.arc_div = max(self.arc_div, 4) self.lin_div = 0. self.div_type = 'CORNERS' elif self.preset == 'CUBOID': self.radius = 0.25 self.size = (2.,2.,2.) self.arc_div = max(self.arc_div, 4) self.lin_div = 0. self.div_type = 'CORNERS' elif self.preset == '3DGRID': self.radius = 1.0 self.arc_div = 1 self.lin_div = max(self.lin_div, 5.) self.div_type = 'ALL' self.size = (2.,2.,2.) self.odd_axis_align = False self.preset = 'CUSTOM' if self.arc_div <=0 and self.lin_div <= 0: self.report({'ERROR'}, 'Either Arc Divisions or Linear Divisions must be greater than zero!') return {'CANCELLED'} if not self.no_limit: arcdiv, lindiv, vert_count = round_cube(self.radius, self.arc_div, self.lin_div, self.size, self.div_type, self.odd_axis_align, True) if vert_count > self.sanity_check_verts: self.report({'ERROR'}, 'More than '+str(self.sanity_check_verts)+' vertices! Check "No Limit" to proceed') return {'CANCELLED'} verts, faces = round_cube(self.radius, self.arc_div, self.lin_div, self.size, self.div_type, self.odd_axis_align) mesh = bpy.data.meshes.new('Roundcube') mesh.from_pydata(verts,[],faces) object_utils.object_data_add(context, mesh, operator=None) return {'FINISHED'}
def vefm_add_object(selfobj): for i in range(len(selfobj.verts)): selfobj.verts[i].index = i v = [el.vector for el in selfobj.verts] e = [[edge.a.index, edge.b.index] for edge in selfobj.edges] if type(selfobj.faces[0]) == type([]): # PKHG should be a list of vertices, which have an index f = [[v.index for v in face] for face in selfobj.faces] else: f = [[v.index for v in face.vertices] for face in selfobj.faces] m = bpy.data.meshes.new(name=selfobj.name) m.from_pydata(v, e, f) # useful for development when the mesh may be invalid. m.validate(verbose=False) object_data_add(bpy.context, m, operator=None)
def add_object(self, context): scale_x = self.scale.x scale_y = self.scale.y verts = [Vector((-1 * scale_x, 1 * scale_y, 0)), Vector((1 * scale_x, 1 * scale_y, 0)), Vector((1 * scale_x, -1 * scale_y, 0)), Vector((-1 * scale_x, -1 * scale_y, 0)), ] edges = [] faces = [[0, 1, 2, 3]] mesh = bpy.data.meshes.new(name="New Object Mesh") mesh.from_pydata(verts, edges, faces) # useful for development when the mesh may be invalid. # mesh.validate(verbose=True) object_data_add(context, mesh, operator=self)
def create_object(self, name, context): from bpy_extras import object_utils data = self._create_object_data(name) ob = object_utils.object_data_add(context, data).object # XXX hack: force exact object name by setting it explicitly ob.name = name return ob
def add_propeller_object(self, context): params = [self.nb_blades, self.thi_mesh, self.coef_ba, self.coef_bf, self.devers, self.step, self.hub_contour, self.blade_width, self.incidence, self.angulae_lag, self.blade_radius, self.cut_rad, self.bl_root_rad, self.boring_rad, self.blade_thick, self.round_root, self.extra_hub] verts, faces = makePropeller(params) mesh_data = bpy.data.meshes.new(name="Propeller") mesh_data.from_pydata(verts, [], faces) mesh_data.update() res = object_data_add(context, mesh_data, operator=self)
def create_mesh_object(context, verts, edges, faces, name): # Create new mesh mesh = bpy.data.meshes.new(name) # Make a mesh from a list of verts/edges/faces. mesh.from_pydata(verts, edges, faces) # Update mesh geometry after adding stuff. mesh.update() from bpy_extras import object_utils return object_utils.object_data_add(context, mesh, operator=None)
def add_object(self, context): row_num = self.wall.y // self.block.y verts = [] edges = [] faces = [] offset = self.offset_dir for i in range(0, int(row_num)): row = BlockRow(Vector((0, i * self.block.y)), self) v, f = row.build(offset, len(verts)) verts.extend(v) faces.extend(f) if self.offset: offset = not offset mesh = bpy.data.meshes.new(name="Block Wall") mesh.from_pydata(verts, edges, faces) # useful for development when the mesh may be invalid. # mesh.validate(verbose=True) object_data_add(context, mesh, operator=self)
def execute(self, context): verts_loc, faces, uvs = add_stairs( self.width, self.height, self.depth, self.stepType, self.numSteps, self.stepHeight, self.sides ) mesh = bpy.data.meshes.new("Stairs") bm = bmesh.new() for v_co in verts_loc: bm.verts.new(v_co) bm.verts.ensure_lookup_table() for f_idx in faces: bm.faces.new([bm.verts[i] for i in f_idx]) #create a uv layer and generate uv coords uv_layer = bm.loops.layers.uv.new() loop = bm.loops.layers.uv[0] for face, faceUvs in zip(bm.faces, uvs): for loop, uv in zip(face.loops, faceUvs): loop[uv_layer].uv = uv bm.to_mesh(mesh) mesh.update() # add the mesh as an object into the scene with this utility module from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def make_cublet(self, context, cublet): from bpy_extras import object_utils verts_loc, faces = self.generate_box_points(1, 1, 1) box_mesh = self.get_mesh(verts_loc, faces, repr(cublet)) self.color_mesh(box_mesh, (0.05, 0.05, 0.05)) # add the mesh as an object into the scene with this utility module cube_obj = object_utils.object_data_add(context, box_mesh) cube_obj.object.location = [ coord * (2 * self.cublet_radius + self.margin) for coord in (cublet.x, cublet.y, cublet.z) ] for sticker in cublet.stickers: dimensions = [0, 0, 0] location = [0, 0, 0] #cubl_xyz = [cublet.x, cublet.y, cublet.z] for i, val in enumerate(sticker['face'].value): # Use for absolute position, comment for relative position to cublet #location[i] = cubl_xyz[i]*(2*self.cublet_radius + self.margin) if val != 0: dimensions[i] = 0.02 location[i] += val * 1.02 else: dimensions[i] = 0.9 #print("{} {} {}".format(i, val, dimensions)) verts_loc, faces = self.generate_box_points(*dimensions) sticker_mesh = self.get_mesh(verts_loc, faces, repr(sticker)) self.color_mesh(sticker_mesh, sticker['color']) sticker_obj = object_utils.object_data_add(context, sticker_mesh) sticker_obj.object.location = location sticker_obj.object.parent = cube_obj.object self.cube[cublet.x + 1][cublet.y + 1][cublet.z + 1] = cube_obj return {'FINISHED'}
def CriaLinhaPontosDef(self, context): context = bpy.context obj = context.active_object scn = context.scene Pontos = [ obj for obj in bpy.context.scene.objects if fnmatch.fnmatchcase(obj.name, "PT_Linh*") ] vertices = [] for i in Pontos: VetorAtual = i.location VetX = i.location[0] VetY = i.location[1] VetZ = i.location[2] vertices.append((VetX, VetY, VetZ)) edges = [] for i in range(len(vertices)): edges.append([i, i + 1]) del (edges[-1]) # Apaga o último elemento da cena faces = [] mesh = bpy.data.meshes.new(name="LineSolid") mesh.from_pydata(vertices, edges, faces) object_data_add(context, mesh, operator=self) bpy.context.object.location = 0, 0, 0 bpy.ops.object.convert(target='CURVE') bpy.context.object.data.fill_mode = 'FULL' bpy.context.object.data.bevel_depth = 0.915 bpy.context.object.data.bevel_resolution = 7
def create_mesh_object_for_img_spec(self, context, img_spec): # check if mesh with image name allready exists NAME = img_spec.image.name if not self.reuse_existing: mesh = create_mesh(NAME) elif not NAME in bpy.data.meshes.keys(): mesh = create_mesh(NAME) else: mesh = bpy.data.meshes[NAME] set_mesh_verticies(mesh, img_spec.size, self.origin) plane_object = object_data_add(context, mesh, operator=self) return plane_object
def create_bezier(context, bm, twists, crossing_angle, crossing_strength, handle_type, weave_up, weave_down, materials): builder = BezierBuilder(bm, crossing_angle, crossing_strength, handle_type, weave_up, weave_down, materials) visit_strands(bm, twists, builder) curve = builder.curve orig_obj = context.active_object # Create an object from the curve object_utils.object_data_add(context, curve, operator=None) # Set the handle type (this is faster than setting it pointwise) bpy.ops.object.editmode_toggle() bpy.ops.curve.select_all(action="SELECT") bpy.ops.curve.handle_type_set(type=HANDLE_TYPE_MAP[handle_type]) # Some blender versions lack the default bpy.ops.curve.radius_set(radius=1.0) bpy.ops.object.editmode_toggle() # Restore active selection curve_obj = context.active_object context.view_layer.objects.active = orig_obj return curve_obj
def invoke(self, context, event): active = get_active_object() auto_edit_mode = bpy.context.preferences.edit.use_enter_edit_mode # working around blender bug, see https://github.com/CGCookie/retopoflow/issues/786 bpy.context.preferences.edit.use_enter_edit_mode = False for o in bpy.data.objects: o.select_set(False) mesh = bpy.data.meshes.new('RetopoFlow') obj = object_utils.object_data_add(context, mesh, name='RetopoFlow') obj.select_set(True) context.view_layer.objects.active = obj obj.matrix_world = active.matrix_world bpy.ops.object.mode_set(mode='EDIT') bpy.context.preferences.edit.use_enter_edit_mode = auto_edit_mode return bpy.ops.cgcookie.retopoflow('INVOKE_DEFAULT')
def CriaMaxilaDef(self, context): verts = [Vector((-34, 30, 0)), Vector((-34, -30, 0)), Vector((-4, -30, 10)), Vector((-4, 30, 10)), Vector((4, 30, 10)), Vector((4, -30, 10)), Vector((34, -30, 0)), Vector((34, 30, 0)), ] edges = [] faces = [[0, 1, 2, 3],[4, 5, 6, 7]] mesh = bpy.data.meshes.new(name="Maxila") mesh.from_pydata(verts, edges, faces) object_data_add(context, mesh, operator=self) bpy.ops.object.modifier_add(type='SOLIDIFY') bpy.context.object.modifiers["Solidify"].thickness = 0.3 bpy.context.object.modifiers["Solidify"].offset = 0
def add_object(self, context, data, filename): m_inst = data['model']['meshInstances'] model = data['model'] for m in m_inst: active_node = model['nodes'][m['node']] name = active_node['name'].replace(' ', '_') position = active_node['position'] rotation = active_node['rotation'] scale = active_node['scale'] try: verts, edges, faces = objectify(model, m) # verts = [[i[0]* scale[0], i[1]* scale[1], i[2]* scale[2]] for i in verts] clean_filename = ''.join(filename.split('.')[0]) mesh = bpy.data.meshes.new(name=f"{name}") mesh.from_pydata(verts, edges, faces) mesh.validate() # Update mesh geometry after adding stuff. mesh.update() # useful for development when the mesh may be invalid. # mesh.validate(verbose=True) context.area.ui_type = 'VIEW_3D' object_data_add(context, mesh, operator=None) obj = bpy.context.active_object mesh['position'] = position obj.location = position obj.scale = scale except: # Possible dissolve fails for some edges, but don't fail silently in case this is a real bug. import traceback traceback.print_exc()
def execute(self, context): verts_loc, faces = add_box( self.width, self.height, self.depth, ) mesh = bpy.data.meshes.new("Box") mesh.vertices.add(len(verts_loc) // 3) mesh.faces.add(len(faces) // 4) mesh.vertices.foreach_set("co", verts_loc) mesh.faces.foreach_set("vertices_raw", faces) mesh.update() # add the mesh as an object into the scene with this utility module from bpy_extras import object_utils object_utils.object_data_add(context, mesh, operator=self) return {'FINISHED'}
def make_curve(self, context, verts, lh, rh): target = bpy.context.scene.objects.active curve_data = bpy.data.curves.new(name=target.name + '_Bevel', type='CURVE') curve_data.dimensions = '3D' for p in range(len(verts)): c = 0 spline = curve_data.splines.new(type='BEZIER') spline.use_cyclic_u = True spline.bezier_points.add(len(verts[p]) / 3 - 1) spline.bezier_points.foreach_set('co', verts[p]) for bp in spline.bezier_points: bp.handle_left_type = 'ALIGNED' bp.handle_right_type = 'ALIGNED' bp.handle_left.xyz = lh[p][c] bp.handle_right.xyz = rh[p][c] c += 1 object_data_add(context, curve_data, operator=self) target.data.bevel_object = bpy.context.scene.objects.active bpy.context.scene.objects.active = target
def execute(self, context): mesh = bpy.data.meshes.new("wRing") wD = mesh.wData wD.rad_1 = self.radius_out wD.inn = self.use_inner wD.rad_2 = self.radius_in wD.seg_1 = self.seg_perimeter wD.seg_2 = self.seg_radius wD.sec_f = self.sector_from wD.sec_t = self.sector_to wD.wType = 'WRING' mesh.from_pydata(*update_WRing(wD)) mesh.update() object_utils.object_data_add(context, mesh, operator=None) bpy.ops.object.shade_smooth() context.object.data.use_auto_smooth = True return {'FINISHED'}
def make_curve(self, context, verts, lh, rh): types = self.types curve_data = bpy.data.curves.new(name='CurlyCurve', type='CURVE') curve_data.dimensions = '3D' for p in range(len(verts)): c = 0 spline = curve_data.splines.new(type='BEZIER') spline.bezier_points.add(len(verts[p]) / 3 - 1) spline.bezier_points.foreach_set('co', verts[p]) for bp in spline.bezier_points: bp.handle_left_type = 'ALIGNED' bp.handle_right_type = 'ALIGNED' bp.handle_left.xyz = lh[p][c] bp.handle_right.xyz = rh[p][c] c += 1 # something weird with this one if types == 1 or types == 2 or types == 3: spline.bezier_points[3].handle_left.xyz = lh[p][3] object_data_add(context, curve_data, operator=self)
def add_object(self, context): scale_x = 1 #self.scale.x scale_y = 1 #self.scale.y scale_z = 1 #self.scale.z print('self :', self) verts = [ Vector((-1 * scale_x, 1 * scale_y, 0)), Vector((1 * scale_x, 1 * scale_y, 0)), Vector((1 * scale_x, -1 * scale_y, 0)), Vector((-1 * scale_x, -1 * scale_y, 0)), ] edges = [] faces = [[0, 1, 2, 3]] mesh = bpy.data.meshes.new(name="New Object Mesh") mesh.from_pydata(verts, edges, faces) # useful for development when the mesh may be invalid. # mesh.validate(verbose=True) object_data_add(context, mesh, operator=self)
def addBarChart(self, context): """ Build the bar chart mesh, and add it to the scene """ # Set a shortcut to the scene. s = context.scene # Load the chart data. data = self.loadData(self.filepath) # Data series stepping variable. yStep = 0 # Iterate over each series in the data. for series in data: # Data point stepping variable. xStep = 0 # Iterate over each data point in the series. for point in series: # Create a bar for this point. b = self.createBar((xStep * s.barS), (yStep * s.barS), point, s.barX, s.barY) # Create a new mesh instance, and load the mesh data. m = bpy.data.meshes.new("Bar_%d_%d" % (xStep, yStep)) b.to_mesh(m) m.update() # Add it to the scene. object_utils.object_data_add(context, m, operator=self) # Advance the X-direction step. xStep += 1 # Advance the Y-direction step. yStep += 1
def make_segment(verts_loc, faces): mesh = bpy.data.meshes.new("Weld Segment") bm = bmesh.new() for v_co in verts_loc: bm.verts.new(v_co) bm.verts.ensure_lookup_table() for f_idx in faces: bm.faces.new([bm.verts[i] for i in f_idx]) bm.to_mesh(mesh) mesh.update() # add the mesh as an object into the scene then adds modifiers context = bpy.context from bpy_extras import object_utils object_utils.object_data_add(context, mesh) bpy.ops.object.shade_smooth() bpy.ops.object.modifier_add(type="ARRAY") bpy.ops.object.modifier_add(type="CURVE")
def execute(self, context): # create a new empty mesh me = bpy.data.meshes.new(name='Ladder') # link the object to the scene & make it active and selected ob = object_utils.object_data_add(context, me, operator=self) # mark it as a Ladder object ob.ladder.ladder = True # create the geometry based on properties in the Ladder panel updateLadder(self, context) return {'FINISHED'}
def add_object(self, context, num, rot): #rot = (pi)/num rot = rot * pi / 180 verts = [] faces = [] for v in range(num): (x, y, z) = (cos((2 * pi * v) / num), sin((2 * pi * v) / num), 0) verts.append((x, y, z)) for v in range(num): (x, y, z) = (cos(((2 * pi * v) / num) + rot), sin(((2 * pi * v) / num) + rot), 1) verts.append((x, y, z)) for f in range(num - 1): (a, b, c, d) = (f, f + 1, f + num + 1, f + num) faces.append((a, b, c, d)) faces.append((num - 1, 0, num, 2 * num - 1)) faces.append([i for i in range(num - 1, -1, -1)]) faces.append([i for i in range(num, 2 * num)]) edges = [] mesh = bpy.data.meshes.new(name="Twister") mesh.from_pydata(verts, edges, faces) object_data_add(context, mesh, operator=self)
def add_object(self, context): scale_x = self.scale.x scale_y = self.scale.y scale_z = self.scale.z verts = [ Vector((0 * scale_x, 0 * scale_y, 0 * scale_z)), Vector((0 * scale_x, 1 * scale_y, 0 * scale_z)), Vector((1 * scale_x, 1 * scale_y, 0 * scale_z)), Vector((1 * scale_x, 0 * scale_y, 0 * scale_z)), Vector((0 * scale_x, 0 * scale_y, 1 * scale_z)), Vector((0 * scale_x, 1 * scale_y, 1 * scale_z)), Vector((1 * scale_x, 1 * scale_y, 1 * scale_z)), Vector((1 * scale_x, 0 * scale_y, 1 * scale_z)), ] edges = [] faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 4, 7, 3], [1, 5, 6, 2], [0, 1, 5, 4], [3, 2, 6, 7]] mesh = bpy.data.meshes.new(name="Box Parametric") mesh.from_pydata(verts, edges, faces) object_data_add(context, mesh, operator=self)
def make_curve(self, context, verts, lh, rh): types = self.types # create object if bpy.context.mode == 'EDIT_CURVE': Curve = context.active_object for p in range(len(verts)): c = 0 newSpline = Curve.data.splines.new(type='BEZIER') # newSpline newSpline.bezier_points.add(len(verts[p]) / 3 - 1) newSpline.bezier_points.foreach_set('co', verts[p]) for bp in newSpline.bezier_points: bp.handle_left_type = 'ALIGNED' bp.handle_right_type = 'ALIGNED' bp.handle_left.xyz = lh[p][c] bp.handle_right.xyz = rh[p][c] c += 1 # something weird with this one if types == 1 or types == 2 or types == 3: newSpline.bezier_points[3].handle_left.xyz = lh[p][3] else: # create curve newCurve = bpy.data.curves.new(name='CurlyCurve', type='CURVE') # curvedatablock for p in range(len(verts)): c = 0 newSpline = newCurve.splines.new(type='BEZIER') # newSpline newSpline.bezier_points.add(len(verts[p]) / 3 - 1) newSpline.bezier_points.foreach_set('co', verts[p]) for bp in newSpline.bezier_points: bp.handle_left_type = 'ALIGNED' bp.handle_right_type = 'ALIGNED' bp.handle_left.xyz = lh[p][c] bp.handle_right.xyz = rh[p][c] c += 1 # something weird with this one if types == 1 or types == 2 or types == 3: newSpline.bezier_points[3].handle_left.xyz = lh[p][3] # create object with newCurve Curve = object_data_add(context, newCurve, operator=self) # place in active scene Curve.select_set(True) # set curveOptions Curve.data.dimensions = '3D' Curve.data.use_path = True
def add_curve_from_csv(context, file_path): file_path = pathlib.Path(file_path) vertices = get_vertices_from_csv(file_path) name = file_path.stem curve_data = bpy.data.curves.new(name, 'CURVE') curve_data.twist_mode = 'Z_UP' curve_data.dimensions = '3D' spline = curve_data.splines.new('NURBS') spline.tilt_interpolation = 'BSPLINE' spline.points.add(len(vertices) - 1) apply_tilt(spline, vertices) curve_object = bpy.data.objects.new(name + " Object", curve_data) curve_object.location = (0, 0, 0) object_data_add(context, curve_data) return {'FINISHED'}
def execute(self, context): mesh = bpy.data.meshes.new("wScrew") wD = mesh.wData wD.seg_1 = self.rounds wD.seg_2 = self.segments wD.siz_z = self.height wD.rad_1 = self.radius_1 wD.rad_2 = self.radius_2 wD.smo = self.smoothed wD.wType = 'WSCREW' mesh.from_pydata(*update_WScrew(wD)) mesh.update() object_utils.object_data_add(context, mesh, operator=None) bpy.ops.object.shade_smooth() context.object.data.use_auto_smooth = True context.object.data.auto_smooth_angle = 1.0 return {'FINISHED'}
def create_direction_obj(name, location, direction): mesh = bpy.data.meshes.new(name) bm = bmesh.new() v0 = bm.verts.new((0, 0, 0)) v1 = bm.verts.new(direction) bm.edges.new((v0, v1)) bm.to_mesh(mesh) mesh.update() ob = object_utils.object_data_add(bpy.context, mesh, name=name) ob = ob.object if location: ob.location = location
def execute(self, context): if bpy.context.mode == "OBJECT": if context.selected_objects != [] and context.active_object and \ (context.active_object.data is not None) and ('Bolt' in context.active_object.data.keys()) and \ (self.change == True): obj = context.active_object use_auto_smooth = bool(obj.data.use_auto_smooth) # Copy value, do not take a reference use_smooth = bool(obj.data.polygons[0].use_smooth) # Copy value, do not take a reference mesh = createMesh.Create_New_Mesh(self, context) # Modify existing mesh data object by replacing geometry (but leaving materials etc) bm = bmesh.new() bm.from_mesh(mesh) bm.to_mesh(obj.data) bm.free() # Preserve flat/smooth choice. New mesh is flat by default obj.data.use_auto_smooth = use_auto_smooth if use_smooth: bpy.ops.object.shade_smooth() bpy.data.meshes.remove(mesh) try: bpy.ops.object.vertex_group_remove(all=True) except: pass else: mesh = createMesh.Create_New_Mesh(self, context) obj = object_utils.object_data_add(context, mesh, operator=self) obj.data["Bolt"] = True obj.data["change"] = False for prm in BoltParameters(): obj.data[prm] = getattr(self, prm) if bpy.context.mode == "EDIT_MESH": obj = context.edit_object mesh = createMesh.Create_New_Mesh(self, context) bm = bmesh.from_edit_mesh(obj.data) # Access edit mode's mesh data bm.from_mesh(mesh) # Append new mesh data bmesh.update_edit_mesh(obj.data) # Flush changes (update edit mode's view) bpy.data.meshes.remove(mesh) # Remove temporary mesh return {'FINISHED'}
def createMeshObject(context, verts, edges, faces, name): # Create new mesh mesh = bpy.data.meshes.new(name) # Make a mesh from a list of verts/edges/faces. mesh.from_pydata(verts, edges, faces) # Set mesh to use auto smoothing: mesh.use_auto_smooth = True # Update mesh geometry after adding stuff. mesh.update() return object_utils.object_data_add(context, mesh, operator=None)
def createMeshFromData(context, name, verts, faces): # Create mesh and object # # utility function for creation of a blender object from vertices and faces me = bpy.data.meshes.new(name + 'Mesh') ob = bpy.data.objects.new(name, me) ob.location = (0, 0, 0) ob.show_name = True me.from_pydata(verts, [], faces) me.update() return object_utils.object_data_add(context, me, operator=None)
def oΔΔ建骨架(context, OBJECTPRESET, s物名, id入): sID名 = OBJECTPRESET["ARMATURE_PROPERTY"].get("ID_NAME") i骨数 = OBJECTPRESET["ARMATURE_PROPERTY"].get("BONE_NUM") if (id入): id = id入 else: id = idΔΔ找id场景相同LIB(s要找名=sID名, i点数=i骨数, s类型="ARMATURE") #----找不到---------------------------------------------------------------- if (id == None): id = bpy.data.armatures.new(name=s物名) #CURVE, SURFACE, FONT #bpy.ops.object.mode_set(mode="EDIT");#编辑骨只在编辑状态下才有数据 #----新建骨架---------------------------------------------------------------- o骨 = bpy.data.objects.new(s物名, id) bpy.context.scene.objects.link(o骨) #o骨.data.name="HAIRBONE"; #bpy.data.meshes.remove(id); #----EB要在编辑模式下---------------------------------------------- if (context.scene.objects.active != o骨): context.scene.objects.active = o骨 if (o骨.mode != "EDIT"): bpy.ops.object.mode_set(mode="EDIT") #----增加eb---------------------------------------------------------------- Ceb = id.edit_bones for s骨名, D属性 in OBJECTPRESET["ARMATURE_PROPERTY"][ "EDITBONE_PROPERTY"].items(): eb = Ceb.new(name=s骨名) Δ字典赋予物体LIB(D属性, eb, []) if ("tail" in D属性): print("apply tail", D属性["tail"]) for s骨名, D属性 in OBJECTPRESET["ARMATURE_PROPERTY"][ "EDITBONE_PROPERTY"].items(): #赋予父骨 s骨父名 = D属性.get("PARENT_NAME") if (s骨父名 != None): Ceb[s骨名].parent = Ceb[D属性["PARENT_NAME"]] if (o骨.mode != 'OBJECT'): bpy.ops.object.mode_set(mode='OBJECT') #只在POSE模式 或物体下才能赋值posebone Lpb = o骨.pose.bones for s骨名, D属性 in OBJECTPRESET["ARMATURE_PROPERTY"][ "POSEBONE_PROPERTY"].items(): Δ字典赋予物体LIB(D属性, Lpb[s骨名], []) return o骨 return object_utils.object_data_add(context, id, operator=None).object