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

I'm new to this stuff, so I don't feel confident editing the main page. But I noticed that this imported uses the deprecated "NMesh" class. As an exercise, I updated it to use Mesh instead. Here's my version; if somebody else likes it and wants to stick it on the main page, great.


 * 1) !BPY

""" Name: 'NewWikibooks' Blender: 244 Group: 'Import' Tooltip: 'Better Wikibooks sample importer' """

import Blender import bpy

def import_obj(path): Blender.Window.WaitCursor(1) name = path.split('\\')[-1].split('/')[-1] mesh = Blender.Mesh.New( name ) # create a new mesh # parse the file file = open(path, 'r') for line in file.readlines: 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.extend(x, y, z)               elif words[0] == 'f': faceVertList = [] for faceIdx in words[1:]: faceVert = mesh.verts[int(faceIdx)-1] faceVertList.append(faceVert) mesh.faces.extend(faceVertList) # 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')