Python - Removing Duplicate Values from a Dictionary

So I had this Maya script that created loads of identical meshes, often in duplicate positions and I needed a clean up script to identify those meshes that were coincident. I could have iterated over a list of objects and compared their positions and filtered them accordingly, however, I thought the immutable nature of a python dictionary could be used instead.

Here I'm using a tuple (made from the object's XYZ position) as a key, and the object's name as a value. The dictionary doesn't permit duplicate keys, so we filter out all duplicate objects. The dictionary is then returned as a list of values (object names). Simple.

import maya.cmds as mc

def getUniqueObjects(selection):

	d = {}

	for mesh in selection:

		pos = tuple(mc.xform(mesh, query=True, translation=True))
		d[pos] = mesh

	return d.values()

No comments:

Post a Comment