<s> import sys , os , argparse , glob <EOL> import xml . etree . ElementTree as ET <EOL> import ResizeInfo , ExportOptions <EOL> from klei import util <EOL> import subprocess <EOL> SCRIPT_DIR = os . path . dirname ( __file__ ) <EOL> sys . path . append ( SCRIPT_DIR ) <EOL> class Asset : <EOL> def __init__ ( self , filename , asset_type ) : <EOL> self . File = filename <EOL> self . Type = asset_type <EOL> class Prefab : <EOL> def __init__ ( self , name , assets = [ ] ) : <EOL> self . Name = name <EOL> self . Assets = assets <EOL> class Prefabs : <EOL> def __init__ ( self , filename ) : <EOL> self . Prefabs = self . ParsePrefabs ( filename ) <EOL> def GetAssetType ( self , asset_type ) : <EOL> asset_type = asset_type . upper ( ) <EOL> assets = [ ] <EOL> for prefab in self . Prefabs : <EOL> for asset in prefab . Assets : <EOL> if asset . Type == asset_type : <EOL> assets . append ( asset ) <EOL> return assets <EOL> def ParsePrefabs ( self , filename ) : <EOL> tree = ET . parse ( filename ) <EOL> prefabs = tree . findall ( "<STR_LIT>" ) <EOL> prefab_list = [ ] <EOL> for prefab in prefabs : <EOL> name = prefab . attrib [ '<STR_LIT:name>' ] <EOL> assets = [ Asset ( asset . attrib [ '<STR_LIT:file>' ] , asset . attrib [ '<STR_LIT:type>' ] . upper ( ) ) for asset in prefab . findall ( "<STR_LIT>" ) ] <EOL> prefab_list . append ( Prefab ( name , assets ) ) <EOL> return prefab_list <EOL> def GetOwner ( self , asset_filename ) : <EOL> for prefab in self . Prefabs : <EOL> for asset in prefab . Assets : <EOL> if os . path . basename ( asset . File ) == asset_filename : <EOL> return prefab <EOL> return None <EOL> def ConvertAnim ( params ) : <EOL> anim , prefabs , args = params <EOL> anim_path = anim <EOL> anim = os . path . basename ( anim . strip ( ) ) <EOL> prefab_owner = prefabs . GetOwner ( anim ) <EOL> scale = ResizeInfo . DefaultSize ( ) <EOL> if prefab_owner : <EOL> scale = ResizeInfo . SizeMap [ prefab_owner . Name ] <EOL> anim_filename = os . path . basename ( anim ) <EOL> anim_name , ext = os . path . splitext ( anim_filename ) <EOL> keys_to_try = [ anim , anim_filename , anim_name ] <EOL> for key in keys_to_try : <EOL> scale = ResizeInfo . SizeMap . get ( key , scale ) <EOL> additional_options = ExportOptions . Options . get ( anim_name , [ ] ) <EOL> cmd_args = [ <EOL> os . path . join ( SCRIPT_DIR , r"<STR_LIT>" ) , <EOL> os . path . join ( SCRIPT_DIR , r"<STR_LIT>" ) , <EOL> "<STR_LIT>" + str ( scale ) <EOL> ] <EOL> additional_args = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for arg in additional_args : <EOL> try : <EOL> val = getattr ( args , arg ) <EOL> cmd_args . append ( '<STR_LIT>' . format ( arg , val ) ) <EOL> except : <EOL> pass <EOL> additional_flags = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for flag in additional_flags : <EOL> try : <EOL> val = getattr ( args , flag ) <EOL> if val : <EOL> cmd_args . append ( '<STR_LIT>' + flag ) <EOL> except : <EOL> pass <EOL> os . chdir ( SCRIPT_DIR ) <EOL> src_file = anim_path <EOL> anim_dir = os . path . normpath ( os . path . join ( "<STR_LIT:..>" , args . outputdir , "<STR_LIT>" ) ) <EOL> if not os . path . exists ( anim_dir ) : <EOL> os . makedirs ( anim_dir ) <EOL> dest_file = os . path . join ( anim_dir , anim ) <EOL> if args . force or util . IsFileNewer ( src_file , dest_file ) : <EOL> cmd_args += additional_options <EOL> cmd_args . append ( src_file ) <EOL> cmd_args . append ( '<STR_LIT>' + args . outputdir ) <EOL> print ( '<STR_LIT>' + anim ) <EOL> return subprocess . call ( cmd_args ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> CWD = os . getcwd ( ) <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , nargs = '<STR_LIT:?>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT:data>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> if len ( args . prefabsdir ) == <NUM_LIT:0> : <EOL> args . prefabsdir = os . path . join ( SCRIPT_DIR , "<STR_LIT:..>" , args . outputdir ) <EOL> prefabs_file = os . path . abspath ( os . path . normpath ( os . path . join ( CWD , args . prefabsdir , "<STR_LIT>" ) ) ) <EOL> prefabs = Prefabs ( prefabs_file ) <EOL> anims = [ ] <EOL> if args . anims : <EOL> anims = [ args . anims ] <EOL> else : <EOL> anims = [ os . path . basename ( anim . File ) for anim in prefabs . GetAssetType ( "<STR_LIT>" ) ] <EOL> anims = sorted ( set ( anims ) ) <EOL> anims = [ ( anim , prefabs , args ) for anim in anims ] <EOL> from multiprocessing import Pool <EOL> pool = Pool ( processes = <NUM_LIT:1> ) <EOL> pool . map ( ConvertAnim , anims ) <EOL> if not args . skip_update_prefabs : <EOL> os . chdir ( os . path . join ( SCRIPT_DIR , "<STR_LIT:..>" ) ) <EOL> os . system ( '<STR_LIT:U+0020>' . join ( ( '<STR_LIT>' , args . outputdir ) ) ) <EOL> os . chdir ( CWD ) </s>
