def check_verts(self,context,event):
verts = []
bm = bmesh.from_edit_mesh(context.active_object.data)
for vert in bm.verts:
if vert.select:
verts.append(vert)
for vert in verts:
vert.co = context.active_object.matrix_world.inverted() * self.limit_cursor_by_bounds(context,context.active_object.matrix_world * vert.co)
bmesh.update_edit_mesh(context.active_object.data)
python类update_edit_mesh()的实例源码
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
def delete_geometry(self,context,bm,position,single_vert=False):
obj = context.active_object
snapped_vert_coord , point_type , bm_ob = self.snap_to_edge_or_vert(position,get_bm_obj = True)
if point_type == "VERT":
vert = bm_ob
if not vert.is_boundary and not vert.is_wire:
bmesh.ops.dissolve_verts(bm,verts=[vert])#,use_face_split=True,use_boundary_tear=True)
else:
if not vert.hide:
if obj.matrix_world * vert.co == snapped_vert_coord:
shortest_edge = None
edge_length = 10000000000000
for edge in vert.link_edges:
if edge.calc_length() < edge_length:
shortest_edge = edge
edge_length = edge.calc_length()
merge_co = shortest_edge.other_vert(vert).co if shortest_edge != None else vert.co
verts = []
verts.append(vert)
if shortest_edge != None:
verts.append(shortest_edge.other_vert(vert))
if len(verts) > 1:
bmesh.ops.pointmerge(bm,verts = verts,merge_co = merge_co)
else:
bm.verts.remove(vert)
elif point_type == "EDGE":
edge = bm_ob
if not edge.is_boundary and not edge.is_wire:
bmesh.ops.dissolve_edges(bm,edges=[edge],use_verts=False)
else:
if len(edge.verts[0].link_edges) > 1 and len(edge.verts[1].link_edges) > 1:
bm.edges.remove(edge)
elif len(edge.verts[0].link_edges) > 1 and len(edge.verts[1].link_edges) <= 1:
bm.verts.remove(edge.verts[1])
elif len(edge.verts[0].link_edges) <= 1 and len(edge.verts[1].link_edges) > 1:
bm.verts.remove(edge.verts[0])
else:
bmesh.ops.delete(bm,geom=[edge.verts[0],edge.verts[1]],context=1)
bmesh.update_edit_mesh(obj.data)
def execute(self, context):
scene = context.scene
obj = context.active_object
bm = bmesh.from_edit_mesh(obj.data)
mult = 1
# if context.scene.coa_distance_constraint:
# mult = bpy.context.space_data.region_3d.view_distance*.05
for edge in bm.edges:
if edge.select:
#scene.coa_distance = (obj.matrix_world * (edge.verts[0].co - edge.verts[1].co)).magnitude/mult# edge.calc_length()/mult
scene.coa_distance = ((edge.verts[0].co - edge.verts[1].co)).magnitude/mult# edge.calc_length()/mult
bmesh.update_edit_mesh(obj.data)
return {"FINISHED"}
def update_uv_unwrap(context):
obj = context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
### pin uv boundary vertex
uv_layer = bm.loops.layers.uv.active
for vert in bm.verts:
uv_vert = get_uv_from_vert(uv_layer, vert)
if uv_vert != None:
pass
bmesh.update_edit_mesh(me)
def main3(self, context, chboxVert0, chboxVert1, chboxVert2, grados, chboxreloj):
# variable angulo dependiendo los grados ingresados
angle = grados
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
vertices = [v for v in bm.verts if (v.select==True and not v.hide)]
if len(vertices) != 3:
print("seleccione solo 2 vertices")
#return {'FINISHED'}
else:
v1,v2,v3 = [v for v in bm.verts if (v.select==True and not v.hide)]
if chboxreloj:
angle = -angle
if chboxVert0:
calcularangulos1(v1, v3, v2,angle)
if chboxVert1:
calcularangulos1(v2, v3, v1,angle)
if chboxVert2:
calcularangulos1(v3, v2, v1,angle)
bmesh.update_edit_mesh(me, True)
def main(self, context, chboxsepara, chboxune):
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
if chboxsepara:
### 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')
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
elif chboxune:
bpy.ops.mesh.select_all(action='SELECT')
bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.1)
bpy.ops.mesh.select_all(action='SELECT')
bmesh.update_edit_mesh(me, True)
def crear_ganchos(self,context):
for vertice_index in self._verticesInvolucrados:
bpy.ops.mesh.select_all(action='DESELECT')
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
if hasattr(bm.verts, "ensure_lookup_table"):
bm.verts.ensure_lookup_table()
# only if you need to:
# bm.edges.ensure_lookup_table()
# bm.faces.ensure_lookup_table()
bm.verts[vertice_index].select=True
bpy.ops.object.hook_add_newob()
bmesh.update_edit_mesh(me, True)
#SELECCIONAMOS LOS VERTICES ORIGINALES
bpy.ops.mesh.select_all(action='DESELECT')
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
if hasattr(bm.verts, "ensure_lookup_table"):
bm.verts.ensure_lookup_table()
# only if you need to:
# bm.edges.ensure_lookup_table()
# bm.faces.ensure_lookup_table()
for vertice_index in self._verticesSeleccionados:
bm.verts[vertice_index].select=True
bmesh.update_edit_mesh(me, True)
def main(self, context, chboxaxisx, chboxaxisy,chboxaxisz, distance):
distancia = distance
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
if chboxaxisx:
x = distancia
else:
x = 0.0
if chboxaxisy:
y = distancia
else:
y = 0.0
if chboxaxisz:
z = distancia
else:
z = 0.0
vertices = [v for v in bm.verts if (v.select and not v.hide)]
bmesh.ops.translate(
bm,
verts=vertices,
vec=(x, y, z))
### separar caras###
bmesh.update_edit_mesh(me, True)
#"Render"
bpy.ops.render.render()
def main(self, context, chboxhide, chboxshow):
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
try:
v1 = [v for v in bm.verts if (v.select == True and v.hide == False)]
e1 = [e for e in bm.edges if (e.select == True and e.hide == False)]
f1 = [f for f in bm.faces if (f.select == True and f.hide == False)]
if len(v1)>0 or len(e1) >0 or f1>0:
if chboxhide:
bpy.ops.mesh.select_all(action='INVERT')
bpy.ops.mesh.hide(unselected=False)
bpy.ops.mesh.select_all(action='SELECT')
elif chboxshow:
bpy.ops.mesh.reveal()
except:
if chboxshow:
bpy.ops.mesh.reveal()
bmesh.update_edit_mesh(me, True)
def corte(bm, v1, v2, v3):
mid_vec = v1.lerp(v2, 0.5)
plane_no = v1 - mid_vec
plane_co = mid_vec
dist = 0.0001
# hidden geometry will not be affected.
visible_geom = [g for g in bm.faces[:]
+ bm.verts[:] + bm.edges[:] if not g.hide]
try:
bmesh.ops.bisect_plane(
bm,
geom=visible_geom,
dist=dist,
plane_co=plane_co, plane_no=plane_no,
use_snap_center=False,
clear_outer=False,
clear_inner=False)
bmesh.update_edit_mesh(me, True)
except:
print("salto excepcion")
################################################################################
########## clase del Circumcentro ##########################################
################################################################################
def main(self, context):
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
edges = [e for e in bm.edges if (e.select and not e.hide)]
vertices = [v for v in bm.verts if (v.select and not v.hide)]
listavertices=[]
if len(edges) !=0:
print("bordes seleccionados: " + str(len(edges)))
for edge in edges:
listavertices.append(edge.verts[0])
listavertices.append(edge.verts[1])
edge.select=False
print(listavertices)
contador = len(listavertices)
print(contador)
while contador != 1:
v1 = listavertices[contador-1].co
v2 = listavertices[contador-2].co
v3 = (0,0,0)
print(v1,v2)
corte(bm, v1, v2, v3)
contador = contador - 1
elif len(edges) ==0 and len(vertices) == 2:
v1,v2 =[v.co for v in bm.verts if (v.select and not v.hide)]
v3 = (0,0,0)
print("seleccionados 2 vertices")
corte(bm, v1, v2, v3)
else:
print("seleccione minimo 1 borde o 2 vertices")
bmesh.update_edit_mesh(me, True)
def main(self, context, chboxVert0, chboxVert1, chboxVert2, cortes):
# variable angulo dependiendo los grados ingresados
secciones = cortes
direccion = 1
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
vertices = [v for v in bm.verts if (v.select==True and not v.hide)]
if len(vertices) != 3:
print("seleccione solo 3 vertices")
#return {'FINISHED'}
else:
vv1,vv2,vv3 = [v for v in bm.verts if (v.select==True and not v.hide)]
if chboxVert0:
calcularangulos(vv1, vv2, vv3,secciones,direccion)
if chboxVert1:
calcularangulos(vv2, vv3, vv1,secciones,direccion)
if chboxVert2:
calcularangulos(vv3, vv1, vv2,secciones,direccion)
bmesh.update_edit_mesh(me, True)
def main(self, context, chboxVert0, chboxVert1, chboxVert2):
obj = bpy.context.object
me = obj.data
bm = bmesh.from_edit_mesh(me)
vertices = [v for v in bm.verts if (v.select and not v.hide)]
if not len(vertices) == 3:
msg = "select ONLY 3 vertices"
self.report({"WARNING"}, msg)
return {'CANCELLED'}
v1,v2,v3 = [v for v in vertices]
if chboxVert0:
corte(bm, v1,v2,v3)
if chboxVert1:
corte(bm, v2,v1,v3)
if chboxVert2:
corte(bm, v3,v1,v2)
verticeje = v3.co
else:
print("Select 1 option")
bmesh.update_edit_mesh(me, True)
def wall_uv(me, bm):
for face in bm.faces:
face.select = face.material_index > 0
bmesh.update_edit_mesh(me, True)
bpy.ops.uv.cube_project(scale_to_bounds=False, correct_aspect=True)
for face in bm.faces:
face.select = face.material_index < 1
bmesh.update_edit_mesh(me, True)
bpy.ops.uv.smart_project(use_aspect=True, stretch_to_bounds=False)
def _end(bm, o):
"""
private, end bmesh editing of active object
"""
bm.normal_update()
bmesh.update_edit_mesh(o.data, True)
bpy.ops.object.mode_set(mode='OBJECT')
bm.free()
def generate_bmesh_repr(p1, v1, axis, num_verts):
'''
p1: center of circle (local coordinates)
v1: first vertex of circle in (local coordinates)
axis: orientation matrix
origin: obj.location
'''
props = bpy.context.scene.tinycad_props
rescale = props.rescale
# generate geometry up front
chain = []
gamma = 2 * math.pi / num_verts
for i in range(num_verts + 1):
theta = gamma * i
mat_rot = mathutils.Matrix.Rotation(theta, 4, axis)
local_point = (mat_rot * ((v1 - p1) * rescale))
chain.append(local_point + p1)
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
# add verts
v_refs = []
for p in chain:
v = bm.verts.new(p)
v.select = False # this might be a default.. redundant?
v_refs.append(v)
# join verts, daisy chain
num_verts = len(v_refs)
for i in range(num_verts):
idx1 = i
idx2 = (i + 1) % num_verts
bm.edges.new([v_refs[idx1], v_refs[idx2]])
bmesh.update_edit_mesh(me, True)
def extend_vertex(self):
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
verts = bm.verts
faces = bm.faces
planes = [f for f in faces if f.select]
if (len(planes) > 1) or (len(planes) == 0):
failure_message(self)
return
plane = planes[0]
plane_vert_indices = [v for v in plane.verts[:]]
all_selected_vert_indices = [v for v in verts if v.select]
M = set(plane_vert_indices)
N = set(all_selected_vert_indices)
O = N.difference(M)
O = list(O)
if not len(O) == 2:
failure_message(self)
return
(v1_ref, v1), (v2_ref, v2) = [(i, i.co) for i in O]
plane_co = plane.calc_center_median()
plane_no = plane.normal
new_co = intersect_line_plane(v1, v2, plane_co, plane_no, False)
new_vertex = verts.new(new_co)
A_len = (v1 - new_co).length
B_len = (v2 - new_co).length
vertex_reference = v1_ref if (A_len < B_len) else v2_ref
bm.edges.new([vertex_reference, new_vertex])
bmesh.update_edit_mesh(me, True)
def execute(self, context):
# final attempt to enter unfragmented bm/mesh
# ghastly, but what can I do? it works with these
# fails without.
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
obj = context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
edges = [e for e in bm.edges if e.select and not e.hide]
if len(edges) == 2:
message = do_vtx_if_appropriate(bm, edges)
if isinstance(message, set):
msg = messages.get(message.pop())
return self.cancel_message(msg)
bm = message
else:
return self.cancel_message('select two edges!')
bm.verts.index_update()
bm.edges.index_update()
bmesh.update_edit_mesh(me, True)
return {'FINISHED'}
def bmesh_to_object(obj, bm):
"""
Object/Edit Mode update the object.
"""
me = obj.data
is_editmode = (obj.mode == 'EDIT')
if is_editmode:
bmesh.update_edit_mesh(me, True)
else:
bm.to_mesh(me)
# grr... cause an update
if me.vertices:
me.vertices[0].co[0] = me.vertices[0].co[0]