Hi, I’m new to Blenders bpy but I am familiar with Python.
Once I have selected a mesh and saved a list of its vertices, I can select a single one from the list and process it. The way I want to process it requires me to get its position (local or global) and a list of connected vertices, how can I do that?
import bmesh
mesh = bpy.context.view_layer.objects.active.data # object must be a mesh and in EditMode
bm = bmesh.from_edit_mesh(mesh)
for vert in bm.verts:
vl=[]
for l in vert.link_edges:
vl.append(l.other_vert(vert).index)
print("Vertex[", vert.index, "]:")
print("\tco: ", vert.co)
print("\tlinks: ", vl)
Why this not working for me? No any print… exist typical box, mesh.
import bpy
import bmesh
# Function to print vertex positions for each object
def print_vertex_positions():
# Table header
print("No.; Name; Location x; Location y; Location z;")
# Variable to count vertices
count = 1
# Iterate through all objects in the scene
for obj in bpy.data.objects:
print("Object:", obj.name, "Type:", obj.type)
# Check if the object is of type 'MESH'
if obj.type == 'MESH':
print("Processing mesh object:", obj.name)
# Access the mesh data
mesh = obj.data
print("Mesh data:", mesh)
# Create a new bmesh from the mesh data
bm = bmesh.new()
bm.from_mesh(mesh)
print("BMesh created")
# Iterate through the vertices in the bmesh
for vert in bm.verts:
# Get the global vertex coordinates
global_co = obj.matrix_world @ vert.co
# Print the vertex position in table format
print(f"{count}; {obj.name}; {global_co.x:.2f}; {global_co.y:.2f}; {global_co.z:.2f};")
count += 1
# Free the bmesh data
bm.free()
# Call the function to print vertex positions
print_vertex_positions()
In some case of experiments I have: Python: Traceback (most recent call last): File "\Text.001", line 6, in <module> ValueError: The mesh must be in editmode
I believe that is possible to get for each object verts without option that manual must be in edit mode.