#!BPY
""" Released under the Blender Artistic Licence (BAL)
Name: 'TReal (POV-Ray .inc)'
Blender: 232
Group: 'Import'
Submenu: 'standard' std
Submenu: 'simple leaves' smp
Tooltip: 'import TReal tree (.inc)'
"""
__author__ = "pat"
__version__ = "1.5 - 09/12/04 -"
__email__ = ('Author, pat:psycho3d*de')
__url__ = ("Author's website, www.psycho3d.de")
__bpydoc__ ="""\
This script imports trees created by TReal.

You may choose to import simple leaves, this will import 1 face and 4 verts
per leaf instead of 6 faces and 8 verts. Useful for background trees.
"""

from Blender import NMesh, Window

def importTree(filename):
  file = open(filename, "r")
  fullText = file.read()
  file.close()
  obStart = -1
  obCount = fullText.count("#dec") # #declare
  if obCount > 0:
    turn = 0
    while turn < 2:
      i = 0
      turn += 1
      me = NMesh.New()
      while i < obCount:  #main loop, done twice for every object in the file (first time import stems, second leaves)
        i += 1
        obStart = fullText.find("#dec", obStart + 1, len(fullText)) #look for "#declare" from old obStart
        obEnd = fullText.find("#dec", obStart + 1, len(fullText))
        if obEnd < 0: obEnd = len(fullText)

        if turn == 1:
          vertsStart = 8 + fullText.find("mes", obStart, obEnd) # mesh2
          if vertsStart != 7:
            vertsEnd = fullText.find("}", vertsStart)
            vertCount = int(fullText[ vertsStart + 17 : fullText.find(",", vertsStart + 17)])
            vEnd = vertsStart
            vertList = []
            j = 0
            while j < vertCount:
              j += 1
              vStart = fullText.find("<", vEnd, vertsEnd) + 1
              vEnd = fullText.find(">", vStart, vertsEnd)
              vPos = fullText[vStart:vEnd].split(",")
              newVert = NMesh.Vert(float(vPos[0]),  -float(vPos[2]), float(vPos[1]))
              me.verts.append(newVert)
              vertList.append(newVert)

            facesStart = fullText.find("fac", obStart) # face_indices
            facesEnd = fullText.find("}", facesStart)
            faceCount = int(fullText[ facesStart + 15 : fullText.find(",", facesStart + 15)])
            fEnd = facesStart
            k = 0
            while k < faceCount:
              k += 1
              fStart = fullText.find("<", fEnd, facesEnd) + 1
              fEnd = fullText.find(">", fStart, facesEnd)
              f = NMesh.Face()
              fVertList = fullText[fStart:fEnd].split(",")
              for vIndex in fVertList:
                f.v.append(vertList[int(vIndex)])
              me.faces.append(f)

        elif turn == 2:
          polyStart = fullText.find("pol", obStart, obEnd) #polygon
          if polyStart != -1:
            polyEnd = fullText.find("pig", polyStart) #pigment
            pEnd = polyStart
            l = 0
            vertList = []
            if reduce == 1: #reduce to 4 verts per leaf
              while l < 8:
                l += 1
                pStart = fullText.find("<", pEnd, polyEnd) + 1
                pEnd = fullText.find(">", pStart, polyEnd)
                pPos = fullText[pStart:pEnd].split(",")
                vert = NMesh.Vert(float(pPos[0]), -float(pPos[2]), float(pPos[1]))
                vertList.append(vert)
              l = 0
              me.verts.append(vertList[0])
              me.verts.append(vertList[2])
              me.verts.append(vertList[4])
              me.verts.append(vertList[6])
              f = NMesh.Face([vertList[0], vertList[2], vertList[4], vertList[6]])
              #vertcols
              f.uv = [(0,0),(0,0),(0,0),(0,0)]
              colStart = fullText.find("<", polyEnd, polyEnd + 21) + 1
              if colStart != 0:
                colEnd = fullText.find(">", colStart, colStart + 60)
                colList = fullText[colStart:colEnd].split(",")
                vcol = NMesh.Col( \
                  int(float(colList[0]) * 255), \
                  int(float(colList[1]) * 255), \
                  int(float(colList[2]) * 255), 255)
                f.col = [vcol, vcol, vcol, vcol]
              me.faces.append(f)
            else: #high quality leaves
              while l < 8:
                l += 1
                pStart = fullText.find("<", pEnd, polyEnd) + 1
                pEnd = fullText.find(">", pStart, polyEnd)
                pPos = fullText[pStart:pEnd].split(",")
                vert = NMesh.Vert(float(pPos[0]), -float(pPos[2]), float(pPos[1]))
                me.verts.append(vert)
                vertList.append(vert) #this is the same vert-object, so i can use this list for indices from 0 for faces
              while l > 2:      #number of verts - 2 = number of faces
                l -= 1
                f = NMesh.Face()
                f.v.append(vertList[0])
                f.v.append(vertList[l - 1])
                f.v.append(vertList[l])
                #vertcols
                f.uv = [(0,0),(0,0),(0,0)]
                colStart = fullText.find("<", polyEnd, polyEnd + 21) + 1
                if colStart != 0:
                  colEnd = fullText.find(">", colStart, colStart + 60)
                  colList = fullText[colStart:colEnd].split(",")
                  vcol = NMesh.Col( \
                    int(float(colList[0]) * 255), \
                    int(float(colList[1]) * 255), \
                    int(float(colList[2]) * 255), 255)
                  f.col = [vcol, vcol, vcol]
                me.faces.append(f)
      NMesh.PutRaw(me)

def f(filename):
  importTree(filename)

arg = __script__['arg']
if arg == 'std':
  reduce = 0
  Window.FileSelector(f, "Select TReal .inc")
else:
  reduce = 1
  Window.FileSelector(f, "Select TReal .inc")
