2007-06-08 11:05:16 -07:00
#!BPY
"""
Name : ' Warzone model (.pie)... '
Blender : 244
Group : ' Import '
Tooltip : ' Load a Warzone model file '
"""
2007-06-19 14:51:57 -07:00
__author__ = " Rodolphe Suescun, Gerard Krol, Kevin Gillette "
2007-06-08 11:05:16 -07:00
__url__ = [ " blender " ]
2007-08-20 08:09:13 -07:00
__version__ = " 1.2 "
2007-06-08 11:05:16 -07:00
__bpydoc__ = """ \
This script imports PIE files to Blender .
Usage :
Run this script from " File->Import " menu and then load the desired PIE file .
"""
#
# --------------------------------------------------------------------------
# PIE Import v0.1 by Rodolphe Suescun (AKA RodZilla)
# v0.2 by Gerard Krol (gerard_)
2007-07-14 03:22:05 -07:00
# v0.3 by Kevin Gillette (kage)
# v1.0 --
2007-06-08 11:05:16 -07:00
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
from Blender import *
2007-07-26 13:53:20 -07:00
from pie_common import *
2007-07-14 03:22:05 -07:00
import os , pie
2007-06-08 11:05:16 -07:00
#==================================================================================#
2007-07-14 03:22:05 -07:00
# Draws a thumbnail from a Blender image at x,y with sides that are edgelen long #
2007-06-08 11:05:16 -07:00
#==================================================================================#
2007-07-14 03:22:05 -07:00
gen = None
2007-08-20 08:09:13 -07:00
ui_ref = None
2007-07-14 03:22:05 -07:00
def fresh_generator ( filename ) :
return pie . data_mutator ( pie . parse ( filename ) )
def seek_to_directive ( err_on_nonmatch , fatal_if_not_found , * directives ) :
for i in gen :
type = i [ pie . TOKEN_TYPE ]
if type == " directive " and i [ pie . DIRECTIVE_NAME ] in directives :
return i
elif type == " error " :
2007-08-20 08:09:13 -07:00
ui . debug ( i [ pie . ERROR ] . args [ 0 ] , " error " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
elif err_on_nonmatch :
2007-08-20 08:09:13 -07:00
ui . debug ( " expected %s directive. " % directives [ 0 ] , " warning " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
if fatal_if_not_found :
2007-08-20 08:09:13 -07:00
ui . debug ( directives [ 0 ] + " directive not found. cannot continue. " ,
2007-07-14 03:22:05 -07:00
" fatal-error " )
2007-08-20 08:09:13 -07:00
def generate_opt_gui ( rect , optlist , tooltips , limits ) :
2007-07-26 13:53:20 -07:00
""" expects rect to be [xmin, ymin, xmax, ymax] """
BGL . glRecti ( * rect )
buttonwidth = 120
buttonheight = 20
margin = 5
defbuttonwidth = 20 # default button
defbuttonpos = rect [ 2 ] - defbuttonwidth - margin
buttonpos = defbuttonpos - margin - buttonwidth
labelwidth = buttonpos - rect [ 0 ] - margin
numopts = len ( optlist )
for i , opt in enumerate ( optlist ) :
name = opt [ ' name ' ]
val = scalar_value ( opt [ ' val ' ] )
posY = rect [ 3 ] - ( i + 1 ) * ( buttonheight + margin )
if posY < rect [ 1 ] + margin : break
Draw . PushButton ( " D " , i + numopts , defbuttonpos , posY , defbuttonwidth , buttonheight ,
" Set this option to its default " )
2007-08-20 08:09:13 -07:00
tooltip = tooltips . get ( name , " " )
2007-07-26 13:53:20 -07:00
if isinstance ( opt [ ' opts ' ] , basestring ) :
title = opt . get ( ' title ' , " " )
if opt [ ' opts ' ] is ' number ' :
opt [ ' val ' ] = Draw . Number ( title , i , buttonpos , posY , buttonwidth ,
2007-08-20 08:09:13 -07:00
buttonheight , val , limits [ name ] [ 0 ] ,
limits [ name ] [ 1 ] , tooltip )
2007-07-26 13:53:20 -07:00
elif opt [ ' opts ' ] is ' bool ' :
opt [ ' val ' ] = Draw . Toggle ( title , i , buttonpos , posY , buttonwidth ,
buttonheight , val , tooltip )
else :
numopts = len ( opt [ ' opts ' ] )
if numopts < 2 :
2007-08-20 08:09:13 -07:00
ui . debug ( " error: invalid option supplied to generate_opt_gui " )
2007-07-26 13:53:20 -07:00
continue
if numopts is 2 :
Draw . PushButton ( opt [ ' opts ' ] [ val ] , i , buttonpos , posY ,
buttonwidth , buttonheight , tooltip )
else :
menustr = opt . get ( ' title ' , " " )
if menustr : menustr + = " % t| "
menustr + = ' | ' . join ( " %s %% x %i " % ( opt [ ' opts ' ] [ j ] , j ) for j in xrange ( numopts ) )
opt [ ' val ' ] = Draw . Menu ( menustr , i , buttonpos , posY , buttonwidth ,
buttonheight , val , tooltip )
Draw . Label ( opt [ ' label ' ] , rect [ 0 ] + margin , posY , labelwidth , buttonheight )
2007-08-20 08:09:13 -07:00
def opt_process ( ui ) :
ui . setData ( ' defaults/script ' , { ' auto-layer ' : True , ' import-scale ' : 1.0 ,
2007-07-26 13:53:20 -07:00
' scripts/layers ' : " pie_levels_to_layers.py " ,
2007-08-20 08:09:13 -07:00
' scripts/validate ' : " pie_validate.py " } )
ui . setData ( ' defaults/user ' , Registry . GetKey ( ' warzone_pie ' , True ) or dict ( ) )
ui . setData ( ' limits ' , { ' import-scale ' : ( 0.1 , 1000.0 ) } )
ui . setData ( ' tooltips ' , { ' auto-layer ' : " Selecting this would be the same as selecting all the newly created objects and running Object -> Scripts -> \" PIE levels -> layers \" " ,
2007-07-26 13:53:20 -07:00
' import-scale ' : " Values under 1.0 may be useful when importing from a model edited at abnormally large size in pie slicer. Values over 1.0 probably won ' t be useful. " ,
' scripts/layers ' : " Basename of the \" PIE levels -> layers \" script. Do not include the path. Whitespace is not trimmed " ,
2007-08-20 08:09:13 -07:00
' scripts/validate ' : " Basename of the \" PIE validate \" script. Do not include the path. Whitespace is not trimmed " } )
2007-07-26 13:53:20 -07:00
i = seek_to_directive ( True , False , " PIE " )
if i is None :
2007-08-20 08:09:13 -07:00
ui . debug ( " not a valid PIE. PIE directive is required " , " warning " )
gen = fresh_generator ( ui . getData ( ' filename ' ) )
ui . setData ( ' pie-version ' , 2 , True )
2007-07-26 13:53:20 -07:00
else :
2007-08-20 08:09:13 -07:00
ui . setData ( ' pie-version ' , i [ pie . DIRECTIVE_VALUE ] , True )
ui . debug ( " version %i pie detected " % ui . getData ( ' pie-version ' ) )
2007-07-26 13:53:20 -07:00
i = seek_to_directive ( True , False , " TYPE " )
if i is None :
2007-08-20 08:09:13 -07:00
ui . debug ( " not a valid PIE. TYPE directive is required " , " warning " )
gen = fresh_generator ( ui . getData ( ' filename ' ) )
ui . setData ( ' pie-type ' , 0x200 , True )
2007-07-26 13:53:20 -07:00
else :
2007-08-20 08:09:13 -07:00
ui . setData ( ' pie-type ' , i [ pie . DIRECTIVE_VALUE ] , True )
ui . debug ( " type %i pie detected " % ui . getData ( ' pie-type ' ) )
2007-07-26 13:53:20 -07:00
optlist = list ( )
dirs = Get ( ' uscriptsdir ' ) , Get ( ' scriptsdir ' )
2007-08-20 08:09:13 -07:00
script = default_value ( ui , ' scripts/layers ' )
2007-07-26 13:53:20 -07:00
for d in dirs :
if d and script in os . listdir ( d ) :
optlist . append ( { ' name ' : " auto-layer " ,
' label ' : " Assign levels to different layers? " ,
' opts ' : ' bool ' , ' title ' : " Automatically Layer " } )
2007-08-20 08:09:13 -07:00
ui . setData ( ' scripts/layers ' , os . path . join ( d , script ) , True )
2007-07-26 13:53:20 -07:00
break
else :
2007-08-20 08:09:13 -07:00
ui . debug ( " Could not find ' %s ' . automatic layering will not be available " % script )
2007-07-26 13:53:20 -07:00
optlist . append ( { ' name ' : " import-scale " , ' label ' : " Scale all points by a factor. " ,
' opts ' : ' number ' , ' title ' : " Scale " } )
for opt in optlist :
2007-08-20 08:09:13 -07:00
opt . setdefault ( ' val ' , default_value ( ui , opt [ ' name ' ] ) )
ui . setData ( ' optlist ' , optlist )
2007-07-26 13:53:20 -07:00
2007-08-20 08:09:13 -07:00
def opt_draw ( ui ) :
optlist = ui . getData ( ' optlist ' )
numopts = len ( optlist )
2007-07-26 13:53:20 -07:00
Draw . PushButton ( " Cancel " , numopts * 2 + 1 , 68 , 15 , 140 , 30 , " Cancel the import operation " )
Draw . PushButton ( " Proceed " , numopts * 2 , 217 , 15 , 140 , 30 , " Confirm texpage selection and continue " )
Draw . PushButton ( " Save Defaults " , numopts * 2 + 3 , 68 , 321 , 140 , 30 , " Save options in their current state as the default " )
Draw . PushButton ( " Default All " , numopts * 2 + 2 , 217 , 321 , 140 , 30 , " Revert all options to their defaults " )
BGL . glClearColor ( 0.7 , 0.7 , 0.7 , 1 )
BGL . glClear ( BGL . GL_COLOR_BUFFER_BIT )
BGL . glColor3f ( 0.8 , 0.8 , 0.8 )
BGL . glRecti ( 5 , 5 , 431 , 411 )
BGL . glColor3f ( 0.7 , 0.7 , 0.7 )
BGL . glRecti ( 15 , 361 , 421 , 401 )
2007-08-20 08:09:13 -07:00
generate_opt_gui ( [ 15 , 55 , 421 , 311 ] , optlist , ui . getData ( ' tooltips ' ) , ui . getData ( ' limits ' ) )
2007-07-26 13:53:20 -07:00
BGL . glColor3i ( 0 , 0 , 0 )
text = ( " General Options " , " large " )
BGL . glRasterPos2i ( int ( ( 406 - Draw . GetStringWidth ( * text ) ) / 2 + 15 ) , 377 )
Draw . Text ( * text )
2007-08-20 08:09:13 -07:00
def opt_evt ( ui , val ) :
opts = ui . getData ( ' optlist ' )
2007-07-26 13:53:20 -07:00
numopts = len ( opts )
if val > = numopts * 2 :
val - = numopts * 2
if val is 0 :
2007-08-20 08:09:13 -07:00
if not ui . getData ( ' defaults/user ' ) : save_defaults ( ui )
2007-07-26 13:53:20 -07:00
for opt in opts :
2007-08-20 08:09:13 -07:00
ui . setData ( opt [ ' name ' ] , scalar_value ( opt [ ' val ' ] ) , True )
2007-07-26 13:53:20 -07:00
return True
elif val is 1 :
return False
elif val is 2 :
2007-08-20 08:09:13 -07:00
if ui . getData ( ' defaults/user ' ) :
2007-07-26 13:53:20 -07:00
def_src = Draw . PupMenu (
" Source of default value % t|Script default|User default " )
else :
def_src = 1
if def_src > 0 :
for opt in opts :
name = opt [ ' name ' ]
2007-08-20 08:09:13 -07:00
if def_src is 1 : opt [ ' val ' ] = ui . getData ( ' defaults/script ' ) [ name ]
elif def_src is 2 : opt [ ' val ' ] = default_value ( ui , name )
2007-07-26 13:53:20 -07:00
else : break
Draw . Redraw ( )
elif val is 3 :
2007-08-20 08:09:13 -07:00
save_defaults ( ui )
2007-07-26 13:53:20 -07:00
return
if val < numopts :
opt = opts [ val ]
if not isinstance ( opt , basestring ) :
if len ( opt [ ' opts ' ] ) is 2 :
opt [ ' val ' ] = abs ( opt [ ' val ' ] - 1 ) # toggle it between 0 and 1
elif val < numopts * 2 :
2007-08-20 08:09:13 -07:00
opt = ui . getData ( ' optlist ' ) [ val - numopts ]
2007-07-26 13:53:20 -07:00
name = opt [ ' name ' ]
2007-08-20 08:09:13 -07:00
if name in ui . getData ( ' defaults/user ' ) :
2007-07-26 13:53:20 -07:00
def_src = Draw . PupMenu (
" Source of default value % t|Script default|User default " )
else :
def_src = 1
2007-08-20 08:09:13 -07:00
if def_src is 1 : opt [ ' val ' ] = ui . getData ( ' defaults/script ' ) [ name ]
elif def_src is 2 : opt [ ' val ' ] = ui . getData ( ' defaults/user ' ) [ name ]
2007-07-26 13:53:20 -07:00
else : return
Draw . Redraw ( )
2007-07-14 03:22:05 -07:00
def thumbnailize ( img , x , y , edgelen ) :
try :
# BGL.glClearColor(0.7, 0.7, 0.7, 1)
# BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
if img : BGL . glColor3f ( 0.0 , 0.0 , 0.0 )
else : BGL . glColor3f ( 1.0 , 1.0 , 1.0 )
BGL . glRecti ( x , y , x + edgelen , y + edgelen )
except NameError , AttributeError :
2007-08-20 08:09:13 -07:00
ui . debug ( " unable to load BGL. will not affect anything, though " )
2007-07-14 03:22:05 -07:00
if not img : return
width , height = img . getSize ( )
edgelen = float ( edgelen )
if width > height : primary , secondary , reverse = width , height , False
else : primary , secondary , reverse = height , width , True
if primary < edgelen : zoom = 1.0
else : zoom = 1.0 / ( primary / edgelen )
offset = int ( ( edgelen - zoom * secondary ) / 2 )
if zoom == 1.0 : offset = [ int ( ( edgelen - zoom * primary ) / 2 ) , offset ]
else : offset = [ 0 , offset ]
if reverse : offset . reverse ( )
Draw . Image ( img , offset [ 0 ] + x , offset [ 1 ] + y , zoom , zoom )
def new_texpage ( filename ) :
2007-08-20 08:09:13 -07:00
texpage_cache = ui_ref . getData ( ' texpage-cache ' )
options = ui_ref . getData ( ' texpage-opts ' )
2007-07-14 03:22:05 -07:00
if filename in texpage_cache :
img = texpage_cache [ filename ]
else :
img = Image . Load ( filename )
texpage_cache [ filename ] = img
if filename not in options :
2007-08-20 08:09:13 -07:00
ui . getData ( ' texpage-menu ' ) . val = len ( options )
2007-07-14 03:22:05 -07:00
options . append ( filename )
2007-08-20 08:09:13 -07:00
if ' texpage-dir ' not in ui :
ui . setData ( ' texpage-dir ' , os . path . dirname ( filename ) )
2007-07-14 03:22:05 -07:00
return img
2007-08-20 08:09:13 -07:00
def texpage_process ( ui ) :
2007-07-14 03:22:05 -07:00
global gen
2007-08-20 08:09:13 -07:00
ui . setData ( ' texpage-menu ' , Draw . Create ( 0 ) )
ui . setData ( ' texpage-opts ' , list ( ) )
ui . setData ( ' texpage-cache ' , dict ( ) )
2007-07-14 03:22:05 -07:00
i = seek_to_directive ( False , False , " NOTEXTURE " , " TEXTURE " )
if i is None : # else assume NOTEXTURE and run it again after everything else
2007-08-20 08:09:13 -07:00
ui . debug ( " not a valid PIE. Either a TEXTURE or NOTEXTURE directive is required " , " warning " )
gen = fresh_generator ( ui . getData ( ' filename ' ) )
2007-07-14 03:22:05 -07:00
texfilename = " "
2007-08-20 08:09:13 -07:00
ui . setData ( ' texpage-width ' , 256 , True )
ui . setData ( ' texpage-height ' , 256 , True )
2007-07-14 03:22:05 -07:00
else :
texfilename = i [ pie . TEXTURE_FILENAME ]
2007-08-20 08:09:13 -07:00
ui . setData ( ' texpage-width ' , i [ pie . TEXTURE_WIDTH ] , True )
ui . setData ( ' texpage-height ' , i [ pie . TEXTURE_HEIGHT ] , True )
2007-06-08 11:05:16 -07:00
2007-07-14 03:22:05 -07:00
basename , ext = os . path . splitext ( texfilename . lower ( ) )
namelen = len ( basename ) + len ( ext )
2007-08-20 08:09:13 -07:00
texpage_opts = ui . getData ( ' texpage-opts ' )
ui . debug ( ' basename: ' + basename )
ui . debug ( ' ext: ' + ext )
2007-07-26 13:53:20 -07:00
for d in ( ( ' .. ' , ' texpages ' ) , ( ' .. ' , ' .. ' , ' texpages ' ) , ( ' .. ' , ' .. ' , ' .. ' , ' texpages ' ) ) :
2007-08-20 08:09:13 -07:00
d = os . path . join ( ui . getData ( ' dir ' ) , * d )
2007-07-14 03:22:05 -07:00
if not os . path . exists ( d ) : continue
2007-08-20 08:09:13 -07:00
ui . setData ( ' texpage-dir ' , d )
2007-07-14 03:22:05 -07:00
for fn in os . listdir ( d ) :
fnlower = fn . lower ( )
if fnlower . startswith ( basename ) :
canonical = os . path . abspath ( os . path . join ( d , fn ) )
if fnlower . endswith ( ext ) and len ( fn ) == namelen :
2007-08-20 08:09:13 -07:00
texpage_opts . insert ( 0 , canonical )
2007-07-14 03:22:05 -07:00
else :
2007-08-20 08:09:13 -07:00
texpage_opts . append ( canonical )
ui . debug ( ' texpage options: ' + str ( ui . getData ( ' texpage-opts ' ) ) )
2007-07-14 03:22:05 -07:00
2007-08-20 08:09:13 -07:00
def texpage_draw ( ui ) :
2007-07-14 03:22:05 -07:00
Draw . PushButton ( " Other texpage " , 2 , 15 , 135 , 140 , 30 , " Select another texpage from your filesystem " )
Draw . PushButton ( " Cancel " , 1 , 15 , 95 , 140 , 30 , " Cancel the import operation " )
Draw . PushButton ( " Proceed " , 0 , 15 , 55 , 140 , 30 , " Confirm texpage selection and continue " )
2007-08-20 08:09:13 -07:00
options = ui . getData ( ' texpage-opts ' )
2007-07-14 03:22:05 -07:00
numopts = len ( options )
menustr = " Texpages % t "
menustr + = ' ' . join ( " | %s %% x %i " % ( options [ i ] , i ) for i in xrange ( numopts ) )
menustr + = " |NOTEXTURE %% x %i " % numopts
2007-08-20 08:09:13 -07:00
ui . setData ( ' texpage-menu ' , Draw . Menu ( menustr , 3 , 15 , 15 , 406 , 30 ,
ui . getData ( ' texpage-menu ' ) . val , " Select a texpage to bind to the model " ) )
menu = ui . getData ( ' texpage-menu ' )
2007-07-14 03:22:05 -07:00
if menu . val < numopts :
selected = options [ menu . val ]
selected = new_texpage ( selected )
w , h = selected . getSize ( )
Draw . Label ( " %i x %i at %i bpp " % ( w , h , selected . getDepth ( ) ) , 16 , 289 , 136 , 20 )
else :
Draw . Label ( " model will appear white " , 16 , 289 , 136 , 20 )
selected = None
BGL . glClearColor ( 0.7 , 0.7 , 0.7 , 1 )
BGL . glClear ( BGL . GL_COLOR_BUFFER_BIT )
BGL . glColor3f ( 0.8 , 0.8 , 0.8 )
BGL . glRecti ( 5 , 5 , 431 , 361 )
BGL . glColor3f ( 0.7 , 0.7 , 0.7 )
BGL . glRecti ( 15 , 241 , 155 , 311 )
BGL . glRecti ( 15 , 321 , 421 , 351 )
BGL . glColor3i ( 0 , 0 , 0 )
text = ( " Texpage Selection " , " large " )
BGL . glRasterPos2i ( int ( ( 406 - Draw . GetStringWidth ( * text ) ) / 2 + 15 ) , 332 )
Draw . Text ( * text )
thumbnailize ( selected , 165 , 55 , 256 )
2007-08-20 08:09:13 -07:00
def texpage_evt ( ui , val ) :
2007-07-14 03:22:05 -07:00
if 0 == val :
2007-08-20 08:09:13 -07:00
options = ui . getData ( ' texpage-opts ' )
texpage_cache = ui . getData ( ' texpage-cache ' )
menu = ui . getData ( ' texpage-menu ' )
2007-07-14 03:22:05 -07:00
msg = " selected texpage: "
currentMat = Material . New ( ' PIE_mat ' )
texture = Texture . New ( )
if menu . val < len ( options ) :
msg + = options [ menu . val ]
2007-08-20 08:09:13 -07:00
ui . debug ( " binding texture to texpage " )
2007-06-08 11:05:16 -07:00
texture . setType ( ' Image ' )
2007-07-14 03:22:05 -07:00
image = texpage_cache [ options [ menu . val ] ]
2007-06-08 11:05:16 -07:00
texture . image = image
2007-07-14 03:22:05 -07:00
else :
msg + = " NOTEXTURE "
texture . setType ( ' None ' )
image = None
2007-08-20 08:09:13 -07:00
ui . debug ( msg )
2007-07-14 03:22:05 -07:00
currentMat . setTexture ( 0 , texture , Texture . TexCo . UV , Texture . MapTo . COL )
2007-08-20 08:09:13 -07:00
ui . setData ( ' material ' , currentMat , True )
ui . setData ( ' image ' , image , True )
2007-07-14 03:22:05 -07:00
return True
if 1 == val :
return False
elif 2 == val :
2007-08-20 08:09:13 -07:00
Window . ImageSelector ( new_texpage , " Select a texpage " ,
ui . getData ( ' texpage-dir ' , ui . getData ( ' dir ' ) ) )
2007-07-14 03:22:05 -07:00
Draw . Redraw ( )
elif 3 == val :
Draw . Redraw ( )
2007-08-20 08:09:13 -07:00
def model_process ( ui ) :
2007-07-14 03:22:05 -07:00
i = seek_to_directive ( True , True , " LEVELS " )
numlevels = i [ pie . DIRECTIVE_VALUE ]
level , nbActualPoints , default_coords , mesh = 0 , 0 , None , None
2007-08-20 08:09:13 -07:00
scale = ui . getData ( ' import-scale ' )
if scale is not 1.0 :
ui . debug ( " scaling by a factor of %.1f " % scale )
point_divisor = 128.0 / scale
2007-07-14 03:22:05 -07:00
def new_point ( ) :
mesh . verts . extend ( * default_coords )
2007-08-20 08:09:13 -07:00
ui . debug ( " patching invalid point or point reference: new point is at ( %.1f , %.1f , %.1f ) " % \
2007-07-14 03:22:05 -07:00
tuple ( default_coords ) , " error " )
default_coords [ 1 ] - = 0.1
return nbActualPoints + 1
2007-08-20 08:09:13 -07:00
if ui . getData ( ' pie-version ' ) > = 5 :
2007-07-14 03:22:05 -07:00
divisorX , divisorY = 1 , 1
else :
2007-08-20 08:09:13 -07:00
divisorX = ui . getData ( ' texpage-width ' )
divisorY = ui . getData ( ' texpage-height ' )
2007-07-14 03:22:05 -07:00
scn = Scene . GetCurrent ( ) # link object to current scene
2007-08-20 08:09:13 -07:00
pieobj = scn . objects . new ( " Empty " , " PIE_ " + os . path . splitext (
os . path . basename ( ui . getData ( ' filename ' ) ) ) [ 0 ] . upper ( ) )
2007-07-14 03:22:05 -07:00
while level < numlevels :
i = seek_to_directive ( True , True , " LEVEL " )
level + = 1
if level != i [ pie . DIRECTIVE_VALUE ] :
2007-08-20 08:09:13 -07:00
ui . debug ( " LEVEL should have value of %i on line %i . reordered to %i " % \
2007-07-26 13:53:20 -07:00
( level , i [ pie . LINENO ] , level ) , " warning " )
mesh = Mesh . New ( ' clean ' )
2007-08-20 08:09:13 -07:00
mesh . materials + = [ ui . getData ( ' material ' ) ]
mesh . addUVLayer ( ' base ' )
mesh . addUVLayer ( ' teamcolor_meta ' )
2007-07-14 03:22:05 -07:00
i = seek_to_directive ( True , True , " POINTS " )
num , nbOfficialPoints , nbActualPoints = 0 , i [ pie . DIRECTIVE_VALUE ] , 0
default_coords = [ 1.0 , 0.5 , 0.0 ]
abandon_level = True
try :
while num < nbOfficialPoints :
i = gen . next ( )
if i [ pie . TOKEN_TYPE ] == " error " :
if isinstance ( i [ pie . ERROR ] , pie . PIESyntaxError ) and \
i [ pie . ERROR_ASSUMED_TYPE ] == " point " :
num + = 1
2007-08-20 08:09:13 -07:00
ui . debug ( " point no. %i is not valid. " % num , " error " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
nbActualPoints = new_point ( )
else :
2007-08-20 08:09:13 -07:00
ui . debug ( i [ pie . ERROR ] . args , " error " )
2007-07-14 03:22:05 -07:00
break
else :
num + = 1
x , y , z = i [ pie . FIRST : ]
#todo: convert to Mesh code
2007-07-26 13:53:20 -07:00
mesh . verts . extend ( - x / point_divisor , - z / point_divisor , y / point_divisor )
2007-07-14 03:22:05 -07:00
nbActualPoints + = 1
else :
abandon_level = False
if abandon_level :
2007-08-20 08:09:13 -07:00
ui . debug ( " remaining data in this LEVEL cannot be trusted. " , " error " )
2007-07-14 03:22:05 -07:00
continue
i = seek_to_directive ( True , True , " POLYGONS " )
num , numtotal = 0 , i [ pie . DIRECTIVE_VALUE ]
while num < numtotal :
i = gen . next ( )
force_valid_points = list ( )
if i [ pie . TOKEN_TYPE ] == " error " :
error = i [ pie . ERROR ]
if isinstance ( error , pie . PIESyntaxError ) and \
i [ pie . ERROR_ASSUMED_TYPE ] == " polygon " :
num + = 1
2007-08-20 08:09:13 -07:00
ui . debug ( " polygon no. %i is not valid. omitting " % num , " error " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
continue
else :
2007-08-20 08:09:13 -07:00
ui . debug ( str ( i [ pie . ERROR ] . args [ 0 ] ) , " error " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
if isinstance ( error , pie . PIEStructuralError ) :
i = gen . next ( )
if i [ pie . TOKEN_TYPE ] != " polygon " :
2007-08-20 08:09:13 -07:00
ui . debug ( " expected polygon data. abandoning this LEVEL " , " error " )
2007-07-14 03:22:05 -07:00
break
nbPoints , pos = i [ pie . FIRST + 1 ] , pie . FIRST + 2
points = i [ pos : pos + nbPoints ]
for p in ( nbPoints - p for p in xrange ( 1 , nbPoints ) ) :
if points . count ( points [ p ] ) > 1 :
i [ pos + p ] , nbActualPoints = nbActualPoints , new_point ( )
force_valid_points . append ( p )
else : break
if i [ pie . TOKEN_TYPE ] != " polygon " :
2007-08-20 08:09:13 -07:00
ui . debug ( " expected polygon data. abandoning this LEVEL " , " error " )
2007-07-14 03:22:05 -07:00
break
flags = i [ pie . FIRST ]
nbPoints = i [ pie . FIRST + 1 ]
pos = pie . FIRST + 2
points = i [ pos : pos + nbPoints ]
for p in xrange ( nbPoints ) :
if points [ p ] > = nbOfficialPoints and p not in force_valid_points :
points [ p ] , nbActualPoints = nbActualPoints , new_point ( )
mesh . faces . extend ( points , ignoreDups = True )
if not flags & 0x200 : continue
pos + = nbPoints
f = mesh . faces [ num ]
num + = 1
2007-06-08 11:05:16 -07:00
f . mat = 0
if flags & 0x4000 :
2007-08-20 08:09:13 -07:00
mesh . activeUVLayer = ' teamcolor_meta '
nbFrames = i [ pos ]
framedelay = i [ pos + 1 ]
2007-07-14 03:22:05 -07:00
width = i [ pos + 2 ]
height = i [ pos + 3 ]
pos + = 4
2007-08-20 08:09:13 -07:00
ui . debug ( ' max ' + str ( nbFrames ) )
ui . debug ( ' time ' + str ( framedelay ) )
ui . debug ( ' width ' + str ( width ) )
ui . debug ( ' height ' + str ( height ) )
if nbFrames < 1 :
ui . debug ( " maximum number of teamcolors/animation frames must be at least 1 " , " error " )
if nbFrames is 1 :
ui . debug ( " maximum number of teamcolors/animation frames should be greater than 1 " , " warning " )
width / = divisorX
height / = divisorY
f . uv = create_teamcolor_meta ( nbPoints , width , height , nbFrames , framedelay )
2007-07-14 03:22:05 -07:00
mesh . activeUVLayer = ' base '
2007-08-20 08:09:13 -07:00
f . image = ui . getData ( ' image ' )
if ui . getData ( ' pie-version ' ) > = 5 :
2007-07-26 13:53:20 -07:00
uv = [ Mathutils . Vector ( i [ pos ] , i [ pos + 1 ] ) for pos in range ( pos , pos + 2 * nbPoints , 2 ) ]
else :
uv = [ Mathutils . Vector ( i [ pos ] / divisorX , 1 - i [ pos + 1 ] / divisorY ) for pos in range ( pos , pos + 2 * nbPoints , 2 ) ]
2007-08-20 08:09:13 -07:00
ui . debug ( " UVs: " + repr ( uv ) )
2007-07-14 03:22:05 -07:00
f . uv = uv
2007-06-08 11:05:16 -07:00
if flags & 0x2000 :
# double sided
f . mode | = Mesh . FaceModes [ ' TWOSIDE ' ]
if flags & 0x800 :
# transparent
f . transp = Mesh . FaceTranspModes [ ' ALPHA ' ]
2007-07-14 03:22:05 -07:00
except StopIteration : pass
2007-07-26 13:53:20 -07:00
ob = scn . objects . new ( mesh , ' LEVEL_ %i ' % level )
mesh . flipNormals ( )
pieobj . makeParent ( [ ob ] , 0 , 0 )
2007-07-14 03:22:05 -07:00
i = seek_to_directive ( False , False , " CONNECTORS " )
if i is not None :
num , numtotal = 0 , i [ pie . DIRECTIVE_VALUE ]
while num < numtotal :
i = gen . next ( )
if i [ pie . TOKEN_TYPE ] == " error " :
if isinstance ( i [ pie . ERROR ] , pie . PIESyntaxError ) and \
i [ pie . ERROR_ASSUMED_TYPE ] == " connector " :
num + = 1
2007-08-20 08:09:13 -07:00
ui . debug ( " connector no. %i is not valid. omitting " % num , " error " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
continue
else :
2007-08-20 08:09:13 -07:00
ui . debug ( i [ pie . ERROR ] . args [ 0 ] , " error " , i [ pie . LINENO ] )
2007-07-14 03:22:05 -07:00
break
num + = 1
x , y , z = i [ pie . FIRST : ]
2007-07-26 13:53:20 -07:00
#empty = scn.objects.new('Empty', "CONNECTOR_%i" % num)
empty = scn . objects . new ( ' Empty ' , " CONNECTOR_ %i " % num )
empty . loc = x / point_divisor , - y / point_divisor , z / point_divisor
2007-07-14 03:22:05 -07:00
empty . setSize ( 0.15 , 0.15 , 0.15 )
2007-07-26 13:53:20 -07:00
pieobj . makeParent ( [ empty ] , 0 , 0 )
2007-08-20 08:09:13 -07:00
if ui . getData ( ' auto-layer ' ) :
ui . debug ( " layering all levels " )
Run ( ui . getData ( ' scripts/layers ' ) )
2007-07-26 13:53:20 -07:00
else :
Redraw ( )
2007-07-14 03:22:05 -07:00
return True
2007-06-08 11:05:16 -07:00
2007-07-14 03:22:05 -07:00
def load_pie ( filename ) :
2007-08-20 08:09:13 -07:00
global gen , ui_ref
ui = BeltFedUI ( True )
ui_ref = ui
ui . append ( opt_process , opt_draw , opt_evt )
ui . append ( texpage_process , texpage_draw , texpage_evt )
ui . append ( model_process )
2007-07-14 03:22:05 -07:00
gen = fresh_generator ( filename )
2007-08-20 08:09:13 -07:00
ui . setData ( ' dir ' , os . path . dirname ( filename ) , True )
ui . setData ( ' filename ' , filename , True )
ui . Run ( )
2007-07-14 03:22:05 -07:00
Window . FileSelector ( load_pie , ' Import Warzone model ' )