void Unreal3DExport::Init()
{
    // Init
    CheckCancel();
    pScene = GetIGameInterface();
    GetConversionManager()->SetUserCoordSystem(UnrealCoords);
    if( bExportSelected )
    {
        Tab<INode*> selnodes;;
        for( int i=0; i<pInt->GetSelNodeCount(); ++i )
        {
            INode* n = pInt->GetSelNode(i);
            selnodes.Append(1,&n);
        }
        if( !pScene->InitialiseIGame(selnodes,false)  )
            throw MAXException(GetString(IDS_ERR_IGAME));
    }
    else
    {
        if( !pScene->InitialiseIGame() )
            throw MAXException(GetString(IDS_ERR_IGAME));
    }


    // Enumerate scene
    NodeCount = pScene->GetTotalNodeCount();
    for( int i=0; i<pScene->GetTopLevelNodeCount(); ++i )
    {
        IGameNode * n = pScene->GetTopLevelNode(i);
        ExportNode(n);
    }
    Progress += U3D_PROGRESS_ENUM;


    // Get animation info
    FrameStart = pScene->GetSceneStartTime() / pScene->GetSceneTicks();
    FrameEnd = pScene->GetSceneEndTime() / pScene->GetSceneTicks();
    FrameCount = FrameEnd - FrameStart+1;
    if( FrameCount <= 0 || FrameEnd < FrameStart ) 
    {
        ProgressMsg.printf(GetString(IDS_ERR_FRAMERANGE),FrameStart,FrameEnd);
        throw MAXException(ProgressMsg.data());
    }
    pScene->SetStaticFrame(FrameStart);
}
void Unreal3DExport::WriteScript()
{
    // Write script file
    {

        fScript = _tfopen(ScriptFileName,_T("wb"));
        if( !fScript )
        {
            ProgressMsg.printf(GetString(IDS_ERR_FSCRIPT),ScriptFileName);
            throw MAXException(ProgressMsg.data());
        }

        TSTR buf;


        // Write class def     
        _ftprintf( fScript, _T("class %s extends Object;\n\n"), FileName ); 

        // write import
        _ftprintf( fScript, _T("#exec MESH IMPORT MESH=%s ANIVFILE=%s_a.3D DATAFILE=%s_d.3D \n"), FileName, FileName, FileName ); 
        
        // write origin & rotation
        // TODO: figure out why it's incorrect without -1
        Point3 porg = OptOffset * OptScale * -1; 
        _ftprintf( fScript, _T("#exec MESH ORIGIN MESH=%s X=%f Y=%f Z=%f PITCH=%d YAW=%d ROLL=%d \n"), FileName, porg.x, porg.y, porg.z, (int)OptRot.x, (int)OptRot.y, (int)OptRot.z ); 
        
        // write mesh scale
        Point3 psc( Point3(1.0f/OptScale.x,1.0f/OptScale.y,1.0f/OptScale.z));
        _ftprintf( fScript, _T("#exec MESH SCALE MESH=%s X=%f Y=%f Z=%f \n"), FileName, psc.x, psc.y, psc.z ); 
        
        // write meshmap
        _ftprintf( fScript, _T("#exec MESHMAP NEW MESHMA=P%s MESH=%smap \n"), FileName, FileName ); 
        
        // write meshmap scale
        _ftprintf( fScript, _T("#exec MESHMAP SCALE MESHMAP=%s X=%f Y=%f Z=%f \n"), FileName, psc.x, psc.y, psc.z ); 

        // write sequence
        _ftprintf( fScript, _T("#exec MESH SEQUENCE MESH=%s SEQ=%s STARTFRAME=%d NUMFRAMES=%d \n"), FileName, _T("All"), 0, FrameCount-1 ); 

        // Get World NoteTrack
        ReferenceTarget *rtscene = pInt->GetScenePointer();
        for( int t=0; t<rtscene->NumNoteTracks(); ++t )
        {
            DefNoteTrack* notetrack = static_cast<DefNoteTrack*>(rtscene->GetNoteTrack(t));
            for( int k=0; k<notetrack->keys.Count(); ++k )
            {


                NoteKey* notekey = notetrack->keys[k];                        
                TSTR text = notekey->note;
                int notetime = notekey->time / pScene->GetSceneTicks();

                while( !text.isNull() )
                {
                    TSTR cmd = SplitStr(text,_T('\n'));
                    
                    if( MatchPattern(cmd,TSTR(_T("a *")),TRUE) )
                    {
                        SplitStr(cmd,_T(' '));
                        TSTR seq = SplitStr(cmd,_T(' '));
                        int end = _ttoi(SplitStr(cmd,_T(' ')));;
                        TSTR rate = SplitStr(cmd,_T(' '));
                        TSTR group = SplitStr(cmd,_T(' '));

                        if( seq.isNull() )
                        {
                            ProgressMsg.printf(_T("Missing animation name in notekey #%d"),notetime);
                            throw MAXException(ProgressMsg.data());
                        }
                        
                        if( end <= notetime )
                        {
                            ProgressMsg.printf(_T("Invalid animation endframe (%d) in notekey #%d"),end,notetime);
                            throw MAXException(ProgressMsg.data());
                        }

                        int startframe = notetime;
                        int numframes = end - notetime;

                        buf.printf( _T("#exec MESH SEQUENCE MESH=%s SEQ=%s STARTFRAME=%d NUMFRAMES=%d"), FileName, seq, notetime, numframes );

                        if( !rate.isNull() )
                            buf.printf( _T("%s RATE=%f"), buf, rate );
                        
                        if( !group.isNull() )
                            buf.printf( _T("%s GROUP=%f"), buf, rate );
                        
                        SeqName = seq;
                        SeqFrame = startframe;

                        buf.printf( _T("%s \n"), buf );
                        _fputts( buf, fScript ); 

                    }
                    else if( MatchPattern(cmd,TSTR(_T("n *")),TRUE) )
                    {
                        SplitStr(cmd,_T(' '));
                        TSTR func = SplitStr(cmd,_T(' '));
                        TSTR time = SplitStr(cmd,_T(' '));
                        
                        if( func.isNull() )
                        {
                            ProgressMsg.printf(_T("Missing notify name in notekey #%d"),notetime);
                            throw MAXException(ProgressMsg.data());
                        }

                        if(  time.isNull() )
                        {
                            ProgressMsg.printf(_T("Missing notify time in notekey #%d"),notetime);
                            throw MAXException(ProgressMsg.data());
                        }

                        buf.printf( _T("#exec MESH NOTIFY MESH=%s SEQ=%s TIME=%s FUNCTION=%s"), FileName, SeqName, time, func );
                        
                        _fputts( buf, fScript ); 
                    }




                }
            }
        }


        // TODO: write materials
        if( Materials.Count() > 0 )
        {
            for( int i=0; i<Materials.Count(); ++i )
            {
                IGameMaterial* mat = Materials[i].Mat;
                if( mat == NULL )
                    continue;

                _ftprintf( fScript, _T("#exec MESHMAP SETTEXTURE MESHMAP=%s NUM=%d TEXTURE=%s \n"), FileName, i, _T("DefaultTexture") ); 
            }
        }

    }
}