Python bmesh 模块,from_edit_mesh() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用bmesh.from_edit_mesh()

项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def create_verts(self,width,height,pos,me,tag_hide=False):
        bpy.ops.object.mode_set(mode="EDIT")
        bm = bmesh.from_edit_mesh(me)
        vert1 = bm.verts.new(Vector((0,0,-height))*self.scale)
        vert2 = bm.verts.new(Vector((width,0,-height))*self.scale)
        vert3 = bm.verts.new(Vector((width,0,0))*self.scale)
        vert4 = bm.verts.new(Vector((0,0,0))*self.scale)

        bm.faces.new([vert1,vert2,vert3,vert4])

        bmesh.update_edit_mesh(me)

        if tag_hide:
            for vert in bm.verts:
                vert.hide = True    

            for edge in bm.edges:
                edge.hide = True    

        bmesh.update_edit_mesh(me)
        bpy.ops.object.mode_set(mode="OBJECT")
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def remove_base_mesh(obj):
    bpy.ops.object.mode_set(mode="EDIT")
    bm = bmesh.from_edit_mesh(obj.data)
    bm.verts.ensure_lookup_table()
    verts = []

    if "coa_base_sprite" in obj.vertex_groups:
        v_group_idx = obj.vertex_groups["coa_base_sprite"].index
        for i,vert in enumerate(obj.data.vertices):
            for g in vert.groups:
                if g.group == v_group_idx:
                    verts.append(bm.verts[i])
                    break

    bmesh.ops.delete(bm,geom=verts,context=1)
    bm = bmesh.update_edit_mesh(obj.data) 
    bpy.ops.object.mode_set(mode="OBJECT")
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def unwrap_with_bounds(obj,uv_idx):
    bpy.ops.object.mode_set(mode="EDIT")
    me = obj.data
    bm = bmesh.from_edit_mesh(me)
    bm.verts.ensure_lookup_table()
    uv_layer = bm.loops.layers.uv[uv_idx]
    scale_x = 1.0 / get_local_dimension(obj)[0] * obj.coa_tiles_x
    scale_z = 1.0 / get_local_dimension(obj)[1] * obj.coa_tiles_y
    offset = [get_local_dimension(obj)[2][0] * scale_x , get_local_dimension(obj)[2][1] * scale_z]
    for i,v in enumerate(bm.verts):
        for l in v.link_loops:
            uv_data = l[uv_layer]
            uv_data.uv[0] = (bm.verts[i].co[0] * scale_x) - offset[0]
            uv_data.uv[1] = (bm.verts[i].co[2] * scale_z)+1 - offset[1]

    bmesh.update_edit_mesh(me)
    bm.free()
    bpy.ops.object.mode_set(mode="OBJECT")
项目:Blender-Add-ons-3DM-Snow    作者:3DMish    | 项目源码 | 文件源码
def bmesh_copy_from_object(obj, transform=True, triangulate=True, apply_modifiers=False):
    assert(obj.type == 'MESH')

    if apply_modifiers and obj.modifiers:
        import bpy
        me = obj.to_mesh(bpy.context.scene, True, 'PREVIEW', calc_tessface=False)
        bm = bmesh.new(); bm.from_mesh(me); bpy.data.meshes.remove(me)
        del bpy
    else:
        me = obj.data
        if obj.mode == 'EDIT': bm_orig = bmesh.from_edit_mesh(me); bm = bm_orig.copy()
        else: bm = bmesh.new(); bm.from_mesh(me)

    if transform: bm.transform(obj.matrix_world)
    if triangulate: bmesh.ops.triangulate(bm, faces=bm.faces)
    return bm
项目:mesh_doshape_tools    作者:YHOYO    | 项目源码 | 文件源码
def execute(self, context):
        oa = bpy.context.active_object
        obj = bpy.context.object
        me = obj.data
        bm = bmesh.from_edit_mesh(me)

        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                override = bpy.context.copy()
                viewport = area.regions[4]
                v_m = area.spaces[0].region_3d.view_matrix  #orientation matrix

                vert = [v for v in bm.verts if (v.select == True and v.hide == False)]
                if len(vert) ==1:
                    self.main1(context, self.chboxVert0, self.grados, self.chboxreloj)
                elif len(vert) == 3:
                    self.main3(context, self.chboxVert0, self.chboxVert1, self.chboxVert2,self.grados, self.chboxreloj)
                else:    
                    msg = "select 1 OR 3  vertices"
                    self.report({"WARNING"}, msg)
                    return {'CANCELLED'}  


        return {'FINISHED'}
