# How to merge all vertices by distance and remove all inner faces?

I have a bunch of 3D objects adjacent one to another (they all have a coincident face). I want to merge them all without having inner faces. I can achieve this by doing the following:

1. Object Mode an select all objects, then I join them with Ctrl+J.
2. Edit Mode -> Select -> All -> Vertex -> Merge Vertices -> By Distance
3. Select All by Trait -> Interior Faces -> then I delete all of them

That way I end up with a single object with no inner faces.

I would like to do the same exact thing but through a Python Script. I have thought about a couple of options which I do not know how to apply:

• Emulate the process mentioned before with python commands
• Doing boolean operations over and over (there are more than 100 objects), but for some reason the result is very messy and a bit slower

Any help will be highly appreciated, thank you very much in advance! :)

• As of Blender 2.8, you can enter Edit Mode for all objects at once (without joining them) so... Do you need a script still? If so, have a look into Remove Doubles on multiple objects. Apr 14 '20 at 20:24

First of all if you want to write your own basic scripts you should go to Edit -> Preferences -> Interface and enable "Python Tooltips".

After that you will see pop-up hints with python references for certain buttons, menus and values commands every time you place a mouse cursor over them. Also you can press Ctrl+C (or Cmd+C on mac) when your cursor is over the button and it will copy the python command for that button to the buffer, so you can paste it in your script.

Also you can open up the Info Window in Blender, many commands appear there right after executing and you can copy them from there. Also you can open Console Window in Blender and start typing in commands you know - and use an autocomplete option (shortcuts may be different but you can easily find it).

A little bit of theory before the script. Note that in most cases copying python commands from the buttons will give you a Blender operator starting with bpy.ops. It is not prohibited to use those operators in the scripts, but it is not very stable and may cause errors because all Blender operators rely on context (exact window, screen, area and region from which it is called). There are hints to override a context, here for example. But generally the more efficient way is to use direct access to blender data and change it directly without using the operators. Unfortunately direct editing of the mesh data with Blender API is pretty... let's call it strange and unpredictable (in my opinion). There is bmesh module which has been written specially for this purpose, but I'd say it is pretty complex especially if you are a beginner not recommended. Anyway, if you are not writing a very complex program, using bpy.ops is ok.

According to your algorithm a script may look like this:

import bpy

merge_threshold = 0.05 # here you can define Merge By Distance threshold

if bpy.context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='SELECT')
# This is to make sure that there is an active object in the scene:
#----------------------------------------------
if not bpy.context.active_object.hide_viewport:
for object in bpy.context.scene.objects:
if not object.hide_viewport:
bpy.context.view_layer.objects.active = object
break
#----------------------------------------------
bpy.ops.object.join()
bpy.ops.object.convert(target='MESH') # Here I've added an option which will
# apply all modifiers by converting object
# to mesh
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.remove_doubles(threshold = merge_threshold)
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_mode(type = 'FACE')
bpy.ops.mesh.select_interior_faces()
bpy.ops.mesh.delete(type='FACE')
bpy.ops.object.mode_set(mode='OBJECT')


UPD. Info Window and Python Console Window can be opened as any other window in Blender: Toggle System Console in Windows opens up a system console where you can find detailed information about errors, results of your print() functions, etc.

• Thank you very much! This not only did solve my problem but it also gave me such valuable information. I do appreciate a lot the well explained answer and the crazy short time you took to answer it since I posted it. I have been playing with the "Python Tooltips", sometimes I get some errors, I guess it is because the context. I have not been able to find the Info and Console Windows. If I go to "Window -> Toggle Window Console" I can not input commands there. On the lower left corner of the Scripting window I can see the python commands appearing, maybe this already is the Info Window. Apr 17 '20 at 18:48