1
$\begingroup$

I am having some trouble with my add ons. basically i have two add ons that append . blends. but they are both using the same script. so whenever i run the second add on it replaces the other one. I am assuming it is because it is a copy of the same script. so what do i need to alter on the second script to get both to work. i know next to nothing about scripting so any help is appreciated

Script

# Addon Info
bl_info = {
    "name": "The Coral And Creatures Collection",
    "description": "Coral And Creature Models",
    "author": "TRBRenders",
    "version": (1, 0),
    "blender": (2, 92, 0),
    "location": "View 3D > Properties Panel",
    "wiki_url": "",
    "tracker_url": "",
    "support": "COMMUNITY",
    "category": "Add Mesh"
    }


# Libraries
import bpy
import os
from bpy.props import *
from bpy.types import Panel, Operator, PropertyGroup
from bpy.utils import previews
from bpy.types import WindowManager


# Panel
class REAL_PT_trees(Panel):
    bl_space_type = "VIEW_3D"
    bl_context = "objectmode"
    bl_region_type = "UI"
    bl_label = "Coral And Creatures"
    bl_category = "The Coral And Creatures Collection"

    def draw(self, context):
        scn = bpy.context.scene
        layout = self.layout
        settings = scn.trees
        wm = context.window_manager

        # Categories
        col = layout.column(align=True)
        row = col.row(align=True)
        row.prop(settings, "category", expand=True)

        # Previews
        row = col.row()
        row.template_icon_view(wm, "tree_previews", show_labels=True)

        # Furniture name
        name = bpy.data.window_managers["WinMan"].tree_previews
        row = col.row(align=True)
        row.alignment = 'CENTER'
        row.label(text=name)

        # Add Button
        row = col.row(align=True)
        row.scale_y = 1.5
        row.operator("trees.add", icon="ADD", text="Add")


# Append
class TREES_OT_Add(Operator):
    bl_idname = "trees.add"
    bl_label = "Add Tree"
    bl_description = "Add the selected Models"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        category = context.scene.trees.category
        selected = bpy.data.window_managers["WinMan"].tree_previews

        # Append tree
        filepath = os.path.join(os.path.dirname(__file__), "Blends/" + category + os.sep + selected + ".blend")
        with bpy.data.libraries.load(filepath) as (data_from, data_to):
            data_to.objects = [name for name in data_from.objects]
        for obj in data_to.objects:
                context.collection.objects.link(obj)

        return{'FINISHED'}


# Previews
def generate_previews(self, context):
    category = context.scene.trees.category
    directory = os.path.join(os.path.dirname(__file__), "Icons")
    pcoll = trees_collection["main"]
    if directory == pcoll.images_location:
        return pcoll.tree_previews
    # Generate the thumbnails
    enum_items = []
    for i, image in enumerate(sorted(os.listdir(directory))):
        icon = pcoll.get(image)
        if not icon:
            filepath = os.path.join(directory, image)
            thumb = pcoll.load(image, filepath, 'IMAGE')
        else:
            thumb = pcoll[image]
        enum_items.append((image[:-4], image[:-4], "", thumb.icon_id, i))
    pcoll.tree_previews = enum_items
    pcoll.images_location = directory
    bpy.context.window_manager['tree_previews'] = 0
    return enum_items


# Properties
class TreeSettings(PropertyGroup):
    category : bpy.props.EnumProperty(
        name="Category",
        items= [
            
            
            ],
        description="Select a category"
        )


#############################################################################################
trees_collection = {}

classes = (
    REAL_PT_trees,
    TREES_OT_Add,
    TreeSettings
    )

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

# Register
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.trees = bpy.props.PointerProperty(type=TreeSettings)

    # Icons
    WindowManager.tree_previews = EnumProperty(
        name="Icons",
        items=generate_previews,
        description="Select Desired Models"
        )
    pcoll = bpy.utils.previews.new()
    pcoll.images_location = ""
    pcoll.tree_previews = ()
    trees_collection["main"] = pcoll


# Unregister
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    del bpy.types.Scene.trees
    del WindowManager.tree_previews

    for preview in trees_collection.values():
        bpy.utils.previews.remove(preview)
    trees_collection.clear()


if __name__ == "__main__":
    register()
$\endgroup$
2
  • 1
    $\begingroup$ Just rename all the classes and give the add-on a new name. Also please always post code as text. $\endgroup$ – brockmann May 4 at 6:06
  • $\begingroup$ blender.meta.stackexchange.com/questions/2788/… Suggest using operator properties to use same operator, eg bpy.ops.tree.add(lib="some.blend", tree="SomeTree") Also would take advantage of bpy.data.libraries and even using a pointer property. $\endgroup$ – batFINGER May 4 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.