项目:mesh_doshape_tools    作者:YHOYO    | 项目源码 | 文件源码
def separarcaras():

    obj = bpy.context.object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

        ### separar caras###
    listabordes = []

    for borde in bm.edges:
        listabordes.append(borde)

    bmesh.ops.split_edges(bm, edges=listabordes)

    bpy.ops.mesh.select_all(action='SELECT')

    bmesh.update_edit_mesh(me, True)
项目:mesh_doshape_tools    作者:YHOYO    | 项目源码 | 文件源码
def separarcaras():

    obj = bpy.context.object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

        ### separar caras###
    listabordes = []

    for borde in bm.edges:
        listabordes.append(borde)

    bmesh.ops.split_edges(bm, edges=listabordes)

    bpy.ops.mesh.select_all(action='SELECT')

    bmesh.update_edit_mesh(me, True)
项目:mesh_doshape_tools    作者:YHOYO    | 项目源码 | 文件源码
def execute(self, context):
        oa = bpy.context.active_object
        obj = bpy.context.object
        me = obj.data
        bm = bmesh.from_edit_mesh(me)

        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                override = bpy.context.copy()
                viewport = area.regions[4]
                v_m = area.spaces[0].region_3d.view_matrix  #orientation matrix

                vert = [v for v in bm.verts if (v.select == True and v.hide == False)]
                if len(vert) ==3:
                    self.main(context, self.chboxVert0, self.chboxVert1, self.chboxVert2, self.cortes)
                else:    
                    msg = "select  3  vertices"
                    self.report({"WARNING"}, msg)
                    return {'CANCELLED'}  


        return {'FINISHED'}
项目:io_scene_kcl    作者:Syroot    | 项目源码 | 文件源码
def scene_update_post_handler(scene):
    obj = scene.objects.active
    if not obj:
        return
    if obj.mode == "EDIT" and obj.type == "MESH":
        # Ensure to have an instance of the edit bmesh in the global dictionary to retrieve it in update methods.
        bm = kcl_dict.setdefault(obj.name, bmesh.from_edit_mesh(obj.data))
        face = bm.faces.active # TODO: bm instance might be dead here due to previous editing.
        flag_layer = bm.faces.layers.int.get("kcl_flags")
        if face and flag_layer:
            flags = face[flag_layer]
            # Update the flags stored in the window manager.
            kcl_dict["update_by_code"] = True
            bpy.context.window_manager.kcl_flag = flags
            bpy.context.window_manager.kcl_is_lakitu = (flags & 0x0010) != 0 # Bit 5
            kcl_dict["update_by_code"] = False
    else:
        kcl_dict.clear()
项目:mesh_maze    作者:elfnor    | 项目源码 | 文件源码
def execute(self, context):
        """build maze
        """
        obj = context.object

        bm = bmesh.from_edit_mesh(obj.data)

        if len(self.vert_centers) == 0:
            self.update = True
        maze_params = self.get_maze_params()
        bpy.ops.mesh.select_mode(type='EDGE')

        bm, self.link_centers, self.vert_centers = generate_maze(bm, maze_params)
        self.update = False

        bmesh.update_edit_mesh(obj.data, destructive=True)

        return {'FINISHED'}
项目:Modeling-Cloth    作者:the3dadvantage    | 项目源码 | 文件源码
def get_bmesh(ob=None):
    '''Returns a bmesh. Works either in edit or object mode.
    ob can be either an object or a mesh.'''
    obm = bmesh.new()
    if ob is None:
        mesh = bpy.context.object.data
    if 'data' in dir(ob):
        mesh = ob.data
        if ob.mode == 'OBJECT':
            obm.from_mesh(mesh)
        elif ob.mode == 'EDIT':
            obm = bmesh.from_edit_mesh(mesh)    
    else:
        mesh = ob
        obm.from_mesh(mesh)
    return obm
项目:Modeling-Cloth    作者:the3dadvantage    | 项目源码 | 文件源码
def get_bmesh(ob='empty'):
    '''Returns a bmesh. Works either in edit or object mode.
    ob can be either an object or a mesh.'''
    obm = bmesh.new()
    if ob == 'empty':
        mesh = bpy.context.object.data
    if 'data' in dir(ob):
        mesh = ob.data
        if ob.mode == 'OBJECT':
            obm.from_mesh(mesh)
        elif ob.mode == 'EDIT':
            obm = bmesh.from_edit_mesh(mesh)    
    else:
        mesh = ob
        obm.from_mesh(mesh)
    return obm
