JScript Object Model Examples (XSISDK)

See Object Model Reference - Additional Examples for guidelines about adding JScript examples to this page.

Table of contents

ProjectItem

ProjectItem.Model

// Given a XSICollection or Selection object, this function
// returns all the models that the object belongs to.

/*XSICollection*/ function GetModelSpace( in_coll )
{
	var ret = new ActiveXObject( "XSI.Collection" ) ;
	
	// Each model will automatically only appear once in the list
	ret.Unique = true; 
	
	for ( var i = 0 ; i < in_coll.Count ; i++ )
	{
		try
		{
			ret.Add( in_coll(i).Model ) ;
		}
		catch(e)
		{
			// XSICollection contains something that doesn't
			// support Model property
			logmessage( "Problem processing " + in_coll(i).FullName ) ;
		}
	}	


	return ret ;
}


// Demo

newscene( null, false ) ;


var oNull = ActiveSceneRoot.AddNull() ;
var oSphere = ActiveSceneRoot.AddGeometry( "Sphere", "MeshSurface" ) ;
var oSphere2 = ActiveSceneRoot.AddGeometry( "Sphere", "MeshSurface" ) ;

// Move sphere into the model
var oModel1 = ActiveSceneRoot.AddModel(oSphere2,"mdl1") ;

var oModel2 = ActiveSceneRoot.AddModel(null,"mdl2") ;
var oModel3 = ActiveSceneRoot.AddModel(null,"mdl3") ;


// Fill various types of objects into a 
// collection
var input = new ActiveXObject( "XSI.Collection" ) ;

input.Add( oNull ) ;
input.Add( oSphere ) ;
input.Add( oSphere.ActivePrimitive ) ;
input.Add( oSphere.Parameters( "posx") ) ;
input.Add( oSphere2  ) ;

// Note: if you ask a model for its Model it will 
// return its parent Model, so "mdl2" does not appear in the
// final results
input.Add( oModel2 ) ;

// But any property or other nested data of the Model belongs to
// the specific model
input.Add( oModel3.Properties( "Visibility" ) ) ;

// Get the list of all the models
var models = GetModelSpace( input ) ;

for ( var i = 0 ; i < models.count ; i++ )
{
	logmessage( models.Item(i) ) ;
}

//Expected results:
//INFO : Scene_Root
//INFO : mdl1
//INFO : mdl3

Texture

Texture.GetTransformValues

// This example demonstrated the relationship
// between Texture.GetTransformValues, the Texture Projection
// and the Cluster Property


var sProjectionName = "MyProjection" ;

var oCylinder = CreateCylinderWithMaterial() ;

var oCurrentTexture = oCylinder.Material.CurrentTexture ;
logmessage( "Current Texture: " + oCurrentTexture.FullName  ) ;
logmessage( "Current UV: " + oCylinder.Material.CurrentUV.FullName ) ;
logmessage( "Current ImageClip: " + oCylinder.Material.CurrentImageClip  ) ;


logmessage( "-------------Before applying a repeat----------" ) ;
PrintTransformValues( oCurrentTexture ) ;

// Now actually apply a transformation at the Texture level 
oCurrentTexture.Parameters( "repeats" ).Parameters("x").Value = 2 ;

logmessage( "-------------After applying a repeat----------" ) ;
PrintTransformValues( oCurrentTexture ) ;

// Show that the raw data was not affected
logmessage( "-------------Raw UV----------" ) ;
PrintRawProjectionData( oCylinder ) ;


SetDisplayMode("Camera", "texturedecal");

function CreateCylinderWithMaterial()
{
	// Function to create a sample scene.  It has a Cylinder
	// with a simple render tree of an image connected to a Phong.
	// To simplify the example it does not load a specific image 
	// clip so it is using the default noIcon image.

	// Create a cylinder

	newscene(null,false) ;

	var oCylinder = ActiveSceneRoot.AddGeometry( "Cylinder", "MeshSurface" ) ;

	// Reduce the size of the geometry to reduce the amount of UV values we will print out
	var oGeometryParams = GetValue( oCylinder + ".polymsh.geom" ).Parameters
	oGeometryParams("subdivu").Value = 2
	oGeometryParams("subdivv").Value = 1
	oGeometryParams("subdivbase").Value = 1

	var oMaterial = oCylinder.AddMaterial( "Phong" ) ;
	var oPhong = oMaterial.Shaders(0) 
	var oPhongParams = oPhong.Parameters ;

	// Create a texture node and connect it to both ambient and diffuse
	var oTextureShader = oPhongParams( "ambient" ).ConnectFromPreset("Image", siTextureShaderFamily) ;
	oPhongParams( "diffuse" ).Connect( oTextureShader ) ;

	// At this point the texture will not draw because there is 
	// no projection to map the 2D onto the 3D

	// This command creates the projection and the support
	CreateProjection(
			oCylinder, 
			siTxtCylindrical, 
			siTxtDefaultCylindrical, 
			"MySupport",      // Name to give new support
			sProjectionName,   // Name to give new projection
			null, 
			null, 
			null);

	// Connect the projection
	oTextureShader.Parameters("tspace_id").SetInstanceValue( oCylinder,sProjectionName ) ;

	return oCylinder ;
}

