Update Prop Origin Only Working If I Display Message Boxes ??? [SOLVED - Stupid! Again... :) ]

3dcheapskate3dcheapskate Posts: 2,719

I have a section of code (from about line 1100 of a 1600 line script) to move the origin of the currently selected prop to the world origin while maintaining the prop's position in world space. It works fine:


 // 3 - Move Prop Origin To World Origin
 
 // Display current values
 tx=oProp.getXPosControl().getValue()
 ty=oProp.getYPosControl().getValue()
 tz=oProp.getZPosControl().getValue()
 ox=oProp.getOriginXControl().getValue()
 oy=oProp.getOriginYControl().getValue()
 oz=oProp.getOriginZControl().getValue()
 sMsg += "

Current XYZ Translations: "+tx.toFixed(3)+","+ty.toFixed(3)+","+tz.toFixed(3)+"
Current Origin: "+ox.toFixed(3)+","+oy.toFixed(3)+","+oz.toFixed(3) if (g_dsVer==3){ // OffsetA/B only exist in DS3 (and always seem to be zero, but just in case...) ax=oProp.getXOffsetAControl().getValue() ay=oProp.getYOffsetAControl().getValue() az=oProp.getZOffsetAControl().getValue() bx=oProp.getXOffsetBControl().getValue() by=oProp.getYOffsetBControl().getValue() bz=oProp.getZOffsetBControl().getValue() sMsg+="
Current OffsetA: "+ax.toFixed(3)+","+ay.toFixed(3)+","+az.toFixed(3)+"
Current OffsetB: "+bx.toFixed(3)+","+by.toFixed(3)+","+bz.toFixed(3) } MessageBox.information(sMsg, "DEBUG", "&OK;", "" ); // Modify prop origin setBusyCursor(); beginUndo(); var vOri = new DzVec3(-tx,-ty,-tz); var vOriInv = new DzVec3(tx,ty,tz); oProp.setOrigin(vOri, true); if (g_dsVer==3){ oProp.setOffsetA(vOri, true); oProp.setOffsetB(vOriInv, true); } acceptUndo( g_sTOOL_NAME + " (origin to world centre)"); clearBusyCursor(); // Display modified values tx=oProp.getXPosControl().getValue() ty=oProp.getYPosControl().getValue() tz=oProp.getZPosControl().getValue() ox=oProp.getOriginXControl().getValue() oy=oProp.getOriginYControl().getValue() oz=oProp.getOriginZControl().getValue() sMsg="<br><b>Unchanged XYZ Translations:</b> "+tx.toFixed(3)+","+ty.toFixed(3)+","+tz.toFixed(3)+"
Adjusted Origin: "+ox.toFixed(3)+","+oy.toFixed(3)+","+oz.toFixed(3) if (g_dsVer==3){ // OffsetA/B only exist in DS3 (and always seem to be zero, but just in case...) ax=oProp.getXOffsetAControl().getValue() ay=oProp.getYOffsetAControl().getValue() az=oProp.getZOffsetAControl().getValue() bx=oProp.getXOffsetBControl().getValue() by=oProp.getYOffsetBControl().getValue() bz=oProp.getZOffsetBControl().getValue() sMsg+="
Adjusted OffsetA: "+ax.toFixed(3)+","+ay.toFixed(3)+","+az.toFixed(3)+"
Adjusted OffsetB: "+bx.toFixed(3)+","+by.toFixed(3)+","+bz.toFixed(3) } MessageBox.information(sMsg, "DEBUG", "&OK;", "" );


But if I DON'T display the before and after message boxes and just do this then the prop origin doesn't appear to be moved at all !


 // 3 - Move Prop Origin To World Origin
 
 // Modify prop origin
 setBusyCursor();
 beginUndo();
 var vOri = new DzVec3(-tx,-ty,-tz);
 var vOriInv = new DzVec3(tx,ty,tz);
 oProp.setOrigin(vOri, true);
 if (g_dsVer==3){
  oProp.setOffsetA(vOri, true);
  oProp.setOffsetB(vOriInv, true);
 }
 acceptUndo( g_sTOOL_NAME + " (origin to world centre)");
 clearBusyCursor();

