1
$\begingroup$

I am having trouble getting Merge by Distance to work in a python script while it works fine manually in the UI.

When I run Mesh > Clean Up > Merge by Distance on a selection of vertices, the vertices are successfully merged.

manual merge

The same selection of vertices is not merged when I have them selected and run

bpy.ops.mesh.remove_doubles(threshold=0.001, use_unselected=False)
Info: Removed 0 vertice(s)
{'FINISHED'}

bpy remove_double

Is there another step needed to script remove_doubles successfully?

My selection script below selects only the inner top layer of vertices that I want to merge but this issue happens if I just select any group of vertices in the UI so I don't think my selection is the issue.

import bpy

ob=bpy.context.active_object
verts=ob.data.vertices
for v in verts:
   if v.co[2] > 0:
      v.select=False
   else:
      v.select=True

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.mesh.select_more()
bpy.ops.mesh.select_all(action='INVERT')

bpy.ops.mesh.remove_doubles(threshold=0.001, use_unselected=False)
$\endgroup$

1 Answer 1

2
$\begingroup$

Your problem is caused by altered unit scale, which apparently you have to use to recalculate the distances on your own:

desired_threshold = 0.001

import bpy
from bpy import context as C

for v in C.object.data.vertices:
    v.select = v.co.z <= 0

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="VERT")
bpy.ops.mesh.select_more()
bpy.ops.mesh.select_all(action='INVERT')

recalculated_threshold = desired_threshold / C.scene.unit_settings.scale_length
bpy.ops.mesh.remove_doubles(threshold=recalculated_threshold, use_unselected=False)
$\endgroup$
2
  • $\begingroup$ Thank you, that did the trick. What is the difference between using object and active_object to access the object? I noticed that I need to select the object (so it is highlighted in orange) before running the script to get the vertices to select even though object and active_object both return the same last selected but now deselected object. $\endgroup$
    – ansonl
    Commented Nov 17 at 20:32
  • 1
    $\begingroup$ @ansonl you can compare active_object to object (same code) $\endgroup$ Commented Nov 17 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .