-1

I am porting my Maya tool from python2 to python3 and ran into an issue where I have been using set() to remove duplicate entries in a list. Previously this worked great, but now I am getting:

TypeError: unhashable type: 'MeshUV'

The lists contain pymel MeshUV objects and might look like this:

[MeshUV('Right_Hand.map[1011]'), MeshUV('Right_Hand.map[1012]'),
 MeshUV('Right_Hand.map[1013]'), MeshUV('Right_Hand.map[1014]')]

Klaus D asked for minimal code to get this error, so use this in Maya with some uvs selected.

import pymel.core as pm
uvs = pm.ls(sl=True, fl=True)
set(uvs)

I don't know why this is suddenly a problem, but maybe there's any fast alternative to using set to remove duplicates or if I can alter the lists somehow to make them work again?

Edit: So after martineau's response I am thinking it's a change in Maya 2022 and rather than python3 per se, even though this is the version of Maya when they made the switch.

optagon
  • 11
  • 4
  • related: https://stackoverflow.com/questions/10784390/python-eliminate-duplicates-of-list-with-unhashable-elements-in-one-line – timgeb Jan 04 '22 at 09:21
  • Please create a minimal reproducible example and show us the full error traceback! – Klaus D. Jan 04 '22 at 09:21
  • `MeshUV` apparently isn't [hashable](https://docs.python.org/3/glossary.html#term-hashable). – deceze Jan 04 '22 at 09:23
  • Nothing changed between Python 2 & 3 that would have affected this, i.e. it wouldn't have worked in Python 2 either — something else is the problem. – martineau Jan 04 '22 at 10:57
  • Details would also help: you say it works in Python 2, but under the same version of Maya , or another version of Maya? Try running the same Maya with -pythonver 2 command line arguments and see if it works. – Klaudikus Jan 24 '22 at 12:20

1 Answers1

0

Python requires items in a set or dictionary to be hashable. If you're not using a custom type in which you can define __eq__ and __hash__ functions, you're better off using a different method of removing duplicates:

new_list = list()
for item in old_list:
    if item not in new_list:
        new_list.append(item)
Adid
  • 1,504
  • 3
  • 13