0

I have a random 3D model from a .blend file and I have been using Blenders Data Libraries to import it.

Here is the code:

import bpy

filepath = r"C:\path\example.blend"

coll_name = "collection name"

link = False

with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to): data_to.collections = [c for c in data_from.collections if c.startswith(coll_name)]

for coll in data_to.collections: if coll is not None: bpy.context.scene.collection.children.link(coll)

It imports my object I need. But I would like to be able to press a button to activate it. So how can I do that?

I want the button to look like this:

enter image description here

(That type of formatting)

-

If you have any questions, please let me know and I will reply as soon as I can.

Thanks Daniel

Daniel
  • 622
  • 4
  • 16

1 Answers1

2

i took the liberty to make an addon out of it.

It's a bit convoluted, so i made the simple version and EXTRA with file picker, it's mostly from https://www.youtube.com/watch?v=9fuFDHR-UkE&list=PLa1F2ddGya_8acrgoQr1fTeIuQtkSd6BW&index=6

it adds a panel default 'N' key the panel can be merged with existing panels by modifying bl_category

just create a file in your blender instal folder /Version Nr/Scripts/Addons/ name it anything.py

then in blender after a restart go to preference/addons search for "Link data" and enable it.

hope that saves you a headache. hehe


bl_info = {
    "name" : "Link to Data Library",
    "author" : "Daniel",
    "description" : "Add description Here",
    "blender" : (2, 80, 0),
    "version" : (0, 0, 1),
    "location" : "",
    "warning" : "",
    "category" : "Generic"
}

import bpy

class OBJECT_OT_link_data(bpy.types.Operator): """Tool tip thingy""" bl_label = "Link Data" bl_idname = "object.link_data" bl_options = {"REGISTER", "UNDO"} bl_context = "objectmode"

def execute(self, context):

    filepath = r"C:\\path\example.blend"

    coll_name = "collection name"

    link = False

    with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
        data_to.collections = [c for c in data_from.collections if c.startswith(coll_name)]

    for coll in data_to.collections:
        if coll is not None:
           bpy.context.scene.collection.children.link(coll)

    return {"FINISHED"}

class VIEW3D_PT_Link_Data(bpy.types.Panel): """Tool tip thingy""" bl_label = "Link To Data" bl_category = "Link Data" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_context = "objectmode"

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

    col = layout.column()

    col.operator("OBJECT_OT_link_data",
        text="link data",
        icon="PACKAGE"
    )


def register(): bpy.utils.register_class(OBJECT_OT_link_data) bpy.utils.register_class(VIEW3D_PT_Link_Data)

def unregister(): bpy.utils.unregister_class(OBJECT_OT_link_data) bpy.utils.unregister_class(VIEW3D_PT_Link_Data)

if name == "main": register()

Modified version with EXTRA bits

bl_info = {
    "name" : "Link to Data Library",
    "author" : "Daniel",
    "description" : "Add description Here",
    "blender" : (2, 80, 0),
    "version" : (0, 0, 1),
    "location" : "",
    "warning" : "",
    "category" : "Generic"
}

import bpy

class OBJECT_OT_link_data(bpy.types.Operator): """Tool tip thingy""" bl_label = "Link Data" bl_idname = "object.link_data" bl_options = {"REGISTER", "UNDO"} bl_context = "objectmode"

def execute(self, context):

    filepath = bpy.path.abspath(context.scene.DanielLinkFile)

    coll_name = context.scene.DanielLinkCollection

    link = context.scene.DanielLink

    with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
        data_to.collections = [c for c in data_from.collections if c.startswith(coll_name)]

    for coll in data_to.collections:
        if coll is not None:
           bpy.context.scene.collection.children.link(coll)

    return {"FINISHED"}

class VIEW3D_PT_Link_Data(bpy.types.Panel): """Tool tip thingy""" bl_label = "Link To Data" bl_category = "Link Data" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_context = "objectmode"

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

    col = layout.column()

    col.prop(context.scene, "DanielLinkFile")
    col.prop(context.scene, "DanielLinkCollection")
    col.prop(context.scene, "DanielLink")

    col.operator("OBJECT_OT_link_data",
        text="link data",
        icon="PACKAGE"
    )


def register(): bpy.types.Scene.DanielLinkFile = bpy.props.StringProperty(name="Link File",subtype="FILE_PATH", default=r"C:\path\example.blend") bpy.types.Scene.DanielLinkCollection = bpy.props.StringProperty(name="Link Collection",default="collection name") bpy.types.Scene.DanielLink = bpy.props.BoolProperty(name="Link",default=False)

bpy.utils.register_class(OBJECT_OT_link_data)
bpy.utils.register_class(VIEW3D_PT_Link_Data)

def unregister(): del bpy.types.Scene.DanielLinkFile del bpy.types.Scene.DanielLinkCollection del bpy.types.Scene.DanielLink bpy.utils.unregister_class(OBJECT_OT_link_data) bpy.utils.unregister_class(VIEW3D_PT_Link_Data)

if name == "main": register()

```