function PrintRawProjectionData( in_oObj )
{
	// Find the ClusterProperty that contains the raw UVW values 
	// of the current texture
	var oTextureClusterProperty = in_oObj.Material.CurrentUV ;
	PrintUVWClusterProperty( oTextureClusterProperty ) ;
}


function PrintUVWClusterProperty( in_oTextureClusterProperty )
{
	// Print the UVW cluster property.  It shows how to read
	// the 2-Dimensional data with JScript VBArray object

	logmessage( "Contents of " + in_oTextureClusterProperty.FullName ) ;

	var aRawUVValues = new VBArray( in_oTextureClusterProperty.Elements.Array ) ;

	for ( i = 0 ; i <= aRawUVValues.ubound(2) ; i++ )
	{
		logmessage( "UVW["+i+"] = " 
			+ XSIRound( aRawUVValues.getItem( 0, i ), 2) + "," +
			+ XSIRound( aRawUVValues.getItem( 1, i ), 2) + "," +
			+ XSIRound( aRawUVValues.getItem( 2, i ), 2)) ;				
	}
}

function PrintTransformValues( in_oTexture )
{
	// Print the transformed UVW values associated with the texture
	// It shows how the data is structured as a flattened 1D array

	logmessage( "Fully transformed contents of " +  in_oTexture.FullName ) ;

	aTransformedTexture = new VBArray(in_oTexture.GetTransformValues(127)) ;

	// We have a 1 dimensional array, with the U,V,W values flattened
	cntItems = ( aTransformedTexture.ubound(1) + 1 ) / 3 ;

	for ( i = 0 ; i < cntItems ; i++ )
	{
		logmessage( "UVW["+i+"] = " 
			+ XSIRound( aTransformedTexture.getItem( 3*i ), 2) + "," +
			+ XSIRound( aTransformedTexture.getItem( 3*i+1 ), 2) + "," +
			+ XSIRound( aTransformedTexture.getItem( 3*i+2 ), 2)) ;
	}

}

// Output:
//
//INFO : Current Texture: cylinder.Material.Phong.Image
//INFO : Current UV: cylinder.polymsh.cls.Texture_Coordinates_AUTO.MyProjection
//INFO : Current ImageClip: Clips.noIcon_pic
//INFO : -------------Before applying a repeat----------
//INFO : Fully transformed contents of cylinder.Material.Phong.Image
//INFO : UVW[0] = 0.61,0,0
//INFO : UVW[1] = 0.25,0,0
//INFO : UVW[2] = 0.75,0,0
//...snip...
//INFO : UVW[28] = 1.25,1,0
//INFO : UVW[29] = 0.75,1,0
//INFO : -------------After applying a repeat----------
//INFO : Fully transformed contents of cylinder.Material.Phong.Image
//INFO : UVW[0] = 1.23,0,0
//INFO : UVW[1] = 0.5,0,0
//INFO : UVW[2] = 1.5,0,0
//...snip...
//INFO : UVW[28] = 1.5,1,0
//INFO : UVW[29] = 0.5,1,0
//INFO : -------------Raw UV----------
//INFO : Contents of cylinder.polymsh.cls.Texture_Coordinates_AUTO.MyProjection
//INFO : UVW[0] = 0.61,0,0
//INFO : UVW[1] = 0.25,0,0
//INFO : UVW[2] = 0.75,0,0
//...snip...
//INFO : UVW[28] = 1.25,1,0
//INFO : UVW[29] = 0.75,1,0

This page was last modified 07:59, 31 Aug 2007.
This page has been accessed 9083 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy