I've been trying to make an addon in Blender 3D, and have basically started out making a custom button that's going to generate a mesh. I want a custom icon for the mesh I'm going to generate, and I can get it to work as a script, but not as an addon. So when I click 'run script' a button shows up with my icon (which is in a folder called 'icons')
I've seen one code template that looks promising for an addon, but I can't get my icon to load when I try it:https://developer.blender.org/diffusion/B/browse/master/release/scripts/templates_py/ui_previews_custom_icon.py
If anyone has any examples of working addon code (like an example addon with a custom button with a custom icon) it would be very helpful.
I've already tried using one solution I found on this site, but it only works as a script, and not as an addon. Here's the code that works as a script:
#----------------------------------------------------------
# File hello.py
#----------------------------------------------------------
import bpy
#
# Menu in tools region
#
class ToolsPanel(bpy.types.Panel):
bl_label = "MyAddon"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
self.layout.operator("hello.hello", icon_value=custom_icons["custom_icon"].icon_id)
global custom_icons
#
class OBJECT_OT_HelloButton(bpy.types.Operator):
bl_idname = "hello.hello"
bl_label = "Generate MyMesh"
bl_icon= "custom_icons[custom_icon].icon_id"
country = bpy.props.StringProperty()
def execute(self, context):
if self.country == '':
print("Hello world!")
else:
print("Hello world from %s!" % self.country)
return{'FINISHED'}
#
# Registration
# All panels and operators must be registered with Blender; otherwise
# they do not show up. The simplest way to register everything in the
# file is with a call to bpy.utils.register_module(__name__).
#
bpy.utils.register_module(__name__)
import os
import bpy
import bpy.utils.previews
class Panel(bpy.types.Panel):
"""Creates a Panel in the 3D view Tools panel"""
bl_label = "Custom Icon Preview Panel"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
def draw(self, context):
global custom_icons
# global variable to store icons in
custom_icons = None
def register():
global custom_icons
custom_icons = bpy.utils.previews.new()
script_path = bpy.context.space_data.text.filepath
icons_dir = os.path.join(os.path.dirname(script_path), "icons")
custom_icons.load("custom_icon", os.path.join(icons_dir, "icon.png"), 'IMAGE')
bpy.utils.register_module(__name__)
def unregister():
global custom_icons
bpy.utils.previews.remove(custom_icons)
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()

__file__– zeffii Jan 14 '16 at 14:18