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")
python类update_edit_mesh()的实例源码
def collapse_short_edges(bm,obj,threshold=1.0):
### collapse short edges
edges_len_average = 0
edges_count = 0
shortest_edge = 10000
for edge in bm.edges:
if True:
edges_count += 1
length = edge.calc_length()
edges_len_average += length
if length < shortest_edge:
shortest_edge = length
edges_len_average = edges_len_average/edges_count
verts = []
for vert in bm.verts:
if not vert.is_boundary:
verts.append(vert)
bmesh.update_edit_mesh(obj.data)
bmesh.ops.remove_doubles(bm,verts=verts,dist=edges_len_average*threshold)
bmesh.update_edit_mesh(obj.data)
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")
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")
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)
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)
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
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'}
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')
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)
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'}
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)
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'}
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')
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)
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)
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()
def execute(self, context):
obj = context.object
me = bmesh.from_edit_mesh(obj.data)
uv_layer = me.loops.layers.uv.active
clear = False
for f in me.faces:
for l in f.loops:
uv = l[uv_layer]
if uv.select and uv.pin_uv:
print(uv.uv, uv.pin_uv)
clear = True
break
if clear:
break
print("pining", clear)
bmesh.update_edit_mesh(obj.data)
bpy.ops.uv.pin(clear = clear)
return {'FINISHED'}
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 = self.frequency * 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)
def terminate(global_undo):
# update editmesh cached data
obj = bpy.context.active_object
if obj.mode == 'EDIT':
bmesh.update_edit_mesh(obj.data, tessface=True, destructive=True)
bpy.context.user_preferences.edit.use_global_undo = global_undo
##########################################
####### Relax functions ##################
##########################################
#Relax: create lists with knots and points, all correctly sorted
def clean_mesh(me):
debug_message("Cleaning mesh " + me.name)
h = bpy.context.scene.hyperpresets[me.hypersettings.preset]
if me.is_editmode:
bm = bmesh.from_edit_mesh(me)
else:
bm = bmesh.new()
bm.from_mesh(me)
layw = bm.verts.layers.float['hyperw']
layx = bm.verts.layers.float['hyperx']
layy = bm.verts.layers.float['hypery']
layz = bm.verts.layers.float['hyperz']
for v in bm.verts:
old = Vector([v[layw], v[layx], v[layy], v[layz]])
newco = map4to4(h, v.co, old)
v[layw] = newco[0]
v[layx] = newco[1]
v[layy] = newco[2]
v[layz] = newco[3]
if me.is_editmode:
bmesh.update_edit_mesh(me)
else:
bm.to_mesh(me)
def project_to_3d(me):
debug_message("Projecting " + me.name + " to 3D")
h = bpy.context.scene.hyperpresets[me.hypersettings.preset]
if me.is_editmode:
bm = bmesh.from_edit_mesh(me)
else:
bm = bmesh.new()
bm.from_mesh(me)
layw = bm.verts.layers.float['hyperw']
layx = bm.verts.layers.float['hyperx']
layy = bm.verts.layers.float['hypery']
layz = bm.verts.layers.float['hyperz']
for v in bm.verts:
p = Vector([v[layw], v[layx], v[layy], v[layz]])
newco = map4to3(h, p)
v.co = newco
if me.is_editmode:
bmesh.update_edit_mesh(me)
else:
bm.to_mesh(me)
me.update()
def execute(self, context):
obj_curr = context.active_object
mesh = obj_curr.data
bm = bmesh.from_edit_mesh(mesh)
normal_buffer = self.addon.preferences.normal_buffer
vertex_normal_weight_layer = bm.verts.layers.int['vertex-normal-weight']
vertex_normal_x_layer = bm.verts.layers.float['vertex-normal-x']
vertex_normal_y_layer = bm.verts.layers.float['vertex-normal-y']
vertex_normal_z_layer = bm.verts.layers.float['vertex-normal-z']
# Assign stored world space normal vector to all selected vertices.
vertex_normal = obj_curr.matrix_world.inverted() * normal_buffer
for v in [v for v in bm.verts if v.select]:
v[vertex_normal_weight_layer] = self.vertex_normal_weight_map['UNWEIGHTED']
v[vertex_normal_x_layer] = vertex_normal.x
v[vertex_normal_y_layer] = vertex_normal.y
v[vertex_normal_z_layer] = vertex_normal.z
# Update the mesh.
bpy.ops.mesh.yavne_update_vertex_normals()
bmesh.update_edit_mesh(mesh)
return {'FINISHED'}
def clean_boundary_edges(bm,obj):
edges_len_average, shortest_edge = get_average_edge_length(bm,obj)
edges = []
for edge in bm.edges:
if edge.calc_length() < edges_len_average*.12 and not edge.tag:
edges.append(edge)
bmesh.ops.collapse(bm,edges=edges,uvs=False)
bmesh.update_edit_mesh(obj.data)
def average_edge_cuts(bm,obj,cuts=1):
### collapse short edges
edges_len_average, shortest_edge = get_average_edge_length(bm,obj)
subdivide_edges = []
for edge in bm.edges:
cut_count = int(edge.calc_length()/shortest_edge)*cuts
if cut_count < 0:
cut_count = 0
if not edge.is_boundary:
subdivide_edges.append([edge,cut_count])
for edge in subdivide_edges:
bmesh.ops.subdivide_edges(bm,edges=[edge[0]],cuts=edge[1])
bmesh.update_edit_mesh(obj.data)
def triangle_fill(bm,obj):
edges = []
for edge in bm.edges:
if edge.select == True:
edges.append(edge)
triangle_fill = bmesh.ops.triangle_fill(bm,edges=edges,use_beauty=True)
bmesh.update_edit_mesh(obj.data)
if triangle_fill["geom"] == []:
return False
else:
return True
def smooth_verts(bm,obj):
### smooth verts
smooth_verts = []
for vert in bm.verts:
if not vert.is_boundary:
smooth_verts.append(vert)
for i in range(50):
#bmesh.ops.smooth_vert(bm,verts=smooth_verts,factor=1.0,use_axis_x=True,use_axis_y=True,use_axis_z=True)
bmesh.ops.smooth_vert(bm,verts=smooth_verts,factor=1.0,use_axis_x=True,use_axis_y=True,use_axis_z=True)
bmesh.update_edit_mesh(obj.data)
def clean_verts(bm,obj):
### find corrupted faces
faces = []
for face in bm.faces:
i = 0
for edge in face.edges:
if not edge.is_manifold:
i += 1
if i == len(face.edges):
faces.append(face)
bmesh.ops.delete(bm,geom=faces,context=5)
edges = []
for face in bm.faces:
i = 0
for vert in face.verts:
if not vert.is_manifold and not vert.is_boundary:
i+=1
if i == len(face.verts):
for edge in face.edges:
if edge not in edges:
edges.append(edge)
bmesh.ops.collapse(bm,edges=edges)
bmesh.update_edit_mesh(obj.data)
for vert in bm.verts:
if not vert.is_boundary:
vert.select = False
verts = []
for vert in bm.verts:
if len(vert.link_edges) in [3,4] and not vert.is_boundary:
verts.append(vert)
bmesh.ops.dissolve_verts(bm,verts=verts)
bmesh.update_edit_mesh(obj.data)
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)
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
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)