The eventual purpose of this script will be to try to stitch two halves of two different, sliced-open objects, together. This is to be done by individually moving each of the verts on the open non-manifold sliced edge of one of the objects, to the nearest point on the non-manifold edge on the other object.
For now, I am just trying to find & print the index of the closest edge on object B, for each non-manifold vertex on object A. Later I'll add more code for the moving of the verts on object A.
I'm a bit stuck as I'm new to python in Blender.
I'm getting the following error for line 44: AttributeError: 'BMVert' object has no attribute 'location'
Code:
import bpy, bmesh
from mathutils import Vector
from mathutils.bvhtree import BVHTree
o = bpy.context.scene.objects
# Get the object and its mesh
obj = o["Cube"]
obj1 = o["Cube.001"]
mesh = obj.data
mesh1 = obj1.data
# Set up the selection of non-manifold verts on obj1
# Select object 1
obj1.select_set(True)
bpy.ops.object.editmode_toggle()
#me1 = obj1.data
# Create bmesh from edit mesh data (obj1)
bm1 = bmesh.from_edit_mesh(mesh1)
# Select non-manifold vertices in obj1
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_non_manifold()
# Loop through the selection of non-manifold vertices on obj1
for v in bm1.verts:
if v.select:
# For each non-manifold vertex on obj1, find the closest edge on the other object ('obj')
# Get vertices locations
vertices = [v.co for v in mesh.vertices]
# Construct polygons from edges
polygons = [(e.vertices[0], e.vertices[1], e.vertices[0]) for e in mesh.edges]
# Create a BVH Tree from it
tree = BVHTree.FromPolygons( vertices, polygons, all_triangles = True )
# Search for a location
location = v.location
# Query the tree
found_location, normal, index, distance = tree.find_nearest( location )
print( index )
Would be grateful for any help. Apologies if I've done anything extremely stupid here, am feeling my way bit by bit.
BVHTree.FromBMesh(bm)
. Have you used AI to create this script ? If so you should disclose it in your question as per the rules of this site. Cheers $\endgroup$polygons = [(e.vertices[0], e.vertices[1], e.vertices[0]) for e in mesh.edges]
will create zero-area triangles if I'm not mistaken ? I'm really struggling to understand the logic :) $\endgroup$found_location_world = obj.matrix_world @ found_location
line is throwing a compile error 'TypeError: Matrix multiplication: not supported between 'Matrix' and 'NoneType' types`. This seems to be because I've now converted 'obj' to a bmesh... not sure how to get around this. edit: to answer your question no I haven't used AI, I've been building this using bits from various different threads here on SE. $\endgroup$