#!BPY

""" Registration info for Blender menus:
Name: 'LDraw colors...'
Blender: 233
Group: 'Import'
Tip: 'Import LDraw colors (ldconfig.ldr) file.'
"""

import Blender

#0 //   Code ColorName        EdgeColor    R   G   B   A   DR  DG  DB  DA

# D is for the Dithered secondary colour. These materials wind up wrong anyway.

def loadcolors(filename):
  colors={}
  text=Blender.Text.New("LDrawMaterials")
  for line in file(filename):
    words=line.split()
    if words[0:2]==['0','COLOR']:
      name=words[3]
      number=words[2]
      mat=Blender.Material.New(name)
      mat.rgbCol=[float(c)/255 for c in words[5:8]]
      #mat.alpha=float(words[8])/255
      # Seems LDraw currently keeps transparent as full/no alpha..
      mat.alpha=(float(words[8])+float(words[12]))/510
      if mat.alpha<1:
      	mat.mode|=Blender.Material.Modes['ZTRANSP']
      #mat.alpha=float(words[12])/255
      text.write('%s %s\n'%(number, name))

from Blender.Window import FileSelector
FileSelector(loadcolors, "ldconfig.ldr")
