Problem looping through mesh objects and joining

I am working on a script to do the following on a mesh imported from an .x3d file:

Delete an unwanted viewpoint

Loop through all of the mesh objects in the scene and: turn on auto smooth, add an edge split modifier, add a subsurf modifier, smooth

Loop though all of the mesh objects in the scene and: select all of the other objects with same rgb diffuse colour, link them all by material, join them into a single mesh.

Adding the modifiers and smoothing works fine, but it crashes when I try to do the joining part. The part of the script to select all of the objects with same rgb diffuse colour works fine in isolation.

I only started python/blender scripting a few days ago and this is my first proper script, so apologies if it is a bit rough and unrefined.


import bpy

process mesh for every object

def smooth():
for ob in obj:
if ob.type == ‘MESH’: = ob

		# turn autosmooth on = True

		# edgesplit modifier
		bpy.ops.object.modifier_add(type = 'EDGE_SPLIT')

		# subsurf modifier
		bpy.ops.object.modifier_add(type = 'SUBSURF')

		# smooth
		for pol in
			pol.use_smooth = True

select objects by diffuse colour

def colourSelect():
col =[0].diffuse_color
mess = [o for o in bpy.context.scene.objects if o.type == ‘MESH’]

for obj in mess:
	list = []
	for mat in
		try: list.append(mat.diffuse_color)
		except: pass = col in list

link by material and join mesh

def join():
# link by material
bpy.ops.object.make_links_data(type = ‘MATERIAL’)

#link by material

deselect all objects

bpy.ops.object.select_all(action = “DESELECT”)

delete Viewpoint



for ob in obj:
if ob.type == ‘MESH’: = ob

Any help would be much appreciated.