Slice of Array of Objects

gollor42gollor42 Posts: 13

Trying to rearrange the selected nodes in a scene and group them by the number tagged at the end of the label. Also trying to do this without having to loop thru those selected nodes too many times. So I build a sparce array:

var aSelectedNodes = new Array(nSelectedNodes);

With my current test data that wil set it to 30 items, but the loop will only fill it with six objects that have four nodes each. 

While trying to get rid of most of the empty array I don't need, I did this, sliced out the non-empty items in the array:

  var aGroups = aSelectedNodes.slice(0,nTrueLength);  print("aGroups.length ["+aGroups.length+"]");

I got a length back, so a thing happened, if not quite what I want. Because the objects inside the array don't seem entirely defined anymore; I get an error trying to access one of the objects inside the array.

Script Error: Line 149

TypeError: Result of expression 'aGroups[ndx]' [undefined] is not an object.
Stack Trace: ()@D:/OneDrive/3D Content/MyDazNav/Scripts/Gollor/Expand Adamasen Level.dsa:149
149: aGroups[ndx].oFrontzone.oOldPos = aGroups[ndx].oFrontzone.oNode.getWSPos();

If I understand the error it's saying that either the object inside the array item got borked, or one of the subobjects of that object got borked. 

So I'm wondering what should I expect the slice function to do when it's working with an array of objects of objects of objects?

 

Post edited by gollor42 on