项目:Nier2Blender    作者:C4nf3ng    | 项目源码 | 文件源码
def add_material_to_mesh(mesh, materials , uvs):
    for material in materials:
        print('linking material %s to mesh object %s' % (material.name, mesh.name))
        mesh.data.materials.append(material)
    bpy.context.scene.objects.active = mesh
    bpy.ops.object.mode_set(mode="EDIT")
    bm = bmesh.from_edit_mesh(mesh.data)
    uv_layer = bm.loops.layers.uv.verify()
    bm.faces.layers.tex.verify()
    for face in bm.faces:
        face.material_index = 0
        for l in face.loops:
            luv = l[uv_layer]
            ind = l.vert.index
            luv.uv = Vector(uvs[ind])
    bpy.ops.object.mode_set(mode='OBJECT')
    mesh.select = True
    bpy.ops.object.shade_smooth()
    #mesh.hide = True
    mesh.select = False
项目:Smooth-Normal    作者:dskjal    | 项目源码 | 文件源码
def index_callback(self, context):
    scn = context.scene
    if scn.ne_split_mode:
        o = context.active_object   
        bm = bmesh.from_edit_mesh(o.data)
        ensure_lookup_table(bm)  
        index = bm.select_history.active.index
        masked_vertices = get_masked_vertices(context)  

        loop_index = scn.ne_view_normal_index
        to_loops = create_loop_table(o.data)
        if loop_index < len(to_loops[index]):
            o.data.calc_normals_split()
            loop_index = to_loops[index][face_index]
            rotated = o.data.loops[loop_index].normal
            if scn.ne_view_sync_mode:
                mView = get_view_rotational_matrix(True)
                mObject = get_object_rotational_matrix()
                rotated = mView * mObject * rotated
            else:
                rotated = rot_vector(rotated, reverse=True)
            scn.ne_view_normal = rotated
项目:Smooth-Normal    作者:dskjal    | 项目源码 | 文件源码
def global_callback_handler(context):
    ob = bpy.context.active_object
    scn = bpy.context.scene
    if is_normal_active(ob):
        new_orientation = get_view_quaternion()
        if new_orientation==None:
            return

        if not is_same_vector(new_orientation, scn.ne_view_orientation):
            #update view orientation
            scn.ne_update_by_global_callback = True
            scn.ne_view_orientation = new_orientation

        #active vertex changed
        bm = bmesh.from_edit_mesh(ob.data)
        ensure_lookup_table(bm)  
        active = bm.select_history.active
        if active!=None:
            index = active.index
            if index != scn.ne_last_selected_vert_index:
                scn.ne_last_selected_vert_index = index
                scn.ne_update_by_global_callback = True
                scn.ne_type_normal = get_active_normal(bpy.context, ob)[0]
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def update(self, context):
        # update height via bmesh to avoid loosing material ids
        # this should be the rule for other simple objects
        # as long as there is no topologic changes
        o = context.active_object
        if archipack_wall.datablock(o) != self:
            return
        bpy.ops.object.mode_set(mode='EDIT')
        me = o.data
        bm = bmesh.from_edit_mesh(me)
        bm.verts.ensure_lookup_table()
        bm.faces.ensure_lookup_table()
        new_z = self.z
        last_z = list(v.co.z for v in bm.verts)
        max_z = max(last_z)
        for v in bm.verts:
            if v.co.z == max_z:
                v.co.z = new_z
        bmesh.update_edit_mesh(me, True)
        bpy.ops.object.mode_set(mode='OBJECT')
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def execute(self, context):
        ob = context.active_object
        me = ob.data
        bm = bmesh.from_edit_mesh(me)

        # Save current selection
        selected_verts = []
        for v in bm.verts:
            if v.select is True:
                selected_verts.append(v.index)
                if v.index != self.vert_and_group[0]:
                    v.select = False

        ob.vertex_groups.active_index = self.vert_and_group[1]
        bpy.ops.object.vertex_group_remove_from()

        # Re-select vertices
        for v in bm.verts:
            if v.index in selected_verts:
                v.select = True

        #XXX Hacky, but there's no other way to update the UI panels
        bpy.ops.object.editmode_toggle()
        bpy.ops.object.editmode_toggle()
        return {'FINISHED'}
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def avail_vgroups(self, context):
        if context is None:
            return []
        ob = context.active_object
        bm = bmesh.from_edit_mesh(ob.data)
        dvert_lay = bm.verts.layers.deform.active
        items = []
        self.vertex = bm.select_history.active.index

        dvert = bm.select_history.active[dvert_lay]

        #XXX since we need an identifier here, user won't be able to add a vgroup with that name ('-1')
        #XXX could check against vgroup names and find an unused name, but it's a rare case after all.
        items.append(("-1", "New Vertex Group", "Add a new vertex group to the active object", -1))

        for i in ob.vertex_groups:
            if i.index not in dvert.keys():
                items.append((i.name, i.name, str(i.index), i.index))

        return items
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def update(self, ed_type):
        tmp = []
        if ed_type == "OBJECT":
            if self.obj.active.type == 'MESH':
                self.active_obj = self.obj.active
            else:
                self.obj.active = self.obj[ self.msh_objs[0] ]
                self.active_obj = self.obj.active
            for i in self.msh_objs:
                if self.obj[i].select:
                    tmp.append(i)
            self.sel_msh_objs = tmp.copy()
            #print("self.sel_msh_objs", self.sel_msh_objs)  # debug

        elif ed_type == "EDIT_MESH":
            bm = bmesh.from_edit_mesh(bpy.context.edit_object.data)
            if hasattr(bm.verts, "ensure_lookup_table"):
                bm.verts.ensure_lookup_table()
            for ind in range(len(bm.verts)):
                if bm.verts[ind].select == True:
                    tmp.append(ind)
            self.sel_msh_vts = tmp.copy()
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def CreateWornEdges(context, factor):
    actobj = bpy.context.object

    bpy.ops.object.mode_set(mode="EDIT")
    bm = bmesh.from_edit_mesh(actobj.data)

    sf = [(vert.calc_shell_factor() - 1.0) * factor for vert in bm.verts[:]]

    bpy.ops.object.mode_set(mode="VERTEX_PAINT")
    purge = {}

    for ind, loop in enumerate(bpy.context.object.data.loops[:]):
        if loop.vertex_index not in purge:
            purge[loop.vertex_index] = [ind]
        else:
            purge[loop.vertex_index].append(ind)

    for vert in actobj.data.vertices[:]:
        if vert.select:
            ran = (sf[vert.index], sf[vert.index], sf[vert.index])
            for i in purge[vert.index]:
                actobj.data.vertex_colors.active.data[i].color = ran
    actobj.data.update()
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def main(context):
    obj = context.active_object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

    uv_layer = bm.loops.layers.uv.verify()
    bm.faces.layers.tex.verify()  # currently blender needs both layers.

    # adjust UVs
    for f in bm.faces:
        for l in f.loops:
            luv = l[uv_layer]
            if luv.select:
                # apply the location of the vertex as a UV
                luv.uv = l.vert.co.xy

    bmesh.update_edit_mesh(me)
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def execute(self, context):
        # must force edge selection mode here
        bpy.context.tool_settings.mesh_select_mode = (False, True, False)

        obj = context.active_object
        if obj.mode == "EDIT":
            bm = bmesh.from_edit_mesh(obj.data)

            selected_edges = [edge for edge in bm.edges if edge.select]
            edge_indices = [i.index for i in selected_edges]

            d = get_intersection_dictionary(bm, edge_indices)

            unselect_nonintersecting(bm, d.keys(), edge_indices)
            update_mesh(bm, d)

            bmesh.update_edit_mesh(obj.data)
        else:
            print('must be in edit mode')

        return {'FINISHED'}
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def add_vertex_to_intersection():

    obj = bpy.context.object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

    edges = [e for e in bm.edges if e.select]

    if len(edges) == 2:
        [[v1, v2], [v3, v4]] = [[v.co for v in e.verts] for e in edges]

        iv = geometry.intersect_line_line(v1, v2, v3, v4)
        if iv:
            iv = (iv[0] + iv[1]) / 2
            bm.verts.new(iv)

            bm.verts.ensure_lookup_table()

            bm.verts[-1].select = True
            bmesh.update_edit_mesh(me)
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def execute(self, context):
        obj = bpy.context.active_object
        self.me = obj.data
        self.bm = bmesh.from_edit_mesh(self.me)
        self.me.update()

        list_0 = [f.index for f in self.bm.faces if f.select]

        if len(list_0) == 0:
            self.report({'WARNING'},
                        "No suitable selection found. Operation cancelled")

            return {'CANCELLED'}

        elif len(list_0) != 0:
            solidify_split(self, list_0)
            context.tool_settings.mesh_select_mode = (True, True, True)
            if self.del_original:
                bpy.ops.mesh.delete(type='FACE')
            else:
                pass

        return {'FINISHED'}
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def __init__(self, spike_base_width=0.5, base_height_inset=0.0, top_spike=0.2, top_relative=False):

        obj = bpy.context.active_object
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.inset(
                use_boundary=True, use_even_offset=True, use_relative_offset=False,
                use_edge_rail=False, thickness=spike_base_width, depth=base_height_inset,
                use_outset=True, use_select_inset=False, use_individual=True, use_interpolate=True
                )
        bpy.ops.mesh.inset(
                use_boundary=True, use_even_offset=True, use_relative_offset=top_relative,
                use_edge_rail=False, thickness=0, depth=top_spike, use_outset=True,
                use_select_inset=False, use_individual=True, use_interpolate=True
                )

        bm = bmesh.from_edit_mesh(obj.data)
        bpy.ops.mesh.merge(type='COLLAPSE')
        bpy.ops.object.mode_set(mode='OBJECT')
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def __get_uv_info(self, context):
        """
        Get UV coordinate
        """
        obj = context.active_object
        uv_info = []
        bm = bmesh.from_edit_mesh(obj.data)
        if muv_common.check_version(2, 73, 0) >= 0:
            bm.faces.ensure_lookup_table()
        if not bm.loops.layers.uv:
            return None
        uv_layer = bm.loops.layers.uv.verify()
        for f in bm.faces:
            if f.select:
                for i, l in enumerate(f.loops):
                    uv_info.append((f.index, i, l[uv_layer].uv.copy()))
        if len(uv_info) == 0:
            return None
        return uv_info
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def execute(self, context):
        props = context.scene.muv_props.texlock
        obj = bpy.context.active_object
        bm = bmesh.from_edit_mesh(obj.data)
        if muv_common.check_version(2, 73, 0) >= 0:
            bm.verts.ensure_lookup_table()
            bm.edges.ensure_lookup_table()
            bm.faces.ensure_lookup_table()

        if not bm.loops.layers.uv:
            self.report(
                {'WARNING'}, "Object must have more than one UV map")
            return {'CANCELLED'}

        props.verts_orig = [
            {"vidx": v.index, "vco": v.co.copy(), "moved": False}
            for v in bm.verts if v.select]

        return {'FINISHED'}
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def execute(self, context):
        props = context.scene.muv_props.wsuv
        obj = bpy.context.active_object
        bm = bmesh.from_edit_mesh(obj.data)
        if muv_common.check_version(2, 73, 0) >= 0:
            bm.verts.ensure_lookup_table()
            bm.edges.ensure_lookup_table()
            bm.faces.ensure_lookup_table()

        if not bm.loops.layers.uv:
            self.report({'WARNING'}, "Object must have more than one UV map")
            return {'CANCELLED'}
        uv_layer = bm.loops.layers.uv.verify()

        sel_faces = [f for f in bm.faces if f.select]

        # measure average face size
        scale = 0.0
        for f in sel_faces:
            scale = scale + calc_face_scale(uv_layer, f)

        props.ref_scale = scale / len(sel_faces)

        return {'FINISHED'}
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def side(self, nombre, offset):

    bpy.ops.object.mode_set(mode="EDIT", toggle=0)
    OBJECT = bpy.context.active_object
    ODATA = bmesh.from_edit_mesh(OBJECT.data)
    bpy.context.tool_settings.mesh_select_mode = (True, False, False)
    for VERTICE in ODATA.verts[:]:
        VERTICE.select = False
    if nombre is False:
        for VERTICES in ODATA.verts[:]:
            if VERTICES.co[0] < (offset):
                VERTICES.select = 1
    else:
        for VERTICES in ODATA.verts[:]:
            if VERTICES.co[0] > (offset):
                VERTICES.select = 1
    ODATA.select_flush(False)
    bpy.ops.object.mode_set(mode="EDIT", toggle=0)
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def SelDoubles(self, context):
    bm = bmesh.from_edit_mesh(bpy.context.object.data)

    for v in bm.verts:
        v.select = 0

    dictloc = {}

    rd = lambda x: (round(x[0],4),round(x[1],4),round(x[2],4))

    for vert in bm.verts:
        dictloc.setdefault(rd(vert.co),[]).append(vert.index)

    for loc, ind in dictloc.items():
        if len(ind) > 1:
            for v in ind:
                bm.verts[v].select = 1

    bpy.context.scene.objects.active = bpy.context.scene.objects.active
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def initialise():
    global_undo = bpy.context.user_preferences.edit.use_global_undo
    bpy.context.user_preferences.edit.use_global_undo = False
    object = bpy.context.active_object
    if 'MIRROR' in [mod.type for mod in object.modifiers if mod.show_viewport]:
        # ensure that selection is synced for the derived mesh
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.mode_set(mode='EDIT')
    bm = bmesh.from_edit_mesh(object.data)

    bm.verts.ensure_lookup_table()
    bm.edges.ensure_lookup_table()
    bm.faces.ensure_lookup_table()

    return(global_undo, object, bm)