Any ideas?

Post edited by 3dcheapskate on

Comments

  • 3dcheapskate3dcheapskate Posts: 2,719
    edited April 2015

    A Bit Of Further Clarification

    I should probably add that the prop in question will have been set up in a specific way before running this:

    1) First it's 'zeroed' and parented to a 'DummyHand' proxy prop (the script below does this).The prop origin will now be at the world origin.

    
     // 1 - Unparent,Zero And Proxy
     
     setBusyCursor();
     beginUndo();
     
     // Unparent the prop if required
     if (oParent)
      oParent.removeNodeChild(oProp);
     // Zero the translations and rotations
     g_oPresetHelper.setTargetElement( oProp );
     g_oPresetHelper.setTransformProperty( "XTranslate",  [ 0 ] );
     g_oPresetHelper.setTransformProperty( "YTranslate",  [ 0 ] );
     g_oPresetHelper.setTransformProperty( "ZTranslate",  [ 0 ] );
     g_oPresetHelper.setTransformProperty( "XRotate",  [ 0 ] );
     g_oPresetHelper.setTransformProperty( "YRotate",  [ 0 ] );
     g_oPresetHelper.setTransformProperty( "ZRotate",  [ 0 ] );
     // Zero the origin (and offsets in DS3)
     var vZero = new DzVec3(0,0,0);
     oProp.setOrigin(vZero, true);
     if (g_dsVer==3){
      oProp.setOffsetA(vZero, true);
      oProp.setOffsetB(vZero, true);
     }
     
     // Zero the orientation
     var qZero = new DzQuat(0,0,0,1);  // DzQuat(x,y,z,w) and NOT w,x,y,z as I thought originally
     oProp.setOrientation(qZero);  
     acceptUndo( g_sTOOL_NAME + " (Unparent and zero)");
     clearBusyCursor();
      
     // Load a 'DummyHand' proxy
     // (and make a note of the node so we can parent to it)
     origNodes = Scene.getNodeList();   // make a temporary note of all the nodes before we load the proxy
     oNewProxy = undefined;               // this will be the node for the newly added proxy
     var oImportMgr = App.getImportMgr();
     spropFile=convertPoserPathToAbsolute(g_sDummyHandProxyFile)
     if (oImportMgr.canImport(spropFile)){  
      poserImporter = oImportMgr.findImporter(spropFile)
      if (poserImporter){
       if (!poserImporter.readFile(spropFile))
        print("Error trying to import the DummyHand proxy prop file")
       else
        print("DummyHand proxy imported - finding node")
        var newNodes =  Scene.getNodeList();
        for( n = 0; n < newNodes.length && !oNewProxy ; n++ ) {
         oNewProxy = newNodes[ n ];
         for ( m = 0 ; m < origNodes.length && oNewProxy ; m++ ) {
          if ( oNewProxy == origNodes[ m ] )
           oNewProxy = undefined;
         }
        }
      }else
       print("oImportMgr can't findImporter for the DummyHand proxy prop file")
     }else
      print("oImportMgr can't canImport for the DummyHand proxy prop file")
     
     // parent the prop to the proxy 
     setBusyCursor();
     beginUndo();
     if (oNewProxy)
      oNewProxy.addNodeChild(oProp); // ~~~ as per the original (which works!), no second parameter given (so inPlace=false)
      
     acceptUndo( g_sTOOL_NAME + " (Parent to proxy)");
     clearBusyCursor();
    

    2) It will have been translated so that the point I want to be the centre of rotation is at the origin. If any translations are applied then the prop origin will move away from the world origin. It is under these circumstances that I get the problem.
    
     // 2 - Translate the prop to the world origin
     
     // Get the values I've already set up for the translations
     xt=g_handproxylist[hplcnt][6];
     yt=g_handproxylist[hplcnt][7];
     zt=g_handproxylist[hplcnt][8];
     
     // Convert to DAZ Studio values
     xt*=243.8; // Poser units to DAZ Studio units conversion factor
     yt*=243.8;
     zt*=243.8;
     
     setBusyCursor();
     beginUndo();
     
     // Apply the translations
     g_oPresetHelper.setTargetElement( oProp );
     g_oPresetHelper.setTransformProperty( "XTranslate",  [ xt ] );
     g_oPresetHelper.setTransformProperty( "YTranslate",  [ yt ] );
     g_oPresetHelper.setTransformProperty( "ZTranslate",  [ zt ] );
     
     acceptUndo( g_sTOOL_NAME + " (apply HANDPROXYDATA translations)" );
     clearBusyCursor(); 
    

    Then after I've moved the prop origin back to the world origin as in the OP I simply rotate the prop. That's how I know that the origin hasn't been moved correctly in the OP
    
     // 4 - Apply the rotations that I've already set up
     
     yr=g_handproxylist[hplcnt][3];
     xr=g_handproxylist[hplcnt][4];
     zr=g_handproxylist[hplcnt][5];
     
     setBusyCursor();
     beginUndo();
     
     // Apply the rotations
     g_oPresetHelper.setTargetElement( oProp );
     g_oPresetHelper.setTransformProperty( "XRotate",  [ xr ] );
     g_oPresetHelper.setTransformProperty( "YRotate",  [ yr ] );
     g_oPresetHelper.setTransformProperty( "ZRotate",  [ zr ] ); 
     
     acceptUndo( g_sTOOL_NAME + " (apply HANDPROXYDATA rotations)" );
     clearBusyCursor(); 
    
    Post edited by 3dcheapskate on
  • 3dcheapskate3dcheapskate Posts: 2,719
    edited November 2015

    Hmmm... It Looks Like It's Something To Do With The Other 1400-Or-So Lines Of Code In That Script...

    Okay, my next step was to take my 1600-or-so-line script and extract just the bits that do the stuff I'm talking about.

    That results in the 241 line script below, which seems to do exactly what it's supposed to, regardless of whether or not the message boxes are displayed.(You need to have the M4 Journeyer Scout 'In Hand' katana loaded and it should be the only thing in the scene that's selected)
    - I've hard-coded the rotation/translation parameters for the M4 Journeyer Scout 'In Hand' katana (the 'In Scabbard'katana requires different values).
    - I've commented out the loading/parenting to a DummyHand proxy, so that anybody can run this script.
    - I've also commented out all the messageboxes.

    If I uncomment all the commented out message boxes and DummyHand loading/parenting stuff it also works as it's supposed to

    Soooo... I'm wondering if it's something to do with the other 1400-or-so lines of code?Especially since I had another still-unresolved problem (but with a different script) where the problem lines work fine in isolation... ("“WARNING: ReferenceError: getNodeParent is not defined” for DzDistantLight::getNodeParent() ??" also in this forum)

    Anyway, here's the 241-line version:

    // OriginProblemTest.dsa/* Cropped from Generic 1.01c-01*/// These are set in accordance with the arguments passed to the scriptvar sVersion = &quot;&quot;; // Will be compared with the SCRIPT_VERSION below to ensure compatibilityvar sTarget = &quot;&quot;; // Can be &quot;hand&quot; or &quot;head&quot;var sType = &quot;&quot;; // If sTarget=&quot;hand&quot;, can be &quot;rod&quot; or &quot;rod1&quot;. If sTarget=&quot;head&quot; must be &quot;hat&quot;var g_oStringHelper = new DzStringHelper;var g_oPresetHelper = new DzPresetHelper;var g_oSceneHelper = new DzSceneHelper;const g_sTOOL_NAME = &quot;Generic Prop Goto+&quot;;var g_dsVer = 0; // DAZ Studio major version number (integer - 0 if unable to get it)var g_sDummyHandProxyFile = &quot;:Runtime:Libraries:Props:3DCheapskate:SmartPlus:Basic:DummyHand.pp2&quot;// sPoserFile is a Poser path+file+ext in MAC format starting :Runtime:// Return value is the absolute path+file+ext, or a null string if it wasn&#39;t foundfunction convertPoserPathToAbsolute(sPoserFile){ sTemp = &quot;&quot; for (i=0;i==&quot;:&quot;)   sTemp += &quot;\\&quot;;  else   sTemp += sPoserFile[ i ]; sAbsPath=App.getContentMgr().getAbsolutePath(sTemp,true); return sAbsPath;}/********************************************* G E N E R I C P R O P G O T O P L U S ( ) *********************************************/function OriginProblemTest(){  // Get the DS version number, main version only    try {        g_dsVer = +App.versionString[0];    }catch(e){        g_dsVer = 0;    }      // Get the number of selected nodes var nNodes = Scene.getNumSelectedNodes(); var aNodes = g_oSceneHelper.collectNodes( true, false, false ); // selected, recurse, from root  if (nNodes!=aNodes.length){  print (&quot;UNEXPECTED ERROR: nNodes=&quot;+nNodes+&quot;, aNodes.length=&quot;+aNodes.length);  //MessageBox.information(&quot;UNEXPECTED ERROR: nNodes=&quot;+nNodes+&quot;, aNodes.length=&quot;+aNodes.length,&quot;UNEXPECTED ERROR&quot;,&quot;&amp;OK;&quot;) }  // If there&#39;s too few nodes selected then abort - let user reselect and try again (alternative is to list all figures/nodes in scene) if( nNodes == 0 ){  print (&quot;No nodes selected - cancelled.&quot;);  MessageBox.information( &quot;Please select a single node and try again.Operation cancelled.&quot;, &quot;No Nodes Selected&quot;, &quot;OK&quot; );  dropout = true; }else if (nNodes&gt;1){  print (&quot;Too many nodes selected - cancelled.&quot;);  MessageBox.information( &quot;Please select a single node and try again.Operation cancelled.&quot;, &quot;Too Many Nodes Selected&quot;, &quot;OK&quot; ); }else{ // Exactly one nodes  print (&quot;One node selected - continuing...&quot;);  var oProp = new DzNode;  oProp = aNodes[0];    if (oProp){   print(&quot;oProp.name=&quot;+oProp.name)   MessageBox.information(&quot;1 oProp.name=&quot;+oProp.name, &quot;ALERT_CALL&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );         // 1 - Unparent,Zero And Proxy   // +++ This bit copied directly from &#39;ToDefaultGeometryPosition.dsa&#39; +++   setBusyCursor();   beginUndo();      // Check whether it&#39;s parented   var oParent=oProp.getNodeParent();       // Unparent the prop if required   if (oParent)    oParent.removeNodeChild(oProp);   // Zero the translations and rotations   g_oPresetHelper.setTargetElement( oProp );   g_oPresetHelper.setTransformProperty( &quot;XTranslate&quot;,  [ 0 ] );   g_oPresetHelper.setTransformProperty( &quot;YTranslate&quot;,  [ 0 ] );   g_oPresetHelper.setTransformProperty( &quot;ZTranslate&quot;,  [ 0 ] );   g_oPresetHelper.setTransformProperty( &quot;XRotate&quot;,  [ 0 ] );   g_oPresetHelper.setTransformProperty( &quot;YRotate&quot;,  [ 0 ] );   g_oPresetHelper.setTransformProperty( &quot;ZRotate&quot;,  [ 0 ] );   // Zero the origin (and offsets in DS3)   var vZero = new DzVec3(0,0,0);   oProp.setOrigin(vZero, true);   if (g_dsVer==3){    oProp.setOffsetA(vZero, true);    oProp.setOffsetB(vZero, true);   }   // Zero the orientation   var qZero = new DzQuat(0,0,0,1);  // DzQuat(x,y,z,w) and NOT w,x,y,z as I thought originally   oProp.setOrientation(qZero);     acceptUndo( g_sTOOL_NAME + &quot; (Unparent and zero)&quot;);   clearBusyCursor();/*      // Load a &#39;DummyHand&#39; proxy   // (and make a note of the node so we can parent to it)   origNodes = Scene.getNodeList();   // make a temporary note of all the nodes before we load the proxy   oNewProxy = undefined;               // this will be the node for the newly added proxy   var oImportMgr = App.getImportMgr();   spropFile=convertPoserPathToAbsolute(g_sDummyHandProxyFile)   if (oImportMgr.canImport(spropFile)){      poserImporter = oImportMgr.findImporter(spropFile)    if (poserImporter){     if (!poserImporter.readFile(spropFile))      print(&quot;Error trying to import the DummyHand proxy prop file&quot;)     else      print(&quot;DummyHand proxy imported - finding node&quot;)      var newNodes =  Scene.getNodeList();      for( n = 0; n &lt; newNodes.length &amp;&amp; !oNewProxy ; n++ ) {       oNewProxy = newNodes[ n ];       for ( m = 0 ; m &lt; origNodes.length &amp;&amp; oNewProxy ; m++ ) {        if ( oNewProxy == origNodes[ m ] )         oNewProxy = undefined;       }      }    }else     print(&quot;oImportMgr can&#39;t findImporter for the DummyHand proxy prop file&quot;)   }else    print(&quot;oImportMgr can&#39;t canImport for the DummyHand proxy prop file&quot;)      // parent the prop to the proxy    setBusyCursor();   beginUndo();   if (oNewProxy)    oNewProxy.addNodeChild(oProp); // ~~~ as per the original (which works!), no second parameter given (so inPlace=false)   acceptUndo( g_sTOOL_NAME + &quot; (Parent to proxy)&quot;);   clearBusyCursor();           */   //   MessageBox.information(&quot;Done UZAP...&quot;, &quot;DEBUG&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );       // 2 - Manually translate the prop   // Position the prop in accordance with the hand proxy parameters... (JSkatanaInHand values)   xt=0.323;   yt=-0.6073;   zt=-0.063;   // Convert to DAZ Studio values   xt*=243.8; // Poser units to DAZ Studio units conversion factor   yt*=243.8;   zt*=243.8;   // Prepare for alterations to the scene   setBusyCursor();   beginUndo();   // Apply the hand proxy parameters to move and orient the prop for the zeroed dummy hand proxy   g_oPresetHelper.setTargetElement( oProp );   g_oPresetHelper.setTransformProperty( &quot;XTranslate&quot;,  [ xt ] );   g_oPresetHelper.setTransformProperty( &quot;YTranslate&quot;,  [ yt ] );   g_oPresetHelper.setTransformProperty( &quot;ZTranslate&quot;,  [ zt ] );   // End alterations to the scene   acceptUndo( g_sTOOL_NAME + &quot; (apply HANDPROXYDATA translations)&quot; );   clearBusyCursor();              //   MessageBox.information(&quot;Done translations...&quot;, &quot;DEBUG&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );      // 3 - Move Prop Origin To World Origin/*      // Get current values for display to user   tx=oProp.getXPosControl().getValue()   ty=oProp.getYPosControl().getValue()   tz=oProp.getZPosControl().getValue()   ox=oProp.getOriginXControl().getValue()   oy=oProp.getOriginYControl().getValue()   oz=oProp.getOriginZControl().getValue()   sMsg = &quot;<strong>Current XYZ Translations: </strong>&quot;+tx.toFixed(3)+&quot;,&quot;+ty.toFixed(3)+&quot;,&quot;+tz.toFixed(3)+&quot;<strong>Current Origin: </strong>&quot;+ox.toFixed(3)+&quot;,&quot;+oy.toFixed(3)+&quot;,&quot;+oz.toFixed(3)   if (g_dsVer==3){ // OffsetA/B only exist in DS3 (and always seem to be zero, but just in case...)    ax=oProp.getXOffsetAControl().getValue()    ay=oProp.getYOffsetAControl().getValue()    az=oProp.getZOffsetAControl().getValue()    bx=oProp.getXOffsetBControl().getValue()    by=oProp.getYOffsetBControl().getValue()    bz=oProp.getZOffsetBControl().getValue()    sMsg+=&quot;<strong>Current OffsetA: </strong>&quot;+ax.toFixed(3)+&quot;,&quot;+ay.toFixed(3)+&quot;,&quot;+az.toFixed(3)+&quot;<strong>Current OffsetB: </strong>&quot;+bx.toFixed(3)+&quot;,&quot;+by.toFixed(3)+&quot;,&quot;+bz.toFixed(3)   }   MessageBox.information(sMsg, &quot;DEBUG&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );   setBusyCursor();   beginUndo();*/      // Set the origin to the inverse of the prop translations   var vOri = new DzVec3(-tx,-ty,-tz);   var vOriInv = new DzVec3(tx,ty,tz);   oProp.setOrigin(vOri, true);   // Set offsetA the same as the origin, and offsetB to the inverse   if (g_dsVer==3){    oProp.setOffsetA(vOri, true);    oProp.setOffsetB(vOriInv, true);   }   acceptUndo( g_sTOOL_NAME + &quot; (origin to world centre)&quot;);   clearBusyCursor(); /*      // Display new (zeroed) values to user   tx=oProp.getXPosControl().getValue()   ty=oProp.getYPosControl().getValue()   tz=oProp.getZPosControl().getValue()   ox=oProp.getOriginXControl().getValue()   oy=oProp.getOriginYControl().getValue()   oz=oProp.getOriginZControl().getValue()   sMsg=&quot;&lt;br&gt;&lt;b&gt;Unchanged XYZ Translations:&lt;/b&gt; &quot;+tx.toFixed(3)+&quot;,&quot;+ty.toFixed(3)+&quot;,&quot;+tz.toFixed(3)+&quot;<strong>Adjusted Origin:</strong> &quot;+ox.toFixed(3)+&quot;,&quot;+oy.toFixed(3)+&quot;,&quot;+oz.toFixed(3)   if (g_dsVer==3){ // OffsetA/B only exist in DS3 (and always seem to be zero, but just in case...)    ax=oProp.getXOffsetAControl().getValue()    ay=oProp.getYOffsetAControl().getValue()    az=oProp.getZOffsetAControl().getValue()    bx=oProp.getXOffsetBControl().getValue()    by=oProp.getYOffsetBControl().getValue()    bz=oProp.getZOffsetBControl().getValue()    sMsg+=&quot;<strong>Adjusted OffsetA:</strong> &quot;+ax.toFixed(3)+&quot;,&quot;+ay.toFixed(3)+&quot;,&quot;+az.toFixed(3)+&quot;<strong>Adjusted OffsetB:</strong> &quot;+bx.toFixed(3)+&quot;,&quot;+by.toFixed(3)+&quot;,&quot;+bz.toFixed(3)               }   MessageBox.information(sMsg, &quot;DEBUG&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );*/   //   MessageBox.information(&quot;Done move origin...&quot;, &quot;DEBUG&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );       // 4 - Manually rotate the prop   // Position the prop in accordance with the hand proxy parameters  (JSkatanaInHand values)   yr=16.0;   xr=22.0;   zr=76.0;   // Prepare for alterations to the scene   setBusyCursor();   beginUndo();   // Apply the hand proxy parameters to move and orient the prop for the zeroed dummy hand proxy   g_oPresetHelper.setTargetElement( oProp );   g_oPresetHelper.setTransformProperty( &quot;XRotate&quot;,  [ xr ] );   g_oPresetHelper.setTransformProperty( &quot;YRotate&quot;,  [ yr ] );   g_oPresetHelper.setTransformProperty( &quot;ZRotate&quot;,  [ zr ] );    // End alterations to the scene   acceptUndo( g_sTOOL_NAME + &quot; (apply HANDPROXYDATA rotations)&quot; );   clearBusyCursor();              //   MessageBox.information(&quot;Done rotations...&quot;, &quot;DEBUG&quot;, &quot;&amp;OK;&quot;, &quot;&quot; );  } }}OriginProblemTest();

     

    Post edited by Richard Haseltine on
  • 3dcheapskate3dcheapskate Posts: 2,719
    edited July 2015

    Solved, I Think !

    (aka... it's nice when you spot your own stupid mistake before anybody else does ! ...)

    If anybody else spots it feel free to post here... LOL

    (hint - I'd commented out something that should have been left in!) :oS

    Post edited by 3dcheapskate on
Sign In or Register to comment.