Blender 3D: Noob to Pro/Advanced Tutorials/Python Scripting/Import scripts

Importing objects into Blender is not that different from exporting. However, there are a few additional things to take care of. Firstly, all references to "export" in the header should be changed to "import". Secondly, instead of simply writing out data that Blender provides to us, we are responsible for giving data to Blender and ensuring that it is properly formatted. Although Blender is flexible, allowing us to ignore things like vertex indices, we do need to be careful that we do things in a sensible order.

Additionally, there is a bit of housekeeping to deal with. We should be in edit mode while modifying the mesh data. We also need to link up our newly created data to the scene, after it has been properly constructed, so that Blender can see it and maintain it. This makes it visible to the user, as well as ensuring that it gets saved along with the scene.

Importing a Mesh

edit

Here is a simple script that can import an OBJ file created by the export script.

import Blender
def import_obj(path):
        Blender.Window.WaitCursor(1)
        name = path.split('\\')[-1].split('/')[-1]
        mesh = Blender.NMesh.New( name ) # create a new mesh
        # parse the file
        file = open(path, 'r')
        for line in file:
                words = line.split()
                if len(words) == 0 or words[0].startswith('#'):
                        pass
                elif words[0] == 'v':
                        x, y, z = float(words[1]), float(words[2]), float(words[3])
                        mesh.verts.append(Blender.NMesh.Vert(x, y, z))
                elif words[0] == 'f':
                        faceVertList = []
                        for faceIdx in words[1:]:
                                faceVert = mesh.verts[int(faceIdx)-1]
                                faceVertList.append(faceVert)
                        newFace = Blender.NMesh.Face(faceVertList)
                        mesh.addFace(newFace)
        
        # link the mesh to a new object
        ob = Blender.Object.New('Mesh', name) # Mesh must be spelled just this--it is a specific type
        ob.link(mesh) # tell the object to use the mesh we just made
        scn = Blender.Scene.GetCurrent()
        for o in scn.getChildren():
                o.sel = 0
        
        scn.link(ob) # link the object to the current scene
        ob.sel= 1
        ob.Layers = scn.Layers
        Blender.Window.WaitCursor(0)
        Blender.Window.RedrawAll()

Blender.Window.FileSelector(import_obj, 'Import')

This will load an OBJ file into Blender, creating a new mesh object. Let's take a look at the more interesting portions.

Blender.Window.WaitCursor(1)

Turn on the wait cursor so the user knows the computer is importing.

name = path.split('\\')[-1].split('/')[-1]
mesh = Blender.NMesh.New( name ) # create a new mesh

Here, we create a new mesh datablock. The name is made from the path only with the filename.

ob = Blender.Object.New('Mesh', name)
ob.link(mesh)

Next, we create a new object and link it to the mesh. This instantiates the mesh.

scn = Blender.Scene.GetCurrent()
scn.link(ob) # link the object to the current scene
ob.sel= 1
ob.Layers = scn.Layers

Finally, we attach the new object to the current scene, making it accessible to the user and ensuring that it will be saved along with the scene. We also select the new object so that the user can easily modify it after import. Copying the scenes layers ensures that the object will occupy the scenes current view layers.

Blender.Window.WaitCursor(0)
Blender.Window.RedrawAll()

Now the finishing touches. We turn off the wait cursor. We also redraw the 3D window to ensure that the new object is initially visible. If we didn't do this, the object might not appear until the user changes the viewpoint or forces a redraw in some other way.