# move the vertices to their new locations
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def execute(self, context):
        import bmesh
        from .bmesh import find_adjacent

        obj = context.active_object
        me = obj.data
        bm = bmesh.from_edit_mesh(me)

        if find_adjacent.select_prev(bm, self.report):
            bm.select_flush_mode()
            bmesh.update_edit_mesh(me, False)

        return {'FINISHED'}


# XXX This is hackish (going forth and back from Object mode...), to be redone once we have proper support of
#     custom normals in BMesh/edit mode.
项目:BRM-UVTools    作者:leukbaars    | 项目源码 | 文件源码
def execute(self, context):
        mesh = bpy.context.object.data
        bm = bmesh.from_edit_mesh(mesh)
        bm.faces.ensure_lookup_table()
        uv_layer = bm.loops.layers.uv.active
        face0=[]

        #save active face
        for l in bm.faces.active.loops:
            face0.append(l[uv_layer].uv)

        #copy uvs to selected face
        for f in bm.faces:
            if f.select:
                if f is not bm.faces.active:
                    for i,l in enumerate(f.loops):
                        if i < len(face0):
                            l[uv_layer].uv=face0[i]
                        else:
                            #TODO: possibly interpolate uvs for better transfer
                            l[uv_layer].uv=face0[len(face0)-1]

        bmesh.update_edit_mesh(mesh, False, False)
        return {'FINISHED'}
