Porting to DAZ Script 2

DAZ Script 1 is the scripting language available in DAZ Studio versions 1.x and 2.x. In DAZ Studio 3.x, the scripting language has changed and has been rebranded as DAZ Script 2. Some simple scripts written for DAZ Script 1 will still work in DAZ Script 2, however, many scripts will require minor changes, and some will require more significant changes in order to work.

Included in DAZ Studio 3.x, is a conversion tool that automatically converts scripts saved from earlier versions of DAZ Studio to the DAZ Script 2 format/syntax. This tool was written with the goal of converting all the DAZ Script 1-based Presets that have been saved from earlier versions of DAZ Studio. This tool is also available to convert custom hand written scripts from earlier versions of DAZ Studio. Since we can not possibly anticipate all of the methods used when writing scripts, it is not realistic for us to provide a converter that is capable of handling every case, but it should serve as a solid first-step in converting scripts that you have written for earlier versions of the application.

When you open a script written for a prior version of the application in the DAZ Script IDE in DAZ Studio 3.x, you will be presented with a message informing you that "The selected script is in a deprecated format (*.ds). The contents of the script has been automatically converted to the new format and the file extension has been updated to reflect the conversion." Upon clicking the OK button, the IDE will then automatically run the conversion and display the result in a new tab of the IDE; it does not modify your existing script file. Manual access to the converter function can be found on DzScript.

What has changed:

Class:

This is the most significant change between DAZ Script 1 to DAZ Script 2. There is no longer the syntax to declare classes and inherit (extend) other classes that was available in DAZ Script 1. In DAZ Script 2, object-oriented design methods can still be used, but the syntax is very different - the new method should be familiar to developers who have written JavaScript, though it may be a little unintuitive for developers more comfortable with C++ and Java style syntax.

For example, in DAZ Script 1, a point class could be defined as:

    class Point3
    {
        var x;
        var y;
        var z;
        function Point3( px, py, pz )
        {
            x = px;
            y = py;
            z = pz;
        }

        function toString()
        {
            return String( "Point3(%1, %2, %3)" ).arg(x).arg(y).arg(z);
        }
    }

In DAZ Script 2, you could represent the same point object as:

    function Point3( px, py, pz )
    {
        this.x = px;
        this.y = py;
        this.z = pz;
    }

    Point3.prototype.toString = function()
    {
        return String( "Point3(%1, %2, %3)" ).arg( this.x ).arg( this.y ).arg( this.z );
    }

In DAZ Script 2, everything is an object, including functions - so the function Point3 becomes our point object, and we assign the x, y, and z variables as properties on the Point3 object. Functions can also be assigned as properties, so we create a toString property on the Point3 object that is set to the function that creates the string.

We can use our new Point3 object just as we would the Point3 class:

    var oPoint = new Point3( 1, 2, 3 );

    debug( oPoint );

Prototype and Inheritance:

Prototypes are a part of the ECMAScript 3.0 specification that was not implemented in DAZ Script 1, but is available in DAZ Script 2. The purpose of a prototype object is to define the behavior that should be shared by a set of similar objects. We can think of objects that share the same prototype as being part of the same class; not to be confused with the class construct of languages like C++, Java and DAZ Script 1.

When an attempt is made to access a property on an object and that object does not have the property, that attempt is then made on the prototype for the object; and if the prototype has the property then its value is returned. If the object's prototype doesn't have the property, the prototype's prototype is checked, and so on. This successive chain of objects is whats referred to as a prototype chain.

Using the prototype of an object, we can make one object derive (or inherit) from another, much like using the 'extends' keyword in DAZ Script 1 to make a child class.

In DAZ Script 1, we could create a Point4 class that extends the Point3 class with a new 'w' property:

    class Point4 extends Point3
    {
        var w;
        function Point4( px, py, pz, pw )
        {
            Point3( px, py, pz );
            w = pw;
        }

        function toString()
        {
            return String( "Point4(%1, %2, %3, %4)" ).arg(x).arg(y).arg(z).arg(w);
        }
    }

In DAZ Script 2, we can use the prototype to do the same thing:

    function Point4( px, py, pz, pw )
    {
        arguments.callee.superclass.call( this, px, py, pz );
        this.w = pw;
    }

    Point4.prototype.toString = function()
    {
        return String( "Point4(%1, %2, %3, %4)" ).arg( x ).arg( y ).arg( z ).arg( this.w );
    }

    Point4.superclass = Point3;

Const:

In DAZ Script 1, variables could be declared as 'const' (constant) meaning that their value could not be changed after declaration. If an attempt was made to change a const variable after declaration the interpreter would throw an error and the script would fail. This has changed in DAZ Script 2 - the 'const' keyword is no longer officially supported (as it is merely a reserved keyword in the ECMAScript 3.0 standard), a variable declared to be 'const' will still not allow itself to be changed, but attempting to do so will not cause the interpreter to throw an error and the script will not fail. We recommend replacing the 'const' specifier from any variable declarations with the var specifier.

Static:

In DAZ Script 1, class member variables could be declared as 'static' meaning that all instances of the class shared one copy of that variable, instead of each instance having its own variable.
    class Point3
    {
        static var count = 0;
    }

    var point = new Point3;
    print( Point3.count );
    print( point.count );

In DAZ Script 2, the equivalent of this is to assign variables that were static members to properties of the constructor function.

    function Point3 ()
    {
        //...;
    }

    Point3.count = 0;

    var point = new Point3;
    debug( Point3.count );
    debug( point.count );

Note that in DAZ Script 1, static members were accessible from an instance of a given class as well as the class itself. In DAZ Script 2, the variable is a member of the constructor object only, and thereby only accessible from the constructor.


Generated on Thu Sep 24 12:21:06 2009

Copyright © 2002 - 2009 DAZ 3D, Inc.