Comments

  • andya_b341b7c5f5andya_b341b7c5f5 Posts: 694
    edited July 2019

    The simplest explanation is that your index into the array aGroups is referencing a position that doesn't contain an object, though you expect it to contain an object.  I would check the value of ndx, and the type of aGroups[ndx], if you haven't done so already, even if just to rule out the obvious.

    I presume the slice() method in Daz script behaves like Javascript and returns a new array that does not include the end position.

    undefined.JPG
    964 x 883 - 100K
    Post edited by andya_b341b7c5f5 on
  • gollor42gollor42 Posts: 13

    The simplest explanation is that your index into the array aGroups is referencing a position that doesn't contain an object, though you expect it to contain an object.  I would check the value of ndx, and the type of aGroups[ndx], if you haven't done so already, even if just to rule out the obvious.

    I presume the slice() method in Daz script behaves like Javascript and returns a new array that does not include the end position.

    I too would presume that the slice() method would work in a similar way to JavaScript. I would further presume it works like the documentation on the Daz Script Array.slice(). Although I'm not sure that's what's happening here.

    I added some additional debugging code as you suggested:

    		print("groups of zones ["+aSelectedNodes.length+"]");				var aGroups = aSelectedNodes.slice(0,nTrueLength);		print("aGroups.length = ["+aGroups.length+"], and aGroup is typeof ["+typeof aGroups+"]");		print("aGroups[0] is type ["+typeof aGroups[0]+"]");

    groups of zones [30]

    aGroups.length = [6], and aGroup is typeof [object]
    aGroups[0] is type [undefined]
    Script Error: Line 150
    TypeError: Result of expression 'aGroups[ndx]' [undefined] is not an object.
    Stack Trace: ()@D:/OneDrive/3D Content/MyDazNav/Scripts/Gollor/Expand Adamasen Level.dsa:150

     

    But it told me pretty much what I already knew, that the contents of the aGroups Array, isn't what I think it should be. In fact I would have expected the typeof operator to indicate aGroups was an Array, not an Object.

    nTrueLength in the slice function is +1 the number of items from the list I want.

     

  • Richard HaseltineRichard Haseltine Posts: 100,990

    Are you expecting the objects in the array to be a defined object? I don't see anywheer you are specifiying them - you are just creating an empty array in your sample code.

  • gollor42gollor42 Posts: 13

    Are you expecting the objects in the array to be a defined object? I don't see anywheer you are specifiying them - you are just creating an empty array in your sample code.

    No I specified the objects I used in the code, I just didnt know that anyone wanted me to paste the whole 177 line script into the forum for what I had hoped would be a simple question, but here it is:

    // DAZ Studio version 4.11.0.383 filetype DAZ Script// Define an anonymous function;// serves as our main loop,// limits the scope of variables(function(){		// Declare local variables	var oNode;	var oSelectedNode = new Object();	oSelectedNode.nGroupNum;	oSelectedNode.oFrontzone;	oSelectedNode.oRearzone;	oSelectedNode.oRightzone;	oSelectedNode.oLeftzone;	var oZone = new Object();	oZone.nNodeId;	oZone.sNodeName;	oZone.sLabel;	oZone.oNode;	oZone.oOldPos;	oZone.oNewPos;	oZone.oOldRot;	oZone.oNewRot;	oZone.oOldScale;	oZone.oNewScale;	// Get the total number of scene nodes	var nSelectedNodes = Scene.getNumSelectedNodes();	var sNodeName = "group";	if(nSelectedNodes > 1) {		//MessageBox.information("Click ok to raise all the oneven groups up 6 meters, and expand them by 6 meter","Here WeGo!","&Ok");		var aSelectedNodes = new Array(nSelectedNodes);		print("iterating over " + aSelectedNodes.length + " Nodes");				var nOldGroup = 0;		var nCurGroup = 1;		var nMaxGroup = 0;		// Iterate over each node		for( i = 0; i < nSelectedNodes; i++ ){			// Get the current node			oNode = Scene.getSelectedNode( i );			sLabel = oNode.getLabel();			print("i ["+i+"]: Node Label ["+sLabel+"]");			nNodeChildren = oNode.getNumNodeChildren();			if(nNodeChildren > 0) {				print("Node ["+i+"] is a group");			}			else {				var aSlices = sLabel.split(" ");				//print("Num slices  ["+aSlices.length+"] in aSlices ["+aSlices+"]");				if(aSlices.length > 1) {					var subA = aSlices[(aSlices.length-1)];					//print("subA ["+subA+"]");					var subB = subA.replace("(","");					//print("subB ["+subB+"]");					var sLastSlice = subB.replace(")","");					//print("sLastSlice ["+sLastSlice+"]");					var sDirtyName = sLabel.replace(subA, "");					sNodeName = sDirtyName.trim();					print("trimmed nodename ["+sNodeName+"]");					nCurGroup = sLastSlice;					//print("group number n");					print("group number ["+nCurGroup+"]");				}				else {					nCurGroup = 1;					sNodeName = sLabel;					print("group number 1");				}				if(nOldGroup != nCurGroup) {					print("old group ["+nOldGroup+"] != ["+nCurGroup+"]");					nOldGroup = nCurGroup;					aSelectedNodes[nCurGroup] = Object.create(Object, oSelectedNode);					aSelectedNodes[nCurGroup].nGroupNum = nCurGroup;							if(nCurGroup > nMaxGroup) {						nMaxGroup = nCurGroup;					}				}				else {					print("old group ["+nOldGroup+"] == ["+nCurGroup+"]");				}				switch (sNodeName) {					case "frontzone":						aSelectedNodes[nCurGroup].oFrontzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oFrontzone.nNodeId = i;						aSelectedNodes[nCurGroup].oFrontzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oFrontzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oFrontzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+sNodeName+"]");						break;					case "rightzone":						aSelectedNodes[nCurGroup].oRightzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oRightzone.nNodeId = i;						aSelectedNodes[nCurGroup].oRightzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oRightzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oRightzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+sNodeName+"]");						break;					case "leftzone":						aSelectedNodes[nCurGroup].oLeftzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oLeftzone.nNodeId = i;						aSelectedNodes[nCurGroup].oLeftzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oLeftzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oLeftzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+sNodeName+"]");						break;					default: // rearzone						aSelectedNodes[nCurGroup].oRearzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oRearzone.nNodeId = i;						aSelectedNodes[nCurGroup].oRearzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oRearzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oRearzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+sNodeName+"]");						break;				}			} // end of else if nodes have no children 		} // end of loop thru selected nodes		// count down from end of aSelectedNodes pop off the empty ones to find out the real number		// of groups		var nTrueLength = aSelectedNodes.length;		var nMaxNdx = aSelectedNodes.length-1;		for(var ndx = nMaxNdx;ndx>0;ndx--){			if(aSelectedNodes[ndx]){				print("First defined aSelectedNode ["+ndx+"], nTrueLength["+nTrueLength+"]");				nTrueLength = ndx;				break;			}			else{				print("ndx["+ndx+"]:aSelectedNodes[ndx]["+aSelectedNodes[ndx]+"]");				nTrueLength--;			}		}		print("groups of zones ["+aSelectedNodes.length+"]");				var aGroups = aSelectedNodes.slice(0,nTrueLength);		print("aGroups.length = ["+aGroups.length+"], and aGroup is typeof ["+typeof aGroups+"]");		print("aGroups[0] is type ["+typeof aGroups[0]+"]");				// iterate thru each group in reverse and raise the height by 6m		var fYPosDelta = 600;		var fFrontXScaleDelta = 6.0;		var fFrontYScaleDelta = 1.2;		var nFrontXPosDelta = 300;		var nGroupHeight = fYPosDelta;		var nStartNdx = aGroups.length		for(var ndx = nStartNdx;ndx>=0;ndx--){			aGroups[ndx].oFrontzone.oOldPos = aGroups[ndx].oFrontzone.oNode.getWSPos();			aGroups[ndx].oFrontzone.oNewPos = aGroups[ndx].oFrontzone.oNode.getWSPos();			aGroups[ndx].oFrontzone.oNewPos.y = aGroups[ndx].oFrontzone.oOldPos.y + fYPosDelta;			aGroups[ndx].oFrontzone.oNode.setWSPos(aGroups[ndx].oFrontzone.oNewPos);			print("Set ["+aGroups[ndx].oFrontzone.sLabel+"], height to ["+aGroups[ndx].oFrontzone.oNewPos.y+"]");			aGroups[ndx].oRearzone.oOldPos = aGroups[ndx].oRearzone.oNode.getWSPos();			aGroups[ndx].oRearzone.oNewPos = aGroups[ndx].oRearzone.oNode.getWSPos();			aGroups[ndx].oRearzone.oNewPos.y = aGroups[ndx].oRearzone.oOldPos.y + fYPosDelta;			aGroups[ndx].oRearzone.oNode.setWSPos(aGroups[ndx].oRearzone.oNewPos);			print("Set ["+aGroups[ndx].oRearzone.sLabel+"], height to ["+aGroups[ndx].oRearzone.oNewPos.y+"]");			aGroups[ndx].oRightzone.oOldPos = aGroups[ndx].oRightzone.oNode.getWSPos();			aGroups[ndx].oRightzone.oNewPos =  aGroups[ndx].oRightzone.oNode.getWSPos();			aGroups[ndx].oRightzone.oNewPos.y = aGroups[ndx].oRightzone.oOldPos.y + fYPosDelta;			aGroups[ndx].oRightzone.oNode.setWSPos(aGroups[ndx].oRightzone.oNewPos);			print("Set ["+aGroups[ndx].oRightzone.sLabel+"], height to ["+aGroups[ndx].oRightzone.oNewPos.y+"]");			aGroups[ndx].oLeftzone.oOldPos = aGroups[ndx].oLeftzone.oNode.getWSPos();			aGroups[ndx].oLeftzone.oNewPos = aGroups[ndx].oLeftzone.oNode.getWSPos();			aGroups[ndx].oLeftzone.oNewPos.y = aGroups[ndx].oLeftzone.oOldPos.y + fYPosDelta;			aGroups[ndx].oLeftzone.oNode.setWSPos(aGroups[ndx].oLeftzone.oNewPos);			print("Set ["+aGroups[ndx].oLeftzone.sLabel+"], height to ["+aGroups[ndx].oLeftzone.oNewPos.y+"]");		}			} // end if selected nodes > 1// Finalize the function and invoke})();

    The script has some specific things it looks for in the selected objects, and it's really meant as a way to help me rearrange a bunch of similar low poly objects, in a very specific configuration. I can try manually copying the objects I need into another array without using the slice function, but since it was there I tried using it. It urks me that it doesn't work as I expected, and I'd like to know if I'm using it wrong, or have wrong assumptions, or whatever reason it didn't do what I wanted it to. 

    The second goal of this script is as a practical bit of code to help me learn Daz Scripting. I know JavaScript very well, and as an interpreted language it's very persnickity. Daz Script seems to share that trait if perhaps not the same set of things it's persnickity about.

     

  • andya_b341b7c5f5andya_b341b7c5f5 Posts: 694
    edited July 2019

    As Richard says, how the aGroups array is being populated needs looking at.  It has 30 elements, you have selected the first six, and for some reason the element at index 0 is undefined.

    As in Javascript, typeof will return 'object' for an array (which is an object).  To test for an array, you could use instanceof and/or check the constructor:

    var a1 = new Array(10)var x = 1var y = 'abc'print(typeof x)print(typeof y)print(typeof a1)print(a1 instanceof Array)if (a1 instanceof Array) {	print( 'a1 is an Array' )}if (x instanceof Array) {	print('x is an Array')}if (a1.constructor == Array){	print('a1 is an Array')}if (y.constructor == String){	print('y is a string')}

    Executing Script...

    number

    string
    object
    true
    a1 is an Array
    a1 is an Array
    y is a string
    Result: 
    Script executed in 0 secs 7 msecs.

     

     

    Post edited by andya_b341b7c5f5 on
  • andya_b341b7c5f5andya_b341b7c5f5 Posts: 694
    edited July 2019

    Your post of the script code appeared after I posted my earlier response.

    Obviously we don't know what your scene looks like, but it seems to me that you have not eliminated all the undefined elements of the array aSelectedNodes before slicing it to chop off the undefined elements at the end, and assigning the result to the array aGroups.  When you come to iterate through aGroups, you then hit an undefined element at the beginning.

    Clearly, you could just skip over the undefined elements with:

    if (typeof aGroups[ndx] === 'undefined') {	continue;}

    as the first step within your for loop through the elements of aGroups, or strip them all out with the filter() function before the loop.  That may nor may not reveal other issues.  Alternatively, and without knowing the structure of your scene this isn't clear, work out why some elements of aSelectedNodes are undefined.

    Post edited by andya_b341b7c5f5 on
  • gollor42gollor42 Posts: 13

    It was a blasted +/-1 error. One difference between Daz Script Arrays and JavaScript Arrays I had not yet internalized - in Daz Script they start at 1, and in JavaScript they start at 0.

    		var nStartNdx = (aGroups.length-1);		for(var ndx = nStartNdx;ndx>0;ndx--){			var oOldPos = aGroups[ndx].oFrontzone.oNode.getWSPos();

    Changed the nStartNdx of the loop where the error occurs, and the exit check on the loop, an now it works. It was starting at the length ndx, which I think is probably wrong in either case. The exit check allowed the loop to go down to zero (ndx>=0), so that caused a second error.

    WIth another fix to use the right incrementing variable to set the height, now it works:

    // DAZ Studio version 4.11.0.383 filetype DAZ Script// Define an anonymous function;// serves as our main loop,// limits the scope of variables(function(){		// Declare local variables	//var oNode;	var oSelectedNode = new Object();	oSelectedNode.nGroupNum;	oSelectedNode.oFrontzone;	oSelectedNode.oRearzone;	oSelectedNode.oRightzone;	oSelectedNode.oLeftzone;	var oZone = new Object();	oZone.nNodeId;	oZone.sNodeName;	oZone.sLabel;	oZone.oNode;	oZone.oOldPos;	oZone.oNewPos;	oZone.oOldRot;	oZone.oNewRot;	oZone.oOldScale;	oZone.oNewScale;	// Get the total number of scene nodes	var nSelectedNodes = Scene.getNumSelectedNodes();	var sNodeName = "group";	if(nSelectedNodes > 1) {		//MessageBox.information("Click ok to raise all the oneven groups up 6 meters, and expand them by 6 meter","Here WeGo!","&Ok");		var aSelectedNodes = new Array(nSelectedNodes);		print("iterating over " + aSelectedNodes.length + " Nodes");				var nOldGroup = 0;		var nCurGroup = 1;		var nMaxGroup = 0;		// Iterate over each node		for( i = 0; i < nSelectedNodes; i++ ){			// Get the current node			var oNode = Scene.getSelectedNode( i );			var sLabel = oNode.getLabel();			print("i ["+i+"]: Node Label ["+sLabel+"]");			nNodeChildren = oNode.getNumNodeChildren();			if(nNodeChildren > 0) {				print("Node ["+i+"] is a group");			}			else {				var aSlices = sLabel.split(" ");				//print("Num slices  ["+aSlices.length+"] in aSlices ["+aSlices+"]");				if(aSlices.length > 1) {					var subA = aSlices[(aSlices.length-1)];					//print("subA ["+subA+"]");					var subB = subA.replace("(","");					//print("subB ["+subB+"]");					var sLastSlice = subB.replace(")","");					//print("sLastSlice ["+sLastSlice+"]");					var sDirtyName = sLabel.replace(subA, "");					sNodeName = sDirtyName.trim();					print("trimmed nodename ["+sNodeName+"]");					nCurGroup = sLastSlice;					//print("group number n");					print("group number ["+nCurGroup+"]");				}				else {					nCurGroup = 1;					sNodeName = sLabel;					print("group number 1");				}				if(nOldGroup != nCurGroup) {					print("old group ["+nOldGroup+"] != ["+nCurGroup+"]");					nOldGroup = nCurGroup;					aSelectedNodes[nCurGroup] = Object.create(Object, oSelectedNode);					aSelectedNodes[nCurGroup].nGroupNum = nCurGroup;							if(nCurGroup > nMaxGroup) {						nMaxGroup = nCurGroup;					}				}				else {					print("old group ["+nOldGroup+"] == ["+nCurGroup+"]");				}				switch (sNodeName) {					case "frontzone":						aSelectedNodes[nCurGroup].oFrontzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oFrontzone.nNodeId = i;						aSelectedNodes[nCurGroup].oFrontzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oFrontzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oFrontzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+aSelectedNodes[nCurGroup].oFrontzone.oNode.name+"]");						break;					case "rightzone":						aSelectedNodes[nCurGroup].oRightzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oRightzone.nNodeId = i;						aSelectedNodes[nCurGroup].oRightzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oRightzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oRightzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+aSelectedNodes[nCurGroup].oRightzone.oNode.name+"]");						break;					case "leftzone":						aSelectedNodes[nCurGroup].oLeftzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oLeftzone.nNodeId = i;						aSelectedNodes[nCurGroup].oLeftzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oLeftzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oLeftzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+aSelectedNodes[nCurGroup].oLeftzone.oNode.name+"]");						break;					default: // rearzone						aSelectedNodes[nCurGroup].oRearzone = Object.create(Object, oZone);						aSelectedNodes[nCurGroup].oRearzone.nNodeId = i;						aSelectedNodes[nCurGroup].oRearzone.sNodeName = sNodeName;						aSelectedNodes[nCurGroup].oRearzone.sLabel = sLabel;						aSelectedNodes[nCurGroup].oRearzone.oNode = oNode;						print("group ["+nCurGroup+"], zone ["+aSelectedNodes[nCurGroup].oRearzone.oNode.name+"]");						break;				}			} // end of else if nodes have no children 		} // end of loop thru selected nodes		// count down from end of aSelectedNodes pop off the empty ones to find out the real number		// of groups		var aGroups;		var nTrueLength = aSelectedNodes.length;		var nMaxNdx = aSelectedNodes.length-1;		var bFoundMax = true;		for(var ndx = nMaxNdx;ndx>0;ndx--){			if(aSelectedNodes[ndx]){				print("First defined aSelectedNode ["+ndx+"], nTrueLength["+nTrueLength+"]");				nTrueLength = ndx;				if(bFoundMax){					bFoundMax = false;					aGroups = new Array(nTrueLength)				}			    aGroups[ndx] = aSelectedNodes[ndx];				print("aSelectedNodes[ndx].nGroupNum ["+aSelectedNodes[ndx].nGroupNum+"]");				print("aSelectedNodes[ndx].oFrontzone.oNode.name ["+aSelectedNodes[ndx].oFrontzone.oNode.name+"]");				print("aGroups[ndx].nGroupNum ["+aGroups[ndx].nGroupNum+"]");				print("aGroups[ndx].oFrontzone.oNode.name ["+aGroups[ndx].oFrontzone.oNode.name+"]");			}			else{				print("ndx["+ndx+"]:aSelectedNodes[ndx]["+aSelectedNodes[ndx]+"]");				nTrueLength--;			}		}		print("groups of zones ["+aSelectedNodes.length+"]");				//var aGroups = aSelectedNodes.slice(0,nTrueLength);		print("aGroups.length = ["+aGroups.length+"], and aGroup is typeof ["+typeof aGroups+"]");		print("aGroups[1] is type ["+typeof aGroups[1]+"]");		if(aGroups.constructor == Array) { print("aGroups is an Array")}		print("aGroups[1] is a "+aGroups[1].constructor);		print("aGroups[1].oFrontzone is a "+aGroups[1].oFrontzone.constructor);				// iterate thru each group in reverse and raise the height by 6m		var fYPosDelta = 600;		var fFrontXScaleDelta = 6.0;		var fFrontYScaleDelta = 1.2;		var nFrontXPosDelta = 300;		var nGroupHeight = fYPosDelta;		var nStartNdx = (aGroups.length-1);		for(var ndx = nStartNdx;ndx>0;ndx--){			var oOldPos = aGroups[ndx].oFrontzone.oNode.getWSPos();			var oNewPos = aGroups[ndx].oFrontzone.oNode.getWSPos();			aGroups[ndx].oFrontzone.oOldPos = oOldPos;			aGroups[ndx].oFrontzone.oNewPos = oNewPos;			aGroups[ndx].oFrontzone.oNewPos.y = aGroups[ndx].oFrontzone.oOldPos.y + nGroupHeight;			aGroups[ndx].oFrontzone.oNode.setWSPos(aGroups[ndx].oFrontzone.oNewPos);			print("Set ["+aGroups[ndx].oFrontzone.sLabel+"], height to ["+aGroups[ndx].oFrontzone.oNewPos.y+"]");			aGroups[ndx].oRearzone.oOldPos = aGroups[ndx].oRearzone.oNode.getWSPos();			aGroups[ndx].oRearzone.oNewPos = aGroups[ndx].oRearzone.oNode.getWSPos();			aGroups[ndx].oRearzone.oNewPos.y = aGroups[ndx].oRearzone.oOldPos.y + nGroupHeight;			aGroups[ndx].oRearzone.oNode.setWSPos(aGroups[ndx].oRearzone.oNewPos);			print("Set ["+aGroups[ndx].oRearzone.sLabel+"], height to ["+aGroups[ndx].oRearzone.oNewPos.y+"]");			aGroups[ndx].oRightzone.oOldPos = aGroups[ndx].oRightzone.oNode.getWSPos();			aGroups[ndx].oRightzone.oNewPos =  aGroups[ndx].oRightzone.oNode.getWSPos();			aGroups[ndx].oRightzone.oNewPos.y = aGroups[ndx].oRightzone.oOldPos.y + nGroupHeight;			aGroups[ndx].oRightzone.oNode.setWSPos(aGroups[ndx].oRightzone.oNewPos);			print("Set ["+aGroups[ndx].oRightzone.sLabel+"], height to ["+aGroups[ndx].oRightzone.oNewPos.y+"]");			aGroups[ndx].oLeftzone.oOldPos = aGroups[ndx].oLeftzone.oNode.getWSPos();			aGroups[ndx].oLeftzone.oNewPos = aGroups[ndx].oLeftzone.oNode.getWSPos();			aGroups[ndx].oLeftzone.oNewPos.y = aGroups[ndx].oLeftzone.oOldPos.y + nGroupHeight;			aGroups[ndx].oLeftzone.oNode.setWSPos(aGroups[ndx].oLeftzone.oNewPos);			print("Set ["+aGroups[ndx].oLeftzone.sLabel+"], height to ["+aGroups[ndx].oLeftzone.oNewPos.y+"]");			nGroupHeight = nGroupHeight + fYPosDelta;		}			} // end if selected nodes > 1	else {		print("You must select the appropriate node types");	}// Finalize the function and invoke})();

    Given groups of four rectangular prisms, it will raise each group 600cm more than the last. Step one complete. Now I need to increase the X and Z scales to gradually make them larger by the same increment.

  • Richard HaseltineRichard Haseltine Posts: 100,990

    Arrays start from 0 in DS too.

  • andya_b341b7c5f5andya_b341b7c5f5 Posts: 694
    edited July 2019

    Arrays start from 0 in DS too.

    And for an array of length N, the 'highest' index will be N-1 (ignoring non-numeric, or unordered numeric indexes!).  So starting the 'reverse' iteration over aSelectedNodes at aSelectedNodes.length-1 will avoid the attempt to access the undefined element aSelectedNodes[N], which is beyond the end of the array, and thus avoid the original error.

    Does Daz script/Javascript/ECMAScript not have a nice, helpful 'Index out of range' error for this type of situation?  If not, it really should.

    Post edited by andya_b341b7c5f5 on
  • Richard HaseltineRichard Haseltine Posts: 100,990

    Arrays start from 0 in DS too.

    And for an array of length N, the 'highest' index will be N-1 (ignoring non-numeric, or unordered numeric indexes!).  So starting the 'reverse' iteration over aSelectedNodes at aSelectedNodes.length-1 will avoid the attempt to access the undefined element aSelectedNodes[N], which is beyond the end of the array, and thus avoid the original error.

    Does Daz script/Javascript/ECMAScript not have a nice, helpful 'Index out of range' error for this type of situation?  If not, it really should.

    It doesn't, possibly because arrays are not necessarily dimensioned.

Sign In or Register to comment.