项目:archipack    作者:s-leger    | 项目源码 | 文件源码
def update(self, context):
        # update height via bmesh to avoid loosing material ids
        # this should be the rule for other simple objects
        # as long as there is no topologic changes
        o = context.active_object
        if archipack_wall.datablock(o) != self:
            return
        bpy.ops.object.mode_set(mode='EDIT')
        me = o.data
        bm = bmesh.from_edit_mesh(me)
        bm.verts.ensure_lookup_table()
        bm.faces.ensure_lookup_table()
        new_z = self.z
        last_z = list(v.co.z for v in bm.verts)
        max_z = max(last_z)
        for v in bm.verts:
            if v.co.z == max_z:
                v.co.z = new_z
        bmesh.update_edit_mesh(me, True)
        bpy.ops.object.mode_set(mode='OBJECT')
项目:Shifter    作者:BlendingJake    | 项目源码 | 文件源码
def execute(self, context):
        ob = context.object

        if self.direction and ob is not None:
            verts = bmesh.from_edit_mesh(ob.data).verts

            cur_set = from_string(eval("ob.shifter_{}_verts".format(self.direction)))
            start_size = len(cur_set)

            for v in verts:
                if v.select and v.index not in cur_set:
                    cur_set.add(v.index)

            end_size = len(cur_set)
            exec("ob.shifter_{}_verts = to_string(list(cur_set))".format(self.direction))

            self.report({"INFO"}, "Shifter: Added {} Vertices".format(end_size - start_size))

        return {"FINISHED"}
项目:Shifter    作者:BlendingJake    | 项目源码 | 文件源码
def execute(self, context):
        ob = context.object

        if self.direction and ob is not None:
            verts = bmesh.from_edit_mesh(ob.data).verts
            indices = []
            for v in verts:
                if v.select:
                    indices.append(v.index)
            size = len(indices)

            if self.direction == "x":
                ob.shifter_x_verts = to_string(indices)
            elif self.direction == "y":
                ob.shifter_y_verts = to_string(indices)
            else:
                ob.shifter_z_verts = to_string(indices)

            self.report({"INFO"}, "Shifter: Set {} Vertices".format(size))

        return {"FINISHED"}
项目:Shifter    作者:BlendingJake    | 项目源码 | 文件源码
def execute(self, context):
        ob = context.object

        if self.direction and ob is not None:
            verts = bmesh.from_edit_mesh(ob.data).verts
            cur_verts = from_string(eval("ob.shifter_{}_verts".format(self.direction)))
            removed = 0

            for v in verts:
                if v.select and v.index in cur_verts:
                    cur_verts.remove(v.index)
                    removed += 1

            exec("ob.shifter_{}_verts = to_string(list(cur_verts))".format(self.direction))
            self.report({"INFO"}, "Shifter: Removed {} Vertices".format(removed))

        return {"FINISHED"}
项目:blender-addons    作者:scorpion81    | 项目源码 | 文件源码
def initialise():
    global_undo = bpy.context.user_preferences.edit.use_global_undo
    bpy.context.user_preferences.edit.use_global_undo = False
    object = bpy.context.active_object
    if 'MIRROR' in [mod.type for mod in object.modifiers if mod.show_viewport]:
        # ensure that selection is synced for the derived mesh
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.mode_set(mode='EDIT')
    bm = bmesh.from_edit_mesh(object.data)

    bm.verts.ensure_lookup_table()
    bm.edges.ensure_lookup_table()
    bm.faces.ensure_lookup_table()

    return(global_undo, object, bm)


# move the vertices to their new locations
项目:blender_addons_collection    作者:manorius    | 项目源码 | 文件源码
def get_mesh_vertices(myobj):
    try:
        if myobj.mode == 'EDIT':
            bm = bmesh.from_edit_mesh(myobj.data)
            obverts = bm.verts
        else:
            obverts = myobj.data.vertices

        return obverts
    except AttributeError:
        return None


