2
$\begingroup$

I tried to recreate the File->Link...->somefile.blend->Collection->Collection linking mechanism in Blender 2.8 (Release canditate) in a script.

Unfortunatley my linking code does not behave like when I link via the GUI.

GUI: -> I get an EMPTY that I can move/rotate/scale /duplicate etc. (orange collection Icon without a "link symbol"). A linked collection inside of it. Perfect! This is what I want.

SCRIPT: -> I get a collection that is linked that I cant move etc. This behaviour seems not really usefull to me. I want to at least do the basic opperations on a "referenced"/linked object like move/rotate/scale.

I tried to place the linked collection in an EMPTY but the GUI does not allow that.

My script so far:

    context = bpy.context
    scenes = []
    mainCollection = bpy.data.collections['Collection']
    with bpy.data.libraries.load(blendFile) as (data_from, data_to):
        for name in data_from.scenes:
            scenes.append({'name': name})
        action = bpy.ops.wm.link
        action(directory=blendFile + "/Scene/", files=scenes)
        scenes = bpy.data.scenes[-len(scenes):]
    for scene in scenes:
        print(scene.name)
        for coll in scene.collection.children:
            print(coll.name)
            context.scene.collection.children.link(coll)

Any ideas how to place the linked collection inside an EMPTY via script to move it?

collection.parent = empty

A collection does not have a "parent" attribute.

Thanks in advance!

$\endgroup$
1
$\begingroup$

Thanks for your initial self-answer! It got me on the right track.

I think this way is even easier:

import bpy

master_collection = bpy.context.scene.collection

blendFile = 'path/to/file.blend'

with bpy.data.libraries.load(blendFile) as (data_from, data_to):
    data_to.collections = data_from.collections


for new_coll in data_to.collections:
    instance = bpy.data.objects.new(new_coll.name, None)
    instance.instance_type = 'COLLECTION'
    instance.instance_collection = new_coll
    master_collection.objects.link(instance)
$\endgroup$
0
$\begingroup$

I actually solved it myself:

    import bpy

    blendFile = "path/to/file.blend"

    context = bpy.context
    scenes = []
    mainCollection = bpy.data.collections['Collection']
    with bpy.data.libraries.load(blendFile) as (data_from, data_to):
        for name in data_from.scenes:
            scenes.append({'name': name})
        action = bpy.ops.wm.link
        action(directory=blendFile + "/Scene/", files=scenes)
        scenes = bpy.data.scenes[-len(scenes):]
    for scene in scenes:
        for coll in scene.collection.children:
            instance = bpy.data.objects.new('Instance', None)
            instance.instance_type = 'COLLECTION'
            instance.instance_collection = coll
            mainCollection.objects.link(instance)   
        bpy.data.scenes.remove(scene)

This behaves now like the link from GUI.

The magic happens here:

instance = bpy.data.objects.new('Instance', None)
instance.instance_type = 'COLLECTION'
instance.instance_collection = coll
mainCollection.objects.link(instance)   

Maybe this is usefull for someone else. :)

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.