You need to use bpy.data.libraries
. The class has a function load which read the datablock of a blend file.
Here an example of how to draw such dialog box to append either a group or an object.
class open_blend_file(bpy.types.Operator, ImportHelper):
"""Import a .blend file to append model from"""
bl_idname = "blend.open"
bl_label = "Import"
bl_options = {'UNDO'}
filter_glob = StringProperty(
default="*.blend",
options={'HIDDEN'},
)
group = BoolProperty()
def get_lib_groups(self,context):
l = [];
if os.path.exists(self.filepath) and self.filepath.endswith('.blend'):
with bpy.data.libraries.load(self.filepath) as (data_from, data_to):
groups = data_from.groups;
# objects = data_from.objects;
for g in groups:
l.append((g,g,g));
if len(l) == 0:
l = [("None","None","None")];
return l;
def get_lib_objects(self,context):
l = [];
if os.path.exists(self.filepath) and self.filepath.endswith('.blend'):
with bpy.data.libraries.load(self.filepath) as (data_from, data_to):
# groups = data_from.groups;
objects = data_from.objects;
for o in objects:
l.append((o, o, o));
if len(l) == 0:
l = [("None","None","None")];
return l;
lib_groups= EnumProperty(
name = "List of groups in the .blend file",
description = "List of groups in the .blend file",
items = get_lib_groups
);
lib_objects = EnumProperty(
name = "List of objects in the .blend file",
description = "List of objects in the .blend file",
items = get_lib_objects
);
def draw(self, context):
scene = bpy.context.scene;
props = scene.property;
layout = self.layout;
row = layout.row();
row.prop(props,'import_from', expand = True)
if props.import_from =='Groups':
if len(self.lib_groups)>0:
row = layout.row();
row.prop(self,'lib_groups', text = "Groups", icon = "GROUP")
else:
if len(self.lib_objects)>0:
row = layout.row();
row.prop(self,'lib_objects', text = "Objects", icon = "OBJECT_DATA")
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
if self.group :
name = self.lib_groups;
else:
name = self.lib_objects;
if self.group:
blend_file = self.filepath + "/Group/"
bpy.ops.wm.append(directory=blend_file, link=False, filename=name, autoselect = True);
group = bpy.data.groups.get(name);
if group is not None:
bpy.ops.object.empty_add(type='PLAIN_AXES', location = (0,0,0));
obj = get_active_object();
obj.name = group.name;
for o in group.objects:
o.parent = obj;
group.objects.link(obj);
else:
blend_file = self.filepath + "/Object/"
bpy.ops.wm.append(directory=blend_file, link=False, filename=name, autoselect = True);
return {'FINISHED'}