Mtor
Slim Ribbox Scripting 6


return to main index

Related Links
   Slim Ribbox Scripting 1
   Slim Ribbox Scripting 2
   Slim Ribbox Scripting 3
   Slim Ribbox Scripting 4
   Slim Ribbox Scripting 5


Introduction

The scripts on this page present a rough and ready way of saving xyz data for an animated particle system. As individual particles move within a scene their positions are saved into separate text (data) files. Particles emitted early in an animation have data files that record more xyz positions than particles generated later in the animation. The script assumes that particles are not deleted from the system.

At the end of the animation the data from the individual data files are consolidated into a single rib file. In effect the scripts shown on this page bake-out particle trajectories. In this example, the xys data is used to describe RenderMan curves. Figure 1 shows a bundle of flow lines (curves) formed by particles striking a sphere.


Breakdown

On the first frame of a particle animation the ribbox script, listing 1, deletes the data files from a previous baking run. This is necessary because the data in the (particle) text files is appended and as such we do not want "new" data combined with "old" data.

On the last frame of an animation the data previously saved into individual particle files is consolidated by an external TCL script, listing 2, into a single rib archive that describes a series of curves. The data could be re-purposed to describe almost anything - blobby flow lines for example.

Figure 2 shows a 30 frame animation of the curves rendered with the sparky shader - listing 3. The shader progressively "moves" a patch of opacity along otherwise transparent curves. Sparky assumes the start and end of each flow line spans an "age" of 0.0 to 1.0. Of course this is wrong because the shorter lines represent the trajectories of young particles. Thus, the very short curves may only span an age of 0.9 to 1.0.

It is left to the reader to figure out a technique whereby sparky can apply a "age" compensation to a curve it is shading. Hint: name each curve ie.

    Attribute "identifier" "name" ["a_number"]

with an integer that matches its number of data points. A shader can querry the name of the object to which it is "attached" ie.

    attribute("identifier:name", name)

Sparky should be able to use the number to apply a compensation factor to the age. Refer to Surface Names & Ray Names for more information.




figure 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 
20
21
22
23
24
25
26
27
28
[
source "PATH_TO_YOUR_ TCL_SCRIPTS/bakeParticleData.tcl"

if {$F == 1} {
    file delete -force M:/vsfx705/tmp
    file mkdir DATA_DIR_PATH
    }

set pName   [clientcmd "listRelatives -parent $OBJNAME"]
set pName   [lindex $pName 0]
set pNum    [clientcmd "particle -q -ct $pName"]
set ribout ""
for {set n 0} {$n < $pNum} {incr n} {
    set pnt [clientcmd "getParticleAttr -at position $OBJNAME.pt\[$n\]"]
    set x   [lindex $pnt 0]
    set y   [lindex $pnt 1]
    set z   [lindex $pnt 2]
    set fname "particle.[format %0.4d $n].txt"

    set datafile [open DATA_DIR_PATH/$fname a]
    puts $datafile "$x $y $z"
    close $datafile
    }
if {$F == $fn} {
    bakeParticleData "DATA_DIR_PATH" "RIB_PATH/particles.bake.rib" 0.07
    }   
return ""
]

ribbox listing 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 
20
21
22
23
24
25
26
27
28
proc bakeParticleData { datadir bakefile width} {
   file delete -force $bakefile   
   set ribfile [open $bakefile w]
   
   set listing [glob -directory $datadir *]
   foreach item $listing {
      set datafile [open $item r]
      set tmp ""
      set count 0
      while { [eof $datafile] != 1 } {
         gets $datafile line
         append tmp "$line\n"
         incr count
         }
      set count [expr $count - 1]
      if {$count >= 4} {
         puts $ribfile "Basis \"b-spline\" 1 \"b-spline\" 1"
         puts $ribfile "Curves \"cubic\" \[$count\] \"nonperiodic\""
         puts $ribfile "\"P\" \["
         puts $ribfile [string trim $tmp]
         puts $ribfile "\] \"constantwidth\" \[$width\]\n"
         }
      close $datafile
      }
   close $ribfile
   }

TCL listing 2


1
2
3
4
5
6
7
8
9
10
11
12
13
14
surface
sparky(float    Kfb = 1,
                age = 0,
                rearblur = 0.05,
                frontblur = 0.0;
        color   start = 1,
                end = color(0.749,0.823,0.980))
{
color    surfcolor = mix(start, end, age);

Oi = Os * smoothstep(age - rearblur, age, v) *
              (1 - smoothstep(age, age + frontblur, v));
Ci = Oi * Cs * surfcolor * Kfb;
}

RSL listing 3





© 2002-6 Malcolm Kesson. All rights reserved.