I'm struggling with implementing a custom icons into script. As my level of Python is low, I did try others suggestions but without any luck. Addon never show up in Blender, but it's installed in blender addon folder.
Always tested with zip file, as zip file contain a script Selection.py and folder "icons" with init.py script inside.
I always get error - ImportError: No module named 'icons'.
I believe that I never register a init.py script and Selection.py script can't see it. ?
Scripts
Selection.py script
import bpy
import bpy.utils.previews
from icons import get_icon_id
from bpy.types import (
Panel,
Menu,
Operator,
)
from bpy.props import IntProperty, FloatProperty
bl_info = {
"name": "IPG",
"author": "InvisibleParrot",
"version": (0, 0, 1),
"blender": (2, 77, 0),
"location": "View3D > Edit Mode",
"description": "IPG tools of trade",
"warning": "",
"wiki_url": "",
"category": "IPG Tools"}
class SelectionPanel(bpy.types.Panel):
"""Selection"""
bl_label = 'Selection'
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = 'IPG Tools'
def draw(self, context):
layout = self.layout
col = layout.column(align = True)
col.operator("object.unwrapobject", text="Unwrap", icon_value=get_icon_id("C_Black"))
def register():
bpy.utils.register_class(ColorSelectionPanel)
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_class(ColorSelectionPanel)
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
and init.py script
import os
import bpy
icons_collection = None
icons_directory = os.path.dirname(__file__)
def get_icon_id(identifier):
# The initialize_icons_collection function needs to be called first.
return get_icon(identifier).icon_id
def get_icon(identifier):
if identifier in icons_collection:
return icons_collection[identifier]
return icons_collection.load(identifier, os.path.join(icons_directory, identifier + ".png"), "IMAGE")
def initialize_icons_collection():
global icons_collection
icons_collection = bpy.utils.previews.new()
def unload_icons():
bpy.utils.previews.remove(icons_collection)
class IconsMock:
def get(self, identifier):
return get_icon(identifier)
icons = IconsMock()
from . import icons
orfrom .icons import get_icon_id
orfrom your_package.icons import get_icon_id
or look at the 'importlib' especially import_module(...) which lets you use string names, and can take advantage of__package__
and__name__
$\endgroup$ – batFINGER Sep 4 '16 at 5:48