#!BPY # """ # Name: 'Import from 3DM' # Blender: 236 # Group: 'Import' # Tooltip: 'Import an object from the 3DM SceneGraphic file format into Blender.' # """ __author__ = "Charles INGELS" __version__ = "V0.0.1 07-02-2005" __url__ = "http://charles.ingels.free.fr/scene-graphic" __email__ = "charles@cingels.org" import sys import string import os import Blender from Blender import Window, Scene, NMesh def getToken ((s, i)): char_list = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_') while s[i] == ' ' and i < len(s): i = i + 1 end = i + 1 while s[end] in char_list and end < len(s): end = end + 1 return (s[i:end].lower(),end) def getValue3 ((s, i)): num_list = ('0','1','2','3','4','5','6','7','8','9','.','+','-') while s[i] == ' ' and i < len(s): i = i + 1 end = i + 1 while s[end] in num_list and end < len(s): end = end + 1 x = string.atof(s[i:end]) i = end while s[i] == ' ' and i < len(s): i = i + 1 end = i + 1 while s[end] in num_list and end < len(s): end = end + 1 y = string.atof(s[i:end]) i = end while s[i] == ' ' and i < len(s): i = i + 1 end = i + 1 while s[end] in num_list and end < len(s): end = end + 1 z = string.atof(s[i:end]) return (x,y,z) import_file = "" def WindowSelectFunc (filename): global import_file import_file = filename try: f = open (import_file, "r") except: print "***Erreur*** Impossible d'ouvrir le fichier %s " % (import_file) sys.exit(2) # Creation of camera data. camData = Blender.Camera.New ("ortho") camData.setName ("MainCamera") # Creation of a new scene. curScene = Blender.Scene.New ("NewScene") # Create a new camera. camObj = Blender.Object.New ("Camera") # Link the camera data to the camera object. camObj.link (camData) # Link the camera to the scene. curScene.link (camObj) # Make the new scene the current one. curScene.makeCurrent() # Create a new mesh. curMesh = Blender.NMesh.New("mesh") while 1: s = f.readline() if s == '': break; if len(s) == 1: continue i = 0 while s[i] == ' ' and i < len(s): i = i + 1 if i < len(s): if s[i] != '#': (chaine, i) = getToken ((s, i)) print "'%s'" % (chaine) if chaine == 'face': # Create a new face. curFace = Blender.NMesh.Face() print "Nouvelle face" if chaine == 'vertex': (x,y,z) = getValue3 ((s, i)) v = Blender.NMesh.Vert (x,y,z) # Add this vertex to the vertex list. curMesh.verts.append (v) # Add this vertex to the list of vertices in the current face. curFace.v.append (v) print "Vertex (%2.3f, %2.3f, %2.3f)" % (x,y,z) if chaine == 'normal': (x,y,z) = getValue3 ((s, i)) print "Normal (%2.3f, %2.3f, %2.3f)" % (x,y,z) if chaine == 'end_face': curMesh.faces.append (curFace) print "Fin de la face" f.close() meshObj = Blender.NMesh.PutRaw(curMesh) cursPos = Blender.Window.GetCursorPos() meshObj.setLocation (float(cursPos[0]), float(cursPos[1]), float(cursPos[2])) Window.FileSelector (WindowSelectFunc, "Import from 3DM", import_file) #if not import_file: # print "***Erreur*** Indiquez un fichier" # sys.exit(1)