Page MenuHome

Python Template demo script error
Closed, ResolvedPublic


System Information
Operating system: Ubuntu 18.04.2 64bit
Graphics card: AMD RX 580 8GB

Blender Version
Broken: 2.80.55, a3b88c917299, 2019-04-10
Broken also in 2.79b

Short description of error
This error appears when running a demo template python script included in Blender

raise KeyError("key {name!r} already exists")

Exact steps for others to reproduce the error

  1. Open Blender
  2. Go in Scripting workspace
  3. Select from Templates > Python > UI Previews Dynamic Enum
  4. Run it, you will find the panel in the object properties window
  5. Now choose a directory for the icons to load
  6. Choose another directory
  7. And choose the directory you have chosen before (point 5)

The following error appears:

Traceback (most recent call last):
  File "/", line 54, in enum_previews_from_directory_items
  File "/home/marco/Scaricati/blender-2.80-a3b88c917299-linux-glibc224-x86_64/2.80/scripts/modules/bpy/utils/", line 97, in load
    raise KeyError("key {name!r} already exists")
KeyError: 'key {name!r} already exists'
File "/", line 26, in enum_previews_from_directory_items

Event Timeline

And i found a solution to the problem:
just add these lines of code after line 53 of the script

icon = pcoll.get(name)
if not icon:
    thumb = pcoll.load(name, filepath, 'IMAGE')
    thumb = pcoll[name]

So that it checks if the icons have already been imported.
This could be helpful to others who need to make a dynamic enum.

Philipp Oeser (lichtwerk) claimed this task.
Philipp Oeser (lichtwerk) triaged this task as Confirmed, Medium priority.

lgtm, will commit, thx for the fix!

Wow :D my first python fix! Thank you a lot, you are awesome!