How to create an Array in a loop

V3DigitimesV3Digitimes Posts: 3,123

Hello,
I am struggling for more than 110 hours (111 now) with a code 1. collecting a genesis 2 figure maps, 2. applying a duf file for a new material with new shader and variable names, 3. refresh the material, 4. and apply the collected maps to the right variables of the new material.
For now I cannot have both initial and final material seen simultaneously in the dsa, the new materials erases the old ones.

So I decided to "store" the old material surface names and associated maps in various arrays, which I should call at the end.

This is why I need to fill these arrays, probably because I am not able to create of fill them properly.

Here is what I have so far :

var bOldRead = true;  if (bOldRead)  {var aFinalNormalMap = new Array  print ('*************************************************Entering Get Material Maps Property 1') for( var i = 0; i < this.m_aNodes.length; i++ ){  this.m_oNode = this.m_aNodes[ i ];  this.m_aMaterials = new Array;    this.m_aMaterialShapes = new Array;  g_oPresetHelper.collectMaterials( this.m_oNode, false, bSelected, bRoot );  this.m_aMaterials = g_oPresetHelper.getCollectedMaterials();  this.m_aMaterialShapes = g_oPresetHelper.getCollectedMaterialShapes();  var aOldSurfacesNames = new Array;  var jindexofMaterial = 0;  var inormalincrementindex = 0;  for( var j = 0; j < this.m_aMaterials.length; j++ )   {this.m_oElement = this.m_aMaterials [ j ];    var oShape = this.m_aMaterialShapes [ j ];    var oLabel = this.m_aMaterialShapes.getLabel;    var oShader = this.m_oElement.getShaderName;            //var aLocalShader = oShader.getName ();    //var oShaderName = oShader.GetName [ j ] ;    //var oShaderName = oShader.getName [ j ]; var sID = this.m_oElement.name;  if( oShape != undefined ){  sID += "_" + oShape.getLabel; } //this.keepMaterialProperties( this.m_aMaterials[ j ], this.m_aMaterialShapes[ j ] ); // DAZ Studio version 4.6.3.49 filetype DAZ Script // Thanks ONCE AGAIN to Rob for providing some parts of the following code. // <a href="http://www.daz3d.com/forums/discussion/42842/">http://www.daz3d.com/forums/discussion/42842/</a>;// Define the list of names you care aboutvar aSurfaceNames = [ "Ears", "Face", "Feet", "Fingernails", "Forearms", "Hands", "Head", "Hips", "Legs",      "Lips", "Neck", "Nipples", "Nostrils", "Shoulders", "Toenails", "Torso" ];//print (j,'IN FIRST FUNCTION THE ELEMENT NAME IS', sID)// Define the name you want to checksFind = sID// Check if the name is in the listvar nIndex = aSurfaceNames.indexOf( sFind );var aOldMaps = new Array;var aOldMappedSurfaces = new Array;if (nIndex > -1) {  var aPROPERTIES = this.m_aMaterials[ j ].getPropertyList(sID);  aOldSurfacesNames[ j ] = oSurfaceName; var oProperty, oOwner, oValue, oNames, oToApply,oSurfaceName, inormalindex ; var inormalindex = -1;   for( var k = 0; k < aPROPERTIES.length; k++ ){  oProperty = aPROPERTIES[ k ]; // on remply l'array    oOwner = oProperty.getOwner();   oValue = oProperty.getValue(); // on attrape la valeur  oNames = oProperty.getName ();  oSurfaceName = oOwner.getName (); // de cette variable  oToApply = this.m_aMaterials[ j ].getProperty (k);  oLabel =oToApply.getLabel ();    //print(i, oSurfaceName +' ' + oNames + ' is a ' + oProperty.className() );    //print (k, oOwner, oNames, oValue);       if ((oLabel == "Normal Map") && oProperty.getValue () !== null) {        var grabNormalMap = oProperty.getValue (k)        var texNormalMapFilename = grabNormalMap.getFilename ();        jindexofMaterial= j+1;        inormalindex = k;        inormalincrementindex = inormalincrementindex + 1;                //print('in the case',oSurfaceName, 'the Normal Map is', texNormalMapFilename)                        }        else {       inormalincrementindex = inormalincrementindex;        var texNormalMapFilename = "null";        //print('in the case',oSurfaceName, 'the Normal Map is', texNormalMapFilename)        }         if (k == inormalindex)       {//print (oSurfaceName,texNormalMapFilename);        aOldMaps[ inormalincrementindex ] = texNormalMapFilename;        aOldMappedSurfaces [ inormalincrementindex ] = oSurfaceName;        //aOldMaps = aOldMaps[ ,texNormalMapFilename];        //aOldMaps = aOldMaps[ , +texNormalMapFilename];                //print ('For', oSurfaceName,' the normal map is ', aOldMaps[ inormalincrementindex ], 'the index is', inormalincrementindex);         }         } // fin de la boucle sur k les properties   //print ('After Propterty loop', 'For', aOldMappedSurfaces,' the normal maps are', aOldMaps, 'the index is', inormalincrementindex)    } // fin de la boucle sur l'indentification de la liste des surfaces    print ('After SURFACE loop', 'For', aOldMappedSurfaces,' the normal maps are', aOldMaps, 'the index is', inormalincrementindex,      'and this is fine')        //rebuild new array????????????????????????????        //for( var l = 0; l < aOldMaps.lentgh; l++ ){     //aFinalNormalMap [ l ] = aOldMaps [ l ]; }// this is absolutely something strange   } //fin de la boucle sur l'ensemble des materials pour chaque node      //print ('After MATERIAL loop', 'For', aOldMappedSurfaces,' the normal maps is', aOldMaps, 'the index is',     //  inormalincrementindex, 'and I need a way out')      }// fin de la boucle sur les nodes i   }

My issue is that the array is not filled as I supposed an array should be. Once I am outside of the main loop, only the last index of the array seems to be kept, the rest is lost. This may be lost during the subloop on surfaces, since when I ask to print, the previous names are replaced by "nothing". An example of what is printed :
[em]After SURFACE loop For ,Legs the normal maps are ,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Limbs_NM.tif the index is 1 and this is fine
After SURFACE loop For the normal maps are the index is 1 and this is fine
After SURFACE loop For ,,Nostrils the normal maps are ,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Face_NM.tif the index is 2 and this is fine
After SURFACE loop For the normal maps are the index is 2 and this is fine
After SURFACE loop For the normal maps are the index is 2 and this is fine
After SURFACE loop For ,,,Lips the normal maps are ,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Face_NM.tif the index is 3 and this is fine
After SURFACE loop For the normal maps are the index is 3 and this is fine
After SURFACE loop For the normal maps are the index is 3 and this is fine
After SURFACE loop For the normal maps are the index is 3 and this is fine
After SURFACE loop For the normal maps are the index is 3 and this is fine
After SURFACE loop For the normal maps are the index is 3 and this is fine
After SURFACE loop For ,,,,Face the normal maps are ,,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Face_NM.tif the index is 4 and this is fine
After SURFACE loop For ,,,,,Toenails the normal maps are ,,,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Limbs_NM.tif the index is 5 and this is fine
After SURFACE loop For the normal maps are the index is 5 and this is fine
After SURFACE loop For ,,,,,,Fingernails the normal maps are ,,,,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Limbs_NM.tif the index is 6 and this is fine
After SURFACE loop For ,,,,,,,Head the normal maps are ,,,,,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Torso_NM.tif the index is 7 and this is fine
After SURFACE loop For ,,,,,,,,Hands the normal maps are ,,,,,,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters/Genesis2/Victoria6/Victoria6_Limbs_NM.tif the index is 8 and this is fine
After SURFACE loop For ,,,,,,,,,Shoulders the normal maps are ,,,,,,,,,F:/DAZ 3D Libraries/Figures/Runtime/Textures/DAZ/Characters....................

So my question is, is there a way to fill an such an array and then keep it out of the loop, else than I did, i.e. with the "aOldMaps[ inormalincrementindex ] = texNormalMapFilename"

By the way, I'm not a scripter, and I'm not sure at all it is the right way to do that, but I have tried so many things that have failed that I have no more clues on what I could now.
By the way, if any scripter sees this thread, I'm so despaired now that I'm ready to hire someone to script this for me, the rest of my product is completely stopped because of this for days and days now, and I would be very disappointed to throw whole my work in the dustbin just because I'm not able to script !

Thanks in advance for your help,

Post edited by Richard Haseltine on

Comments

  • djigneodjigneo Posts: 283
    edited December 1969

    Just from looking at your code, the error may be that you're declaring your variables inside of your for loops.

    Try moving your main array declaration above the first for loop. This explains why your print statements are showing what you want, because on each iteration, the value for i is indeed what you think, but the aOldSurfacesNames array is created freshly each iteration.

    
    when i == 0,
    aOldSurfacesNames[0] == 
    when i == 1
    aOldSurfacesNames[0] = 
    aOldSurfacesNames[1] = 
    when i == 2
    aOldSurfacesNames[0] = 
    aOldSurfacesNames[1] = 
    aOldSurfacesNames[2] = 
    when i == 3
    aOldSurfacesNames[0] = 
    aOldSurfacesNames[1] = 
    aOldSurfacesNames[2] = 
    aOldSurfacesNames[3] = 
    

    and so on.
    So, move

    
    var aOldSurfacesNames = new Array; // move this line here
    for( var i = 0; i < this.m_aNodes.length; i++ ){
    
  • V3DigitimesV3Digitimes Posts: 3,123
    edited December 1969

    djigneo said:
    Just from looking at your code, the error may be that you're declaring your variables inside of your for loops.

    Try moving your main array declaration above the first for loop. This explains why your print statements are showing what you want, because on each iteration, the value for i is indeed what you think, but the aOldSurfacesNames array is created freshly each iteration.

    
    when i == 0,
    aOldSurfacesNames[0] == 
    when i == 1
    aOldSurfacesNames[0] = 
    aOldSurfacesNames[1] = 
    when i == 2
    aOldSurfacesNames[0] = 
    aOldSurfacesNames[1] = 
    aOldSurfacesNames[2] = 
    when i == 3
    aOldSurfacesNames[0] = 
    aOldSurfacesNames[1] = 
    aOldSurfacesNames[2] = 
    aOldSurfacesNames[3] = 
    

    and so on.
    So, move

    
    var aOldSurfacesNames = new Array; // move this line here
    for( var i = 0; i < this.m_aNodes.length; i++ ){
    

    O, thanks, I had not seen this, I'm gonna test that.
    I was also having a look at using the .push, but maybe what you told to me will be enough.

  • V3DigitimesV3Digitimes Posts: 3,123
    edited December 1969

    Good news, it was perfectly enough and my problem is solved. Don't ask me how I managed to reset the array in my loops, this looks so obvious now! Thanks!

  • djigneodjigneo Posts: 283
    edited December 1969

    Great! Thanks for letting me know that was it!

    What are you making, by the way?

  • V3DigitimesV3Digitimes Posts: 3,123
    edited December 1969

    I'm making some special tanned skins for Iray, (take any figure and make it tanned and wet), but the "any figure" is really annoying because I have to transfer all the maps.. And sometimes with some PA textures, I have strange results...
    Oh yeah, the hard thing is that there is not a lot all of the variables passed from 3Delight to Iray with shaders out of shader mixer. Diffuse and Bump are kept, for the rest it requires a script to transfer the maps to the right place once the material has been applied.
    And this is where the nightmare begins... :)

Sign In or Register to comment.