Home

Menu
Home
Articles
Downloads
AoI Extensions Catalog
Portal Blog
Forum
Links
AoI Wiki
AoI Homepage
About Art of Illusion
Popular Downloads
Live Translation
Login Form





Lost Password?
The Cornell Box
Name:

Message:

Polls
What do you think about the Spline Mesh toolbar button? Do you use it?
 
Which area of AoI needs to be made more convenient?
 
Syndicate
FS AoI Portal
Who's Online
We have 2 guests online
Latest Forum Posts
    Scripting Tutorial - Part II
    User Rating: / 2
    PoorBest 
    Written by Francois Guillet   
    Wednesday, 27 June 2007
    This article is the second in the series that introduce scripting for Art of Illusion.

    After all the efforts made to write your first script, let's review the code so as to make sure you fully understand the notions behind the program.

    When the script starts, all you have access to is a variable called window. This object is an instance of the class LayoutWindow. It is your only entry point to AoI internals. A LayoutWindow object is an editing window with edits a scene, or, rather, a Scene. A scene holds all the 3D objects you put in AoI. So obviously adding an object or modifying an existing one needs access to the currently edited scene. Fortunately, the LayoutWindow class provides a method called getScene() which returns the Scene object associated to the window. In a similar manner, we ask the Scene instance which objects are currently selected. Any object in the scene is of type (understand is of Class) Object3D, embedded in an ObjectInfo instance which holds various information such as how the object is oriented in the scene.

    We check how many selected objects there are and stop for any other value than one. We access the object field of the ObjectInfo instance and check that its type is indeed a Curve. Hey, wait a minute! the object field of the ObjectInfo class is an Object3D, not a Curve?! Well, thanks to inheritance, a Curve is indeed an Object3D. When class are designed, you can specify that a new class inherits all the properties (fields and methods) of another class. The class that inherits is the subclass and the class it inherits from is the superclass. The Curve class thus inherits from the Object3D class. Imagine that Human is a Java class. We could design a Man class and a Woman class. Both Man and Woman object can be addressed as Human because they share this aspect, but you can't mistake a Man for a Woman (we'll suppose makeup and fancy dressing doesn't exist in the Java world, right?). It's the same with AoI 3d objects. All, absolutely all inherit Object3D. So any AoI object will yield true when checked against instanceof Object3D. However only curves are Curve.

    Only? Hmmmm. Let's have a look at this Curve page on the documentation. What does it say?

    Direct Known Subclasses:
    Tube
    So this means a Tube inherits a Curve, so technically it's also a curve. That makes sense. So, logically, our script should also be able to mirror tubes as well. Well, let's see: create a tube (create a curve first then seelct Tube command from the Tools menu), select it and run the script. Tah-daaaah, it works!

    Now that the foundations are set, let's build up on this fresh knowledge. We've modifed an object, but what if we want to add one to the scene? Let's suppose you want to keep the original curve and add a mirrored copy of it to the scene. Of course, you can manually copy and paste the curve, select the copy and run the script. But that would be cheating... Let's modify the script to automatically add the mirrored curve. Obviously, the code remains unchanged until we change the original curve vertices positions.

    But now, we need to create a Curve. Objects are created through constructors. A class can provide one or several constructors (and sometimes no constructor at all, but this is an advanced topic). In case it provides several constructors, this just means there different ways to create an instance of the class probably to best fit the different situations in which you'd like to create a new object of this class. Let's look at the Curve class constructors. The Constructors Summary say there are two constructors. The first one, which uses a stream, is for loading a Curve from an AoI file on disk. This is not the one we're looking for. The second one looks more like although there are more parameters than just the array of vertices positions we're ready to provide. Let's review them:
    public Curve(Vec3[] v,
    float[] smoothness,
    int smoothingMethod,
    boolean isClosed)

    The array Vec3[]v is the array of vertices positions we have computed. The float[] smoothness array is the set of smoothness coefficients that describe how the curve is smoothed. The smoothingMethod parameters tells if the curve has to be smoothed at all, and if so if the smoothing has to be approximating or interpolating. Finally, the isClosed boolean tells if the curve is closed or not. But which values do we have to specify for these parameters? Since the script mirrors the original curve, the parameters must be cloned from this curve. We thus need to access them from the selected curve. We'll use the following methods:

    -getSmoothness() to get smoothness coefficients;

    -getSmoothingMethod() to get the smoothing method;

    -isClosed() to access the closed character of the curve.

    Now, fork the original script and save it as MirroredCurve2.bsh. Delete everything after the loop that inverts the vertices positions x coordinates. First, we'll create a new curveusing the new vertices positions:

    curve = new Curve(v, object.getSmoothness(), object.getSmoothingMethod(), object.isClosed());

    Next we have to add this new object to the scene. Looking at the Scene class methods, we see there is a one called.. addObject. Who said writing scripts was difficult? Ok, there are actually three addObject methods. We'll use the one that most fits our needs:

    void addObject(Object3D obj, CoordinateSystem coords, java.lang.String name, UndoRecord undo)

    We have the object. We can easily provide a name. The UndoRecord is here to alow the user to revert back from mirrored curve creation. This is a bit off topic, fortunately we can provide a null record (this means the user won't be able to revert). And what aout the coordinate system? You know, the one that tells the scene where to place the object and how to orientate it? We'll do as before: we'll clone the exisiting one. We can't reuse the existing one, otherwise the two curves would share the same coordinate system and that would lead to trouble. But we can clone a coordinate system using the following code (look at the duplicate method in the CoordinateSystem class):

    coords = info.coords.duplicate();

    and finally we can add the created curve to the scene:

    scene.addObject(curve, coords, "MirroredCurve", null);

    Let's try our new script. Hurray, it works! Err, well, actually something is missing. Look at the object list at the right. Yes, it doesn't show or new "MirroredCurve" object, although the ucrve does show in the scene. Hmm, it looks like we forgot to do something at the LayoutWindow level. Let's browse through the methods in this class. Hey, there is also an addObject method! The same one in fact. That would explain why uding addObject on the the Scene instance was insufficient. Change the least line to:

    window.addObject(curve, coords, "MirroredCurve", null);

    This time it works perfectly! Now you know how to modify an existing object and how to add one to the scene. Next time we'll see how we can provide the user some information and let him specify some parameter values.

     





    Leave a comment
    RSS comments

    Only registered users can leave comments.
    Please login or register.

    Powered by AkoComment Tweaked Special Edition v.1.4

    Last Updated ( Tuesday, 04 December 2007 )
     
    < Prev   Next >
     
    Web design by Marc Carson, a Ukiah Web Designer