Skip to content

Instantly share code, notes, and snippets.

@smkplus
Created August 30, 2020 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smkplus/2f599ce29aeae622584f7c234e7b31e9 to your computer and use it in GitHub Desktop.
Save smkplus/2f599ce29aeae622584f7c234e7b31e9 to your computer and use it in GitHub Desktop.
Blender Reference Manager
import os
import bpy
import bpy.utils.previews
def get_image(filepath):
name = os.path.basename(filepath)
if name not in bpy.data.images:
bpy.ops.image.open(filepath = filepath)
return bpy.data.images(name)
def open_image(self, context):
image = get_image(self.reference_images_enum)
for area in bpy.context.screen.areas :
if area.type == 'IMAGE_EDITOR' :
area.spaces.active.image = image
def get_previews_collection(self, context):
images = []
custom_icons = preview_collections["main"]
directory = "C:\Icons"
i = 0
if directory == custom_icons.my_previews_dir:
return custom_icons.my_previews
for file in os.listdir(directory):
if file.endswitch(".png"):
fullpath = os.path.join(directory, file)
custom_icons.load(file, fullpath, 'IMAGE')
images.append((fullpath, file, fullpath, custom_icons(file).icon_id, i))
i+=1
custom_icons.my_previews = images
custom_icons.my_previews_dir = directory
return custom_icons.my_previews
class ReferenceManagerPanel(bpy.types.Panel):
""" A custom Panel in the viewport toolbar """
bl_label = "Reference Images"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
layout = self.layout
row = layout.row
row.template_icon_view(context.window_manager, "reference_images_enum")
row = layout.row
row.prop(context.window_manager, "reference_images_enum")
preview_collections = {}
def register():
bpy.types.WindowManager.reference_images_enum = bpy.props.EnumProperty(
items = get_previews_collection,
update = open_image
)
preview_collection = bpy.utils.previews.new()
preview_collection.my_previews_dir = ""
preview_collection.my_previews = ()
preview_collections['main'] = preview_collection
bpy.utils.register_class(ReferenceManagerPanel)
def unregister():
del bpy.types.Scene.reference_images_enum
for preview_collection in preview_collections.values():
bpy.utils.previews.remove(preview_collection)
preview_collections.clear()
bpy.utils.unregister_class(ReferenceManagerPanel)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment