Layers

In the Select Window tabs, the user can manage various type of objects, such as geometric primitives, materials, rendering settings and so on. Each of the Select Window tabs correspond to a certain layer object. For example, the material tab shows you the contents of the 'material layer' of the current project. The Grid tab shows you the contents of the 'grid layer' of the current project.

We have already explored some of these layer objects, such as geometric primitives and material objects.

All layer objects have a common base class, called 'scripts/js/real/layer/r3objlay.js'. This class defines methods such as INSERT(), LOCKEXCLUSIVE() and RELEASE().

Let's go through a couple of other useful methods defined by the object layer class.

Exporting Selected Objects

The layer object defines a property called 'selected objects'. Many of the tools in Realsoft 3D tools are actually applied to selected objects. For example, the Move tool translates selected geometric objects. We have already used the method ENUMSELECTLIST to deform objects. Let's demonstrate another enumeration method called ENUMOBJECTS. This method enumerates the entire contents of a layer.

Let's assume we want to export geometric objects to a file named 'export.js'.

To create a Realsoft 3D file object, call:

    myFile = new r3File(R3FIA_FileName, "export.js", R3FIA_Mode, "wa");

Then we need a hook function for handling geometric objects. In the hook function, we create a JavaScript wrapper object for the enumerated Realsoft 3D geometry object, fetch the attributes of it and write them out to the opened file. For sake of simplicity, we handle only the sphere object here. It would be a very straight forward task to add support for other geometric objects as well.

    include("real/objects/r3sphere.js");

    function myExportHook(r3obj)
    {
      obj = R3ToJS(r3obj);
      if(!jso)
          return 1;

      // fetch the class identifier of the object
      iClid = obj.GetClassID();

      if(clid == R3CLID_SPHERE) {

        sName = obj.GetName();
        pCenter = obj.GetCenter();
        vA = obj.GetA();
        vB = obj.GetB();
        vC = obj.GetC();
        
        // write them out 
        this.myFile.PUTS("ClassID: " + iClid + "(ellipsoid)\n");
        this.myFile.PUTS("Name: " + sName + "\n");
        this.myFile.PUTS("Center: {" + pCenter.x + ", " + pCenter.y +
                                   ", " + pCenter.z + "}\n");
        this.myFile.PUTS("A: {" + vA.x +  ", " + vA.y + ", " + vA.z + "}\n");
        this.myFile.PUTS("B: {" + vB.x + ", " + vB.y + ", " + vB.z + "}\n");
        this.myFile.PUTS("C: {" + vC.x + ", " + vC.y + ", " + vC.z + "}\n");
      }
      return 1;
    }    

Then you can export objects by calling the ENUMOBJECTS method:

    primLayer.LOCKSHARED();
    primLayer.ENUMOBJECTS([R3RA_Hook, myExportHook]);
    primLayer.RELEASE();

Setting Attributes to the Selected Objects

Another often needed task is to set desired attributes to the selected objects. There are many ways to do this. For example, you can use the ENUMSELECTLIST method to enumerate selected objects to a hook function, which then checks the type of the object and sets appropriate attributes.

Another approach is to call the SETONSELECTED method. This methods accepts a tag list, specifying attribute identifiers, and corresponding values. For example, let's assume we need to set spin and velocity for the selected objects. Velocity and Spin are attributes defined by 'scripts/js/real/objects/r3prim.js' class, which is the base class for all geometric objects.

    vel = new r3Vect(0.1, 0.1, 0);
    spin = new r3Vect(PI * 2, 0, 0);
    
    primLayer.LOCKEXCLUSIVE();
    primLayer.SETONSELECTED([R3PRIMA_Mass, 1.2,
                             R3PRIMA_Velocity, vel,
                             R3PRIMA_Spin, spin]);
    primLayer.RELEASE();