populate blend file according to input file (e.g. for architecture visualization)

I work for a company that is developing a house buildings system where you assemble houses out of standard parts. Think Lego © just for real houses.

Once a house is designed using our construction software I end up with a file that contains all the locations of the parts used.

Now I want to build a blend file that will duplicate all used parts from a standard object library and then be able to do renders of the house.

This is the script I have developed so far (with lots of help from here).

Name: 'populate'
Blender: 237
Group: Object
import Blender
import math
import string
import bpy

from Blender import *
from math import *
from string import *

# prebuilding children list for speed reason
print "building child list" #status information the console window
children = {}
for ob in bpy.data.objects:
    if 1 in ob.layers and ob.parent: # all the object library parts are on layer 1 for speed reason
        if not ob.parent in children:
            children[ob.parent] = []

#function to select children of parent objects
def mark_as_selected(the_object):
    if the_object in children:
        for ob in children[the_object]:
            ob.sel = 1

print "starting population" 
print "writing logfile"
pf=open('D:\\UDaten\\LLpopulate.log', 'w') #log file

f = file('input_file.dat')
scn = Blender.Scene.GetCurrent()

#read file
for line in f:
    ar = split(line, '|') #delimiter character
        ob_act = Blender.Object.Get(ar[0]) # object name is first argument

        scn.objects.selected = []
        ob_act.sel = 1

        # select children

        Blender.Object.Duplicate() #Duplicate linked (might want to change this to not linked
        ob_act = Object.GetSelected()
        for t in ob_act:
            t.layers = [2] # move object to layer 2
            if t.getParent() is None: # Find parent
                t.LocX = float(ar[7].replace(",",".")) # reposition object at x (7th argument in this case and change "," to "."
        pftext =   "Ojbect %s not found.
" % (ar[0]) # update log file if object not found

print "ending population"

This supports objects that have sub-objects (parent-child). And for rendering ease the original parts (objects) are on layer 1 and the new populated parts are on layer 2

Basically this could be used for any case where you have to populate a blend file with objects according to an input file (trees, figures, cars, science stuff …)

Feel free to use it and let me know if you have ideas on how to improve it :smiley:


This sounds really cool (oddly enough, I’m currently trying to make a Lego library for Blender), but I loaded it up and got a few filepath errors (obviously) and don’t quite understand how it works. Does it only work with your particular piece of CAD software you’re using? Does it access other Blend files for objects?


I have all the “library” objects within one .blend file on layer 1. I guess you could have different library files and then link (Shift-F1 and then click on “link”) them into the current .blend file for organisation sake.

The objects all have an unique name and this name is the same as in the input file. The input file has the object name and stuff like x,y,z coordinates and rotation.

Our CAD software does not touch blender (other than producing a file that is read by the script) so it’s just a matter of adapting the script to whatever you need. You would need to upate the filename maybe and obviously the section where you reposition the duplicated objects.

I am not sure if you are having filepath errors with this script, but if yes, then I can only guess that it’s a problem of importing the python libraries?

Does this help?

Yup it does, thanks!