Python Template demo script error #63504

Closed
opened 2019-04-11 22:27:14 +02:00 by Marco · 10 comments
Member

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")

Screenshot.jpg

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
**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") ``` ![Screenshot.jpg](https://archive.blender.org/developer/F6932169/Screenshot.jpg) **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 ```
Author
Member

Added subscriber: @nacioss

Added subscriber: @nacioss
Member

Added subscriber: @Jeroen-Bakker

Added subscriber: @Jeroen-Bakker
Author
Member

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]

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

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] ``` ![Screenshot.jpg](https://archive.blender.org/developer/F6934674/Screenshot.jpg) So that it checks if the icons have already been imported. This could be helpful to others who need to make a dynamic enum.

This issue was referenced by blender/blender@f3fc8271b8

This issue was referenced by blender/blender@f3fc8271b81f7c23ba42356a95bbe487e1e339ae
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Philipp Oeser self-assigned this 2019-04-12 14:07:36 +02:00
Member

lgtm, will commit, thx for the fix!

lgtm, will commit, thx for the fix!
Member

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Author
Member

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

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

Added subscriber: @AlfonsoAnnarumma

Added subscriber: @AlfonsoAnnarumma

You can do a

pcoll.clear()

After the if that control the chance of directory.
If you change directory do a clear of pcoll.

You can do a ``` pcoll.clear() ``` After the if that control the chance of directory. If you change directory do a clear of pcoll.
Sign in to join this conversation.
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#63504
No description provided.