#!BPY
""" Released under the Blender Artistic Licence (BAL)
Name: 'POVtree (remdoubles)'
Blender: 232
Group: 'Import'
Tooltip: 'Import POVtree mesh (.inc)'
"""
__author__ = "pat"
__version__ = "1.21 - 07/11/05 -"
__email__ = ('Author, pat:psycho3d*de')
__url__ = ("Author's website, www.psycho3d.de")
__bpydoc__ ="""\
This script imports POVtree generated trees.
Note:
Removing doubles makes importing much slower,
the bigger the file the longer it takes.
There might be some "doubles" left depending on the limit you set when
removing doubles in Blender's EditMode.
"""
from Blender import NMesh
from Blender.Window import FileSelector
#returns a list of coordinates
def str2vert(input):
input = input.split(",")
for i in range(3):
input[i] = float(input[i].strip(", "))
return input
### main procedure
def import_POVtree(filename):
file = open(filename, "r")
incfile = file.read()
file.close()
folStart = incfile.find("#declare FOLIAGE")
folEnd = incfile.find("#declare", folStart + 1)
blosStart = incfile.find("#declare BLOSSOM")
blosEnd = incfile.find("#declare", blosStart + 1)
woodStart = incfile.find("#declare WOOD")
woodEnd = incfile.find("#declare", woodStart + 1)
if folStart != -1:
trilist = []
vertlist = []
triEnd = folStart + 15
triStart = incfile.find("triangle", triEnd, folEnd)
while triStart != -1:
triEnd = incfile.find("}", triStart)
tri = []
e = triStart
for i in range(3):
s = incfile.find("<", e) + 1
e = incfile.find(">", s)
newVert = incfile[s:e]
try: # to append an existing vertex
tri.append(vertlist.index(newVert))
except ValueError:
#vert doesn't exist, create new one
tri.append(len(vertlist)) #that's the index of the current vert
vertlist.append(newVert)
trilist.append(tri)
triStart = incfile.find("triangle", triEnd, folEnd)
#create verts and faces from lists of strings and indices
for i in range(len(vertlist)):
v = str2vert(vertlist[i])
vertlist[i] = NMesh.Vert(v[0], -v[2], -v[1])
foliage = NMesh.New()
foliage.verts.extend(vertlist)
for i in range(len(trilist)):
foliage.faces.append(NMesh.Face([ \
vertlist[trilist[i][0]], \
vertlist[trilist[i][1]], \
vertlist[trilist[i][2]]]))
NMesh.PutRaw(foliage)
del foliage
if blosStart != -1:
trilist = []
vertlist = []
triEnd = blosStart + 15
triStart = incfile.find("triangle", triEnd, blosEnd)
while triStart != -1:
triEnd = incfile.find("}", triStart)
tri = []
e = triStart
for i in range(3):
s = incfile.find("<", e) + 1
e = incfile.find(">", s)
newVert = incfile[s:e]
try:
tri.append(vertlist.index(newVert))
except ValueError:
tri.append(len(vertlist))
vertlist.append(newVert)
trilist.append(tri)
triStart = incfile.find("triangle", triEnd, blosEnd)
for i in range(len(vertlist)):
v = str2vert(vertlist[i])
vertlist[i] = NMesh.Vert(v[0], -v[2], -v[1])
blossom = NMesh.New()
blossom.verts.extend(vertlist)
for i in range(len(trilist)):
blossom.faces.append(NMesh.Face([ \
vertlist[trilist[i][0]], \
vertlist[trilist[i][1]], \
vertlist[trilist[i][2]]]))
NMesh.PutRaw(blossom)
del blossom
if woodStart != -1:
trilist = []
vertlist = []
triEnd = woodStart + 12
triStart = incfile.find("triangle", triEnd, woodEnd)
while triStart != -1:
triEnd = incfile.find("}", triStart)
tri = []
e = triStart
for i in range(3):
s = incfile.find("<", e) + 1
e = incfile.find(">", s)
newVert = incfile[s:e]
try:
tri.append(vertlist.index(newVert))
except ValueError:
tri.append(len(vertlist))
vertlist.append(newVert)
trilist.append(tri)
triStart = incfile.find("triangle", triEnd, woodEnd)
for i in range(len(vertlist)):
v = str2vert(vertlist[i])
vertlist[i] = NMesh.Vert(v[0], -v[2], -v[1])
wood = NMesh.New()
wood.verts.extend(vertlist)
for i in range(len(trilist)):
wood.faces.append(NMesh.Face([ \
vertlist[trilist[i][0]], \
vertlist[trilist[i][1]], \
vertlist[trilist[i][2]]]))
NMesh.PutRaw(wood)
### fileSelector callback
def f(filename):
import_POVtree(filename)
##### script start #####
FileSelector(f, "Select POVtree .inc")