# --------------------------------------------------------------------
# Get position for scale text
#
# --------------------------------------------------------------------
项目:BetterBlender    作者:bobtherobot    | 项目源码 | 文件源码
def get_selected_faces_by_sel_seq(obj):
    """
    get information about selected indices.
    @param  obj object
    @return information about selected faces (list of SelectedFaceInfo)
    """
    # get indices by selection sequence
    bm = bmesh.from_edit_mesh(obj.data)
    if check_version(2, 73, 0) >= 0:
        bm.faces.ensure_lookup_table()
    indices = [
        e.loops[0].face.index
        for e in bm.select_history
        if isinstance(e, bmesh.types.BMFace) and e.select]

    # get selected faces by selection sequence
    return get_faces_from_indices(obj, indices)
项目:BetterBlender    作者:bobtherobot    | 项目源码 | 文件源码
def display_color_callback():    
    if bpy.context.object.mode == 'EDIT':         
        if display_color[0]:              
            ob = bpy.context.object
            me = ob.data
            bm = bmesh.from_edit_mesh(me)

            quads = tris = ngons = 0
            ngons_to_tris = 0            
            verts = len(bm.verts)
            faces = len(bm.faces)

            for f in bm.faces:                
                v = len(f.verts)
                if v == 3: # tris
                    f.material_index = 2
                elif v == 4: # quads
                    f.material_index = 0    
                elif v > 4: # ngons 
                    f.material_index = 1

            bmesh.update_edit_mesh(me)
项目:BetterBlender    作者:bobtherobot    | 项目源码 | 文件源码
def noise_obj(obj, context, self):
    bm = bmesh.from_edit_mesh(obj.data)
    verts = [v for v in bm.verts if v.select]
    if not verts:
        verts = [v for v in bm.verts if v.hide is False]

    for vert in verts:
        noise_pos = vert.co.copy()
        noise_pos.x += self.offset_x
        noise_pos.z += self.offset_y
        noise_pos.z += self.offset_z

        noise_val = None
        if self.noise_type == 'Turbulence':
            noise_val = mathu.noise.turbulence(noise_pos, self.octaves, self.hard, mathu.noise.types.STDPERLIN, self.amplitude_scale, self.frequency_scale)
        elif self.noise_type == 'Fractal':
            noise_val = mathu.noise.fractal(noise_pos, self.amplitude_scale, self.frequency_scale, self.octaves, mathu.noise.types.STDPERLIN)
        else:
            noise_val = mathu.noise.hetero_terrain(noise_pos, self.amplitude_scale, self.frequency_scale, self.octaves, 0, mathu.noise.types.STDPERLIN)

        vert_offset = vert.normal.copy().normalized() * noise_val
        vert.co += vert_offset * self.intensity

    bm.normal_update()
    bmesh.update_edit_mesh(obj.data)
