Lesson 7 : Write scripts in Jerboa

 

You should now be able to write a good variety of rules so you’re going to learn how to string them together in a script. You will first learn how to call upon different rules in a script and then you will discover loops and alternatives in the Jerboa script language.

 

APPLYING RULES IN A SCRIPT

There is a precise syntax to call upon rules. These rules must be in the same modeler as the script, the folders in which the rules are sorted are not taken into account in scripts.

Here is the syntax to apply a rule:

@rule<[NameOfTheRule]>([TopologicalHooks],[EmbeddingParameters]);

Now you will create a script which builds a cube from scratch.

  • open the editor and create a new script (the script button is next to the rule button) called MyCube

You will notice that the layout is slightly different, there are no longer left and right patterns but only one window at the top in which you can write commands in the Jerboa script language.

You will start by creating a free dart which you will then extrude. To use the extrusion rules you will need to give the created dart as a parameter by making the result of the CreateFreeVertex rule a « JerboaRuleResult » and then giving the first dart of the JerboaRuleResult list as a parameter to the following rules.

  • create a JerboaRuleResult called « res0 » (see Screen 7.1) and give it the result of CreateFreeVertex which will have two parameters (a new instance of Points and the « false » boolean to indicate that it should not ask the user for coordinates, remember that from the last lesson)
Screen 7.1: MyCube

You will now use vectors of the « Vectors.java » class which must be imported in the « Header » window under « Views » using these lines:

@header(java){
import jerboatutorial1.Vectors;
}

(see Screen 7.2)

Screen 7.2: header

You can use the Vectors constructor « public Vectors(float x, float y, float z) » to create the parameters for the extrusion rules.

  • extrude the dart to create an edge
  • extrude the edge to create a square face
  • extrude the face to create a cubic volume
  • create a second JerboaRuleResult called res1 which will be the result of the last operation
  • return res1

To keep things simple, make sure to extrude with a vector along all three different axes (x, y and z).

At the end of a script you must return something which could be the result of an operation in the form of a list of darts or you can return null if you don’t want your script to return anything.

Save, export and try your script by double clicking on it in the editor without having selected any darts.

LOOPS IN A SCRIPT

There are different loops you can use in scripts, these are the main java loops (for, foreach, switch/case, while, etc.) just like in rules.

You will create a script which cuts a volume in two.

  • open the editor and create a new script called « CutInHalf » (see Screen 7.4)
  • open the « graphs » window at the bottom of the screen and place a node in the left pattern and make it a hook (see Screen 7.3)
Screen 7.3: graphs
  • create a loop on one dart of each vertex (1) of a face (0,1) starting from the hook (n0#0)
  • place the SubdivideEdge3 rule in that loop, the dart to which this rule must be applied is reached through 2-links and then 1-links from each dart on which the rule is looped

You may remember from lesson 4 that the foreach loop which you will need to use has the following syntax:

for ( [type] [variable] : [condition] ) {

[instruction]

};

You may also remember from the same lesson that accessing a neighboring dart through an n-link is done using the « @n » syntax.

  • create a loop on one dart of each edge (0) of a face (0,1) starting from the hook (n0#0)
  • place the « AddInsideEdge » rule in that loop, the first dart to which this rule must be applied are reached through 2-links, then 1-links, then 0-links from each dart on which the rule is looped, the second is reached through 0,2,1 and 0-links

The AddInsideEdge rule is provided to you and you can discover it in the editor.

  • create a loop on one dart of each vertex (1) of a face (0,1) starting from the hook (n0#0)
  • place the Sew1 rule in that loop, the first dart to which this rule must be applied are reached through 2-links, then 1-links, then 0-links, then 1-links, then 2-links from each dart on which the rule is looped, the second is reached through 2,1,0,2,1 and 2-links
  • return null because this script doesn’t need to return anything
Screen 7.4: CutInHalf
  • try your new script on the cube you created with the previous script
Screen 7.5: before operation
Screen 7.6: after operation

ALTERNATIVES IN A SCRIPT

You will create a script which sweeps an object and applies either of two rules depending on the shape of each face. You will use the precondition of TriangulateFace to triangulate only the non triangular faces, if the precondition is not met (if the face is a triangle) then (and only then) the following rule will be applied, this is done by separating the rules by a « | ».

  • open the editor and create a new rule called « Alternatives » (see Screen 7.7)
  • go to the graphs window and create a hook like in the previous script
  • place the TriangulateFace and MakeRed rules in a loop on each face (0,1,3) of the connected component (0,1,2,3) staring from the hook (n0#0)
Screen 7.7: Alternatives
  • try the rule on the IcositeHexagon which has square and triangular faces
  • apply the rule a second time
Screen 7.8: before application
Screen 7.9: after first operation
Screen 7.10: after second operation

What have you learned?

  • What type of loop did you use in the CutInHalf script?
  • Why does every face turn red the second time you apply the Alternatives script to any object?
  • Which rules (which you have written) could you use in the MyCube script to reduce the number of extrusions necessary?