0

According to this question and this example I am trying to add some custom icons to my addon's operators. But I have an "issue".

In both examples, functions draw, register and unregister are in the same file. In my case, register and unregister functions located into my __init__.py file and looks totally different and the draw function located into an other file I have create by the name fu3dm_main.py. Can anyone help me on how I'll apply this example in my case?

Here is how my __init__.py file looks:

bl_info = {
    "name" : "Blah blah blah",
    "author" : "Simonetos The Greek <simonetos.the.greek@gmail.com>",
    "description" : "Blah blah blah",
    "blender" : (2, 90, 1),
    "version" : (1, 0, 0),
    "location" : "View3D",
    "warning" : "",
    "category" : "Generic"
}

import bpy from . fu3dm import * from . fu3dm_main import *

ops = fu3dm.ops

classes = ( fu3dm.ops.view.front, fu3dm.ops.view.back, fu3dm.ops.view.right, fu3dm.ops.view.left, fu3dm.ops.view.top, fu3dm.ops.view.bottom, fu3dm.ops.preferences.model, fu3dm_main.panel )

register, unregister = bpy.utils.register_classes_factory(classes)

And here is how my fu3dm_main.py file looks:

import bpy
from . fu3dm import *

class fu3dm_main(): """Main class"""

class panel(bpy.types.Panel):
    &quot;&quot;&quot;Main panel class&quot;&quot;&quot;

    bl_idname = &quot;fu3dm.main_PT_Panel&quot;
    bl_label = &quot;Blah blah blah&quot;
    bl_space_type = &quot;VIEW_3D&quot;
    bl_region_type = &quot;UI&quot;
    bl_category = &quot;FU3DM&quot;

    @classmethod
    def poll(cls, context):
        return bpy.data.collections.get(&quot;FU3DM v20.0&quot;)

    def draw(self, context):
            layout = self.layout
        #   ┌────────┐
        #   │ Box #1 │
        #   └┬───────┘
            box1 = layout.box()
            box1_col1 = box1.column()

            box1_row1 = box1_col1.split()
            box1_row1.label(text=&quot;Preferences:&quot;)
            box1_row1.operator(&quot;fu3dm.model_prefs&quot;)
        #   ┌┴────┐
        #   │ End │
        #   └─────┘

Simos Sigma
  • 413
  • 3
  • 11
  • 2
    Personally I go for methods outlined here https://blender.stackexchange.com/questions/158775/having-trouble-creating-an-addon-with-multiple-modules and again https://blender.stackexchange.com/a/183817/15543 in which case can edit a module without need to edit module init. Please also see https://docs.blender.org/api/current/info_best_practice.html re class names. Why the double up module and class names fu3dm_main.fu3dm_main? Lastly using from foo import * is rarely recommended. – batFINGER Oct 13 '20 at 11:36
  • 1
    Possible duplicate https://blender.stackexchange.com/questions/41565/loading-icons-into-custom-addon – batFINGER Oct 13 '20 at 11:42
  • @batFINGER Thank you very much for these useful links, I have to study them for sure!!! The from foo import * it's just temporary, I'll fix it later. – Simos Sigma Oct 13 '20 at 11:44

1 Answers1

0

Well I found the solution from this attached file here, and here is a code example...

import os

import bpy import bpy.utils.previews from bpy.types import Panel

preview_collections = {}

If you work with a simple script.

dir = os.path.dirname(bpy.data.filepath)

If you work with an addon.

dir = os.path.join(os.path.dirname(file), "icons")

pcoll = bpy.utils.previews.new()

for entry in os.scandir(dir): if entry.name.endswith(".png"): name = os.path.splitext(entry.name)[0] pcoll.load(name.upper(), entry.path, "IMAGE")

class VIEW3D_PT_test(Panel): bl_label = "TEST" bl_category = "TEST" bl_space_type = "VIEW_3D" bl_region_type = "UI"

def draw(self, context):
    layout = self.layout
    scene = context.scene

    layout.prop(scene, &quot;use_gravity&quot;, text=&quot;Enable Layout&quot;)

    col = layout.column()
    col.enabled = scene.use_gravity
    col.operator(&quot;object.duplicate_move&quot;, text=&quot;Custom Icon&quot;, icon_value=pcoll[&quot;ICON&quot;].icon_id)
    col.operator(&quot;object.duplicate_move&quot;, text=&quot;Built-in Icon&quot;, icon=&quot;OUTPUT&quot;)


bpy.utils.register_class(VIEW3D_PT_test)

Just note, that If you work on an addon, the dir should be like this dir = os.path.join(os.path.dirname(__file__), "icons") and icons should be saved in .png format into folder icons into the same place where your .py file is.

And if you work on a simple script, dir should be like this dir = os.path.dirname(bpy.data.filepath) and icons should be into the same place where your .py and .blend file is.

Simos Sigma
  • 413
  • 3
  • 11