项目:JARCH-Vis    作者:BlendingJake    | 项目源码 | 文件源码
def random_uvs(self, context):
    import bpy
    import bmesh
    from mathutils import Vector
    from random import uniform
    bpy.ops.object.editmode_toggle()
    bpy.ops.mesh.select_all(action="SELECT")
    obj = bpy.context.object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

    uv_layer = bm.loops.layers.uv.verify()
    bm.faces.layers.tex.verify()
    # adjust UVs
    for f in bm.faces:
        offset = Vector((uniform(-1.0, 1.0), uniform(-1.0, 1.0)))
        for v in f.loops:
            luv = v[uv_layer]
            luv.uv = (luv.uv + offset).xy

    bmesh.update_edit_mesh(me)
    bpy.ops.object.editmode_toggle()
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def side (self, nombre, offset):

    bpy.ops.object.mode_set(mode="EDIT", toggle=0)
    OBJECT = bpy.context.active_object
    ODATA = bmesh.from_edit_mesh(OBJECT.data)
    MODE = bpy.context.mode
    bpy.context.tool_settings.mesh_select_mode = (True, False, False)
    for VERTICE in ODATA.verts[:]:
        VERTICE.select = False
    if nombre == False:
        for VERTICES in ODATA.verts[:]:
            if VERTICES.co[0] < (offset):
                VERTICES.select = 1
    else:
        for VERTICES in ODATA.verts[:]:
            if VERTICES.co[0] > (offset):
                VERTICES.select = 1
    ODATA.select_flush(False)
    bpy.ops.object.mode_set(mode="EDIT", toggle=0)
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def execute(self, context):
        active_obj = context.scene.objects.active
        active_mesh = active_obj.data
        select_and_change_mode(active_obj, 'EDIT')
        edit_obj = bpy.context.edit_object
        active_mesh = edit_obj.data
        uv_layer_holdr1 = active_mesh.uv_textures.get(kWPLSmoothHolderUVMap1)
        if uv_layer_holdr1 is None:
            active_mesh.uv_textures.new(kWPLSmoothHolderUVMap1)
        uv_layer_holdr2 = active_mesh.uv_textures.get(kWPLSmoothHolderUVMap2)
        if uv_layer_holdr2 is None:
            active_mesh.uv_textures.new(kWPLSmoothHolderUVMap2)
        bm = bmesh.from_edit_mesh(active_mesh)
        bm.verts.ensure_lookup_table()
        bm.faces.ensure_lookup_table()
        bm.verts.index_update()
        uv_layer_holdr1 = bm.loops.layers.uv.get(kWPLSmoothHolderUVMap1)
        uv_layer_holdr2 = bm.loops.layers.uv.get(kWPLSmoothHolderUVMap2)
        for face in bm.faces:
            for vert, loop in zip(face.verts, face.loops):
                loop[uv_layer_holdr1].uv = (vert.co[0],vert.co[1])
                loop[uv_layer_holdr2].uv = (vert.co[2],0)
        return {'FINISHED'}
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def remove_doubles(obj,edge_average_len,edge_min_len):
    bm = bmesh.from_edit_mesh(obj.data)
    verts = []
    for vert in bm.verts:
        if not vert.hide:
            verts.append(vert)
    bmesh.ops.remove_doubles(bm,verts=verts,dist=0.0001)
    bmesh.update_edit_mesh(obj.data)
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def reproject(self,context):
        ### unwrap
        obj = context.active_object
        hide_base_sprite = obj.data.coa_hide_base_sprite
        obj.data.coa_hide_base_sprite = False
        bm = bmesh.from_edit_mesh(obj.data)
        selected_edges = []
        for edge in bm.edges:
            if edge.select:
                selected_edges.append(edge)

        unselected_verts = []
        for vert in bm.verts:
            if not vert.select:
                unselected_verts.append(vert)
                vert.select = True
        unselected_faces = []
        for face in bm.faces:
            if not face.select:
                unselected_faces.append(face)
                face.select = True        
        bpy.ops.uv.project_from_view(camera_bounds=False, correct_aspect=True, scale_to_bounds=True)        

        for edge in selected_edges:
            edge.select = True
        for vert in unselected_verts:
            vert.select = False
        for face in unselected_faces:
            face.select = False
        bmesh.update_edit_mesh(obj.data)    
        obj.data.coa_hide_base_sprite = hide_base_sprite
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def normal_fill(self,context):
        obj = context.active_object

        bpy.ops.mesh.edge_face_add()
        bpy.ops.uv.project_from_view(camera_bounds=False, correct_aspect=True, scale_to_bounds=True)

        self.reset_spritesheet(context,obj)


        bm = bmesh.from_edit_mesh(obj.data)
        unselected_faces = []
        for face in bm.faces:
            if face.select == False:
                unselected_faces.append(face)
            face.select = True    


        bpy.ops.uv.project_from_view(camera_bounds=False, correct_aspect=True, scale_to_bounds=True)


        for face in unselected_faces:
            face.select = False

        bmesh.update_edit_mesh(obj.data)

        self.revert_rest_spritesheet(context,obj)
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def set_bone_shape_color_and_wireframe(self,context,obj):
        if self.bone.bone_group != None:
            bone_group_name = self.bone.bone_group.name
            bone_group_color = self.bone.bone_group.colors.normal
            suffix = "_group_color"
            if (bone_group_name+suffix) not in bpy.data.materials:
                material = bpy.data.materials.new(bone_group_name+suffix)
            else:
                material = bpy.data.materials[bone_group_name+suffix]

            material.diffuse_color = bone_group_color
            material.use_shadeless = True

            if len(obj.material_slots) == 0:
                obj.data.materials.append(material)
            else:
                obj.material_slots[0].material = material
        else:
            if len(obj.material_slots) > 0:
                obj.material_slots[0].material = None

        bm = bmesh.from_edit_mesh(obj.data)
        if len(bm.faces) > 0:
            self.armature.data.bones[self.bone.name].show_wire = False
        else:
            self.armature.data.bones[self.bone.name].show_wire = True
        bm.free()
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def get_selected_vert_pos(self,context):
        bm = bmesh.from_edit_mesh(context.active_object.data)
        for vert in bm.verts:
            if vert.select == True:
                bmesh.update_edit_mesh(context.active_object.data)
                return vert.co
        bmesh.update_edit_mesh(context.active_object.data)
        return None