Page MenuHome

Python Template demo script error
Closed, ResolvedPublic

Description

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 ui_previews_dynamic_enum.py 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 "/ui_previews_dynamic_enum.py", 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/previews.py", line 97, in load
    raise KeyError("key {name!r} already exists")
KeyError: 'key {name!r} already exists'
File "/ui_previews_dynamic_enum.py", 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')
else:
    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!