#!BPY
"""
Name: 'Collada 1.4.1 Importer (.dae)'
Blender: 245
Group: 'Import'
Tip: 'Import Collada..(.dae)'

"""

__author__ = "teraapi"
__url__ = "http://teraapi.com"
__version__ = "1.0 2008/10/11"


import Blender
import Dom4Blender
import datetime
import bpy
from Blender import Mathutils
from Blender.Mathutils import *
from Blender import Draw, BGL


g_scale = 0.1
g_autoScale = 0
g_filename = ""
g_smooth = 1
g_clearAll = 1
g_noAnimation = 0
g_fps = 25

def import_dae():
  global g_scale, g_autoScale, g_filename, g_smooth, g_clearAll, g_noAnimation, g_fps
  Blender.Window.WaitCursor(1)
  Blender.Window.EditMode(0)
  print "Start: %s" % datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
#  updateStore() #debug
  if g_autoScale==1:
    Dom4Blender.setAutoScale()
  else:
    Dom4Blender.setScale(g_scale)
  if g_smooth==1:
    Dom4Blender.setSmooth()
#  if g_noAnimation==1:
#    Dom4Blender.setNoAnimation()
  Dom4Blender.setFps(g_fps)
  Dom4Blender.load(g_filename)
  updateStore()
  Blender.Window.WaitCursor(0)
#  Blender.Window.EditMode(1)
  Blender.Window.RedrawAll()
  Blender.Draw.Redraw()
  Blender.Draw.Exit()
  for o in bpy.data.objects:
    o.makeDisplayList()
  print "End: %s" % datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
  Blender.Window.RedrawAll()
  Blender.Draw.Redraw()

def updateStore():
  global g_scale,g_autoScale, g_filename, g_smooth, g_clearAll, g_noAnimation, g_fps
  s = {}
  s['filename'] = g_filename
  s['scale'] = g_scale
  s['autoScale'] = g_autoScale
  s['smooth'] = g_smooth
  s['clearAll'] = g_clearAll
  s['noAnimation'] = g_noAnimation
  s['fps'] = g_fps
  Blender.Registry.SetKey('daeImport10', s, True)

r = Blender.Registry.GetKey('daeImport10', True)
if r:
  try:
    g_filename = r['filename']
    g_scale = r['scale']
    g_autoScale = r['autoScale']
    g_smooth = r['smooth']
    g_clearAll = r['clearAll']
    g_noAnimation = r['noAnimation']
    g_fps = r['fps']
  except:
    updateStore()

def file_select(filename):
  global g_filename
  if filename:
    g_filename = filename
  Draw.Redraw(1)

def event(evt, val):
  if evt == Draw.ESCKEY:
    Draw.Exit()
    return
  else: return
  Draw.Redraw(1)

def button_event(evt):
  global g_scale,g_autoScale,g_filename,g_smooth,g_clearAll, g_noAnimation, g_fps
  if evt == 11:
    Blender.Window.FileSelector( file_select, "Import", g_filename )
    Draw.Redraw(1)
  elif evt == 12:
    ret = Draw.PupFloatInput("Scale:", 1, 0.0001, 10000, 1, 4)
    if ret:
      g_scale = ret
      g_autoScale = 0
    Draw.Redraw(1)
  elif evt == 13:
    if g_autoScale==1:
      g_autoScale = 0
    else:
      g_autoScale = 1
    Draw.Redraw(1)
  elif evt == 14:
    if g_smooth==1:
      g_smooth = 0
    else:
      g_smooth = 1
    Draw.Redraw(1)
  elif evt == 16:
    if g_noAnimation==1:
      g_noAnimation = 0
    else:
      g_noAnimation = 1
    Draw.Redraw(1)
  elif evt == 17:
    ret = Draw.PupIntInput("Fps:", g_fps, 1, 120)
    if ret:
      g_fps = ret
    Draw.Redraw(1)
  elif evt == 18:
    g_fps = bpy.data.scenes.active.getRenderingContext().fps
    Draw.Redraw(1)
  elif evt == 1:
    import_dae()
  elif evt == 2:
    Blender.Window.RedrawAll()
    Blender.Draw.Exit()

def gui():
  global g_scale,g_autoScale, g_filename,g_smooth,g_clearAll, g_noAnimation, g_fps
  BGL.glClearColor(0,0,0,1)
  BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)

  BGL.glColor3f(0.921,0.38,0)
  BGL.glRasterPos2i(10, 350)
  Draw.Text("Collada 1.4.1 Importer", "large")
  BGL.glRasterPos2i(30, 330)
  Draw.Text("http://teraapi.com/collada4blender/", "large")

  BGL.glColor3f(1,1,1)

  Draw.PushButton("File:", 11, 10, 273, 80, 30)
  BGL.glRasterPos2i(110, 290)
  Draw.Text(g_filename)

  Draw.PushButton("Scale:", 12, 10, 228, 80, 30)
  BGL.glRasterPos2i(110, 240)
  if g_autoScale==0:
    Draw.Text("%f"%(g_scale))
  Draw.Toggle("Auto", 13, 240, 235, 80, 20, g_autoScale, "Auto Scale")

  Draw.Toggle("Smooth", 14, 10, 130, 80, 30, g_smooth, "Auto Smooth")
#  Draw.Toggle("Clear All", 15, 110, 130, 80, 30, g_clearAll, "Clear All")
#  Draw.Toggle("NoAnimation", 16, 110, 130, 80, 30, g_noAnimation, "NoAnimation")

  Draw.PushButton("Fps:", 17, 10, 178, 80, 30)
  BGL.glRasterPos2i(110, 190)
  Draw.Text("%d"%(g_fps))
  Draw.Toggle("Current FPS", 18, 240, 178, 80, 20, 0, "Get FPS of current scene")

#  BGL.glColor3f(0.921,0.38,0)
#  BGL.glRasterPos2i(10, 80)
#  Draw.Text("Please run \'Collada 1.4.1 Importer: fix disappear (Update Objects)\' in 'File->Import menu' after \'Import\'")

  Draw.PushButton("Import", 1, 10, 10, 150, 50)
  Draw.PushButton("Exit", 2, 170, 10, 50, 30)

Draw.Register(gui, event, button_event)
Draw.Redraw(1)



