4

I want to import multiple objects from another .blend file using a python script. What is the best way to do this without having to import every single object?

Example of what i want to import:

Picture shows outliner with different mesh items all parented to house.container and starting with "house"

froggyman
  • 503
  • 5
  • 16

2 Answers2

5

Using BlendDataLibraries # bpy.data.libraries

import bpy

filepath = "//link_library.blend"

append all objects starting with 'house'

with bpy.data.libraries.load(filepath) as (data_from, data_to): data_to.objects = [name for name in data_from.objects if name.startswith("house")]

link them to scene

scene = bpy.context.scene for obj in data_to.objects: if obj is not None: scene.collection.objects.link(obj)

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • Thanks, it almost works! There is just one problem: When importing a beveled curve (for example like this: http://prntscr.com/elvbjv) I get this symbol (i think it is for "linked"?): http://prntscr.com/elvcr7. When i try to edit the Curve Bevel now i get the error "Cannot edit external libdata" and also the animation of the Curve Bevel doesn't show up after importing. – froggyman Mar 19 '17 at 15:45
  • Works fine for me, ran a simple test and it appends both curves and bevel objects etc from other blend files. Is Curve.bevel (your picture of which shows SFA) a linked object in your library file – batFINGER Mar 19 '17 at 16:04
  • It appended both for me, too. I also just ran a simple test scene but it appended this http://prntscr.com/elvrr0 instead of this http://prntscr.com/elvslb. This is a problem because the Animation data doesn't get transferred. All objects were created in the original file. – froggyman Mar 19 '17 at 16:20
  • Don't know, works for me and appends (simple test) actions from lib file. Might pay to make a sample lib file. – batFINGER Mar 19 '17 at 16:23
  • The action of the Curve (not the curve-bevel) gets appended automatically. Is there even a way to do this separately? – froggyman Mar 19 '17 at 16:27
  • Action on both curve and bevel get appended automatically for me and can be edited, I'll repeat, make a test file that shows same behaviour and post. – batFINGER Mar 19 '17 at 16:32
  • Here are the files: https://goo.gl/8KeVi5 – froggyman Mar 19 '17 at 16:41
  • 1
    Think someone else needs to look at this, as for me your sample files do exactly as expected and append both curves. There is no animation data. – batFINGER Mar 19 '17 at 17:04
  • Sorry, i forgot to include animation data. I updated the files. https://goo.gl/UsrCJA – froggyman Mar 19 '17 at 17:06
1

You need to use append and choose which object you want to import and which type this object is.

Some of the different types are shown below:

enter image description here

Once you know that then you can use the below python script which is found here to import the object you want from another .blend file

blendfile = 'D:/path_to_blend_file/source.blend'
section   = '\\Object\\'
objects    = ['myobj1','myobj2','myobj3','myobj4','myobj5'] ###<-- Add the name of objects you want to append

directory = blendfile + section

for obj in objects:  
    filename  = obj
    bpy.ops.wm.append(filename=filename, directory=directory)
Tak
  • 6,293
  • 7
  • 43
  • 85
  • 1
    FYI information directory and filename may be enough. See http://blender.stackexchange.com/questions/74378/how-to-import-a-3d-object-by-csv-file/74384#74384 – lemon Mar 16 '17 at 05:30
  • Thank you this helped, i don't know how to effeciently import multiple objects with your script. I edit my question to make it more clear. – froggyman Mar 16 '17 at 16:43
  • You can look this post http://blender.stackexchange.com/questions/17876/import-object-without-bpy-ops-wm-link-append – pistiwique Mar 16 '17 at 19:43
  • Thanks but i already tried it and it doesn't work with beveled Curves somehow – froggyman Mar 16 '17 at 19:45
  • @froggyman answer updated. You include the object names you want to append in the objects list – Tak Mar 16 '17 at 23:29
  • Thanks, but this is kind of what i didn't want to do. I wrote in my question "without having to import every single object". Is there really no way? For example could i import all parents of an object? Or all objects starting with "house."? – froggyman Mar 17 '17 at 12:24