Difference between revisions of "Amira.hx"

From CRIPTWiki
Jump to: navigation, search
(Created page with '<nowiki># Amira Script ################################################################### # This script sets up the amira demos. # It looks for existing demos and generates a ...')
 
(No difference)

Latest revision as of 19:45, 17 July 2009

# Amira Script ################################################################### # This script sets up the amira demos. # It looks for existing demos and generates a new index.html page # containing only the required links and descriptions. # NOTE that you should have write permissions in DemoScripts/ and # html/ ################################################################### # authors: # Patrick Barthelemy (patrick@tgs.com) # Hartmut Schirmacher (hartmut@tgs.com) ################################################################### ################################################################### # configuration section ################################################################### load $SCRIPTDIR/DemosConfig.hx if {$USE_LOADING_LOGO} { viewer 0 setBackgroundImage $SCRIPTDIR/DemoShared/$LOADING_LOGO } viewer 0 setBackgroundMode $BACKGROUND_MODE viewer 0 setBackgroundColor $BACKGROUND_COLOR viewer 0 setBackgroundColor2 $BACKGROUND_COLOR2 # procedure source_fixed for avoiding "saved network" problems load $SCRIPTDIR/DemoShared/hxfix.hx # no mercy for existing modules remove -All set allModulesList [all]; for {set i 0} {$i < [llength $allModulesList]} {incr i} { set currentModule [lindex $allModulesList $i]; remove $currentModule } # load a module that prevents other scripts from modifying the viewer #set hideNewModules 0 #create HxScriptObject {ViewerCommandsLock} #{ViewerCommandsLock} script setValue ${AMIRA_ROOT}/share/script-objects/LockViewerCommands.scro #ViewerCommandsLock setIconPosition 1 1 #ViewerCommandsLock fire #ViewerCommandsLock lock setValue 0 1 #ViewerCommandsLock lock setValue 1 1 #ViewerCommandsLock lock setValue 2 0 #ViewerCommandsLock lock setValue 3 0 #ViewerCommandsLock lock setValue 4 0 #ViewerCommandsLock lock setValue 5 0 #ViewerCommandsLock lock2 setValue 5 1 #ViewerCommandsLock fire # read file or return "" proc readFile {filename} { if [file readable $filename] { set content "" set f [open $filename] while {![eof $f]} { lappend content [gets $f] } close $f return $content } else { echo "could not read $filename" return "" } } ################################################################### # read back description file to retrieve list of scripts, and VR menu information. ################################################################### set description "" set description [readFile $SCRIPTDIR/DemosList.txt] set ALL_DEMOS "" set VR_MAIN_MENU "" set VR_ITEM_MENU "" for {set i 0} {$i < [llength $description]} {incr i} { set currententry [lindex $description $i] set currententry [string trim $currententry] if {[string length $currententry] > 0 && [string index $currententry 0] != "#"} { lappend ALL_DEMOS [lindex [split $currententry |] 0] lappend VR_MAIN_MENU [lindex [split $currententry |] 1] lappend VR_ITEM_MENU [lindex [split $currententry |] 2] } } ################################################################### # find out which demos actually exist in the DemoScripts directory ################################################################### set existing_demos [list] set existing_vrmainmenu [list] set existing_vritemmenu [list] set i 0 foreach demo $ALL_DEMOS { if [file readable $SCRIPTDIR/DemoScripts/$demo] { lappend existing_demos $demo lappend existing_vrmainmenu [lindex $VR_MAIN_MENU $i] lappend existing_vritemmenu [lindex $VR_ITEM_MENU $i] } else { echo "Cannot include $demo" } incr i } set ALL_DEMOS $existing_demos set VR_MAIN_MENU $existing_vrmainmenu set VR_ITEM_MENU $existing_vritemmenu set N_DEMOS [llength $ALL_DEMOS] set I_DEMOS -1 set AMIRA_DEMO_ROOT $SCRIPTDIR ################################################################### # generate a wrapped version of each .hx demo script that # includes the demo footer and the demo header. ################################################################### foreach demo $ALL_DEMOS { set f [open $SCRIPTDIR/DemoTmp/wrap_$demo w] puts $f "set CURRENT_SCRIPTFILE \"$SCRIPTDIR/DemoScripts/$demo\"" puts $f "source \"$SCRIPTDIR/DemoShared/header.hx\"" puts $f "source_fixed \"$SCRIPTDIR/DemoScripts/$demo\"" puts $f "source \"$SCRIPTDIR/DemoShared/footer.hx\"" close $f } ################################################################### # generate an HTML index page that only shows the existing demos # as well as help page ################################################################### if [file writable $SCRIPTDIR/DemoTmp] { echo "generating index.html and help.html" set pageIndex "<html><head><title>Amira demos</title></head> <body><p align=\"center\"><br><font size=\"5\"><a href=\"../DemoLoops/loop_all.hx\">Full Loop</a> <a href=\"../DemoShared/logo.hx\"><img src=\"../DemoShared/title.png\" width=\"75\" height=\"43\"></a> <a href=\"help.html\">HELP<br></a></font> %DESCRIPTION% <br><font size=\"5\"> %LOOP% <br><a href=\"../DemoShared/quit.hx\">QUIT</a></font></p></body></html>" set pageHelp "<html><head><title>Amira</title></head><body><p><div align=\"left\"><table border=\"1\">%DESCRIPTION%</table></div></body></html>" # for each existing demo, paste description into help and icon into index set nIcons 0 set curIndex 0 set lastMainVRMenu "" set isBR 1 foreach demo $ALL_DEMOS { set path "$SCRIPTDIR/DemoScripts/[file rootname $demo]-files" if {![string equal $lastMainVRMenu [lindex $VR_MAIN_MENU $curIndex]]} { if {$isBR} { regsub "%DESCRIPTION%" $pageIndex "<i><b><u><font color=\"#FF0000\" size=\"5\">[lindex $VR_MAIN_MENU $curIndex]</font></u></b></i><br>\n%DESCRIPTION%" pageIndex } else { regsub "%DESCRIPTION%" $pageIndex "<br><i><b><u><font color=\"#FF0000\" size=\"5\">[lindex $VR_MAIN_MENU $curIndex]</font></u></b></i><br>\n%DESCRIPTION%" pageIndex } set nIcons 0 } set lastMainVRMenu [lindex $VR_MAIN_MENU $curIndex] incr curIndex set descr "<a href=\"../DemoTmp/wrap_[file rootname $demo].hx\"><img src=\"../DemoScripts/[file rootname $demo]-files/icon.png\" width=\"75\" height=\"75\"></a>" regsub "%DESCRIPTION%" $pageIndex "$descr\n%DESCRIPTION%" pageIndex incr nIcons if {$nIcons == 7} { set nIcons 0 regsub "%DESCRIPTION%" $pageIndex "<br>\n%DESCRIPTION%" pageIndex set isBR 1 } else { set isBR 0 } set descr [readFile $path/description.txt] set descr "<tr> <td valign=\"middle\" align=\"center\"> <a href=\"../DemoTmp/wrap_[file rootname $demo].hx\"><img border=\"0\" src=\"../DemoScripts/[file rootname $demo]-files/icon.png\" width=\"75\" height=\"75\"></a> </td> <td>$descr</td> </tr>" regsub "%DESCRIPTION%" $pageHelp "$descr\n%DESCRIPTION%" pageHelp echo "including $demo" } # for each existing loop, paste a link into the skeleton set loop_files [glob -nocomplain "$SCRIPTDIR/DemoLoops/loop_*.hx"] foreach loop_file $loop_files { set txt [readFile $loop_file] set descr [file tail $loop_file] # extract description from loop file regexp "# *DESCRIPTION *\"\(\[^\"\]*\)\"" $txt dummy descr # paste into skeleton file set line "<a href=\"$loop_file\">$descr</a><br>" regsub -all "%LOOP%" $pageIndex "${line}\n%LOOP%" pageIndex echo "including loop [file tail $loop_file]" } # remove remaining markers regsub -all "%DESCRIPTION%" $pageIndex "" pageIndex regsub -all "%LOOP%" $pageIndex "" pageIndex regsub -all "%DESCRIPTION%" $pageHelp "" pageHelp # write new index.html set f [open $SCRIPTDIR/DemoTmp/index.html w] puts $f $pageIndex close $f # write new help.html set f [open $SCRIPTDIR/DemoTmp/help.html w] puts $f $pageHelp close $f } else { echo "$SCRIPTDIR/DemoTmp not writable - skipping index generation." } ################################################################### # enable tracking (if desired) and create 3D menu ################################################################### if {$AMIRAVR} { ################################################################### # activate VR ################################################################### create HxTracking AmiraVR AmiraVR setIconPosition 0 0 AmiraVR select AmiraVR stereo setValue 0 $STEREO set numConfig [AmiraVR config getNumOptEntries] for {set i 0} {$i < $numConfig} {incr i} { if {[AmiraVR config getOptLabel $i] == $AMIRAVR_FILE} break; } if {$i < $numConfig} { AmiraVR config setOptValue $i echo "$AMIRAVR_FILE successfully loaded." } else { echo "Cannot load $AMIRAVR_FILE... Load configuration file manually." } AmiraVR fire AmiraVR options setValue 2 $AMIRAVRMENU AmiraVR fire AmiraVR options setValue 2 1 AmiraVR action setValue 0 0 AmiraVR fire ################################################################### # generate script for 3D VR menu ################################################################### if {[exists AmiraVR] && [file writable $SCRIPTDIR/DemoTmp]} { echo "Creating AmiraVR 3D menu interface" set pageVRMenu "# Amira Script menu reset" set existingMenu [list] set i 0 set curMenuIndex 0 foreach demo $ALL_DEMOS { set mainMenu [lindex $VR_MAIN_MENU $i] set index [lsearch -exact $existingMenu $mainMenu] if { $index == -1} { set index $curMenuIndex lappend existingMenu $mainMenu set curMenuItem($index) -1 set pageVRMenu "$pageVRMenu menu insertMenu -id $curMenuIndex -text \"$mainMenu\"" incr curMenuIndex } incr curMenuItem($index) set pageVRMenu "$pageVRMenu menu $index insertItem -id $curMenuItem($index) -text \"[lindex $VR_ITEM_MENU $i]\" -proc \"Menu$index\"" incr i } set i 0 for {set j 0} {$j < $curMenuIndex} {incr j} { set pageVRMenu "$pageVRMenu proc Menu$j \{ id \} \{ global AMIRAVR AMIRAVRMENU AMIRAVR_FILE STEREO STEREO_MODE DISPLAY_LOGO LOOP_STEP FULLSCREEN USE_LOADING_LOGO LOOP_MODE BACKGROUND_MODE SLICES_VISIBLE global BACKGROUND_COLOR BACKGROUND_COLOR2 SHOWHTML TEXTURE2D3D VOLTEX_SLICES USE_PALETTED_TEXTURE AMIRA_DEMO_ROOT LENGTH_CAMERA_PATH CAMERA_PATH_OBJECT switch \$id \\" for {set k 0} {$k < $curMenuItem($j)} {incr k} { set scriptName [lindex $ALL_DEMOS $i] set pageVRMenu "$pageVRMenu $k \{ uplevel \\#0 load \$\{AMIRA_DEMO_ROOT\}/DemoTmp/wrap_$scriptName \} \\" incr i } set scriptName [lindex $ALL_DEMOS $i] incr i set pageVRMenu "$pageVRMenu $k \{ uplevel \\#0 load \$\{AMIRA_DEMO_ROOT\}/DemoTmp/wrap_$scriptName \} \}" } set f [open $SCRIPTDIR/DemoTmp/VRmenu.hx w] puts $f $pageVRMenu close $f load $AMIRA_DEMO_ROOT/DemoTmp/VRmenu.hx } } ################################################################### # launch HTML page ################################################################### help html load $AMIRA_DEMO_ROOT/DemoTmp/index.html ;# content.html html reload ;# just in case, to overcome caching html setSize 735 950 if {$SHOWHTML} { html show } else { html hide } proc onKeyF10 { } { global AMIRA_DEMO_ROOT SCRIPTFILE SCRIPTDIR STEREO STEREO_MODE FULLSCREEN AMIRAVR AMIRAVR_FILE LOOP_STEP DISPLAY_LOGO SLICES_VISIBLE global USE_LOADING_LOGO BACKGROUND_MODE USE_PALETTED_TEXTURE html load $AMIRA_DEMO_ROOT/DemoTmp/index.html html show } proc onKeyCtrlF10 { } { html show } proc onKeyCtrlF2 { } { global AMIRA_DEMO_ROOT I_DEMOS N_DEMOS ALL_DEMOS set I_DEMOS [ expr $I_DEMOS-1 ] if { $I_DEMOS < 0 } { set I_DEMOS [expr $N_DEMOS - 1]; } set currentScript [lindex $ALL_DEMOS $I_DEMOS] uplevel \#0 load \"${AMIRA_DEMO_ROOT}/DemoTmp/wrap_${currentScript}\"; } proc onKeyCtrlF3 { } { global AMIRA_DEMO_ROOT I_DEMOS N_DEMOS ALL_DEMOS set I_DEMOS [ expr ($I_DEMOS+1)%$N_DEMOS ]; set currentScript [lindex $ALL_DEMOS $I_DEMOS]; uplevel \#0 load \"${AMIRA_DEMO_ROOT}/DemoTmp/wrap_${currentScript}\"; } # subroutine for linearly animating a vector of parameters # (e.g. slice number, position) # from and to are vectors of arbitrary length, # cmd is Tcl code with %0, %1, %2 referencing the # interpolated parameters proc interpol {from to num_steps cmd} { set channels [llength $from] for {set i 0} {$i < $num_steps} {incr i} { set thecmd $cmd for {set ch 0} {$ch < $channels} {incr ch} { # compute current value if { ($i == ($num_steps -1)) || ($num_steps < 2) } { set val [lindex $to $ch] } else { set val [expr [lindex $from $ch] + \ ([lindex $to $ch] - [lindex $from $ch]) * \ (double($i)/double($num_steps-1))] } # replace the current value into "%<ch>" in the command regsub -all "%$ch" $thecmd $val thecmd } # execute the command with the substituted values eval $thecmd } } ################################################################### # some user output ################################################################### echo "**********************************************************" if {$DISPLAY_LOGO} { echo "DISPLAY_LOGO is ON" } else { echo "DISPLAY_LOGO is OFF" } echo "LOOP_STEP is $LOOP_STEP" if {$STEREO} { echo "STEREO is ON and will be set with $STEREO_MODE" } else { echo "STEREO is OFF" } if {$AMIRAVR} { echo "AMIRAVR is ON" echo "Will use configuration $AMIRAVR_FILE" if {$AMIRAVRMENU} { echo "3D menu interface is activated." } else { echo "3D menu interface is deactivated." } } else { echo "AMIRAVR is OFF" } echo "**********************************************************" echo "Hit F10 for index page, Ctrl+F10 to raise browser" echo "Hit Ctrl+F2 for previous demo, Ctrl+F3 for next demo" if {$LOOP_MODE} { catch { load $SCRIPTDIR/DemoScripts/Loop.hx } result }