World Recenter script - feedback sought

I've finally dipped my toe into the world of Daz scripting and written my first one. Here's what it does...

There are rendering errors that can occur in Iray if a character is too far away from the world origin. Eyes can turn black. Strange marks show on scalps. Maybe other stuff too. So what I've written is a script to move the selected object to (0,0,0) and everything else in the scene stays in the same place relative to it. The only thing that doesn't move is the Perspective View position (is it possible to move that via scripting?).

I've tested it, and it seems to work, but I'd appreciate a once-over from more experienced eyes. Is there anything that I've missed?

// DAZ Studio version 4.10.0.123 filetype DAZ Script
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables

(function(){

   /*********************************************************************/
   // String : A function for retrieving a translation if one exists

   function text( sText )
   {
      // If the version of the application supports qsTr()
      if( typeof( qsTr ) != "undefined" ){
         // Return the translated (if any) text
         return qsTr( sText );
      }
      // Return the original text
      return sText;
   };

   // Define common message variables
   var sButton = text( "&OK" );

   // Get the selected nodes
   var aNodes = Scene.getSelectedNodeList();
   // Get the number of nodes
   var nNodes = aNodes.length;

   if( nNodes != 1 ){
      // We're done...
      MessageBox.warning( "Please select a single item in the scene before invoking this script", "Selection Error", sButton, "" );
      return;
   }

   var oNode = aNodes[0];

    //Offset is the selected object's position, less any origin positioning
    //This should give the same figures that show in the X/Y/Z Translate properties
    // (for unparented items)

    var vOffset = oNode.getWSPos().subtract(oNode.getOrigin());

    var aAllNodes = Scene.getNodeList();
    var nAllNodes = aAllNodes.length;

    // Iterate over the nodes
    for( var i = 0; i < nAllNodes; i += 1 ){

       // Get the 'current' node
       oNode = aAllNodes[ i ];

       // If the node is a root node, move it by the offset amount
       // if it isn't, there's no need as it'll move with its parent

       if (oNode.isRootNode()) {
          var vNewPos = oNode.getWSPos().subtract(vOffset);
          oNode.setWSPos(vNewPos);
       }
   }

   // Finalize the function and invoke
})();

 

Post edited by chris-2599934 on

Comments

  • Richard HaseltineRichard Haseltine Posts: 80,465
    edited October 2018

    Although Perspective View isn't a selectable camera in the Scene list it is a cam,era and can be manipulated (as a DzNode) like any other http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/camera_dz . To get it you need to get the Dz3DViewport for your active view and use getCamera() http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/3dviewport_dz (ulitmately, http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/viewportmgr_dz from the MainWindow, then get the active viewport from that and the 3D Viewport from that).

    Remember to acknowledge and link the source when using one of rob's sample scripts, as noted in the license terms.

    Post edited by Richard Haseltine on
  • w00tusw00tus Posts: 19
    edited January 2021

    *HOW TO ACTUALLY USE THIS SCRIPT* (Let go of your hair and breath)

    Copy all of the text in the quoted box and paste it into a text editor like notepad or anything that's simple. Save it as [whatever you'll recognise].dsa into the following directory ==> Wherever you're PUBLIC Documents are/My DAZ 3D Library\Scripts\Utilities\MoveScripts. The most important thing is the file extension. It MUST be ".dsa" (Dot Dee Ess A). The other suggestions are just to make your life easier. So that after you refresh your resources or just x out daz and start it again, you can find this dashing young scholar and god amongst men's life changing/hair saving script and you can quit pulling your effing hair out over this black eye issue.

    Before and After using this beautiful, beautiful man's script.

    Just felt like it would he helpful to add this instruction here, since I'm an uber noob who was just about to join an Ahmish community and shun all technology forever over this gee dee cee essing seemingly unfixable and never to be patched render ruining bee effing bug. Had I not already spent like a grand on content for this program, I would have given up after a few hours. Fortunately I persisted and found a link to the estemed appostle chris-2599934's here's great gift to all of humanity.

    We need a tip feature for people like Chris. I'd seriously give him a hundred bucks right now.

    Before-After.jpg
    1596 x 713 - 518K
    Post edited by Chohole on
  • great job ! the time I was wasting to create a group, then unparented etc... thank you very much !

    and thanks to w00tus for the explanation ;) 

    again thank you :)

  • barbultbarbult Posts: 18,515
    edited February 2020
    w00tus said:

    *HOW TO ACTUALLY USE THIS SCRIPT* (Let go of your hair and breath)

    Copy all of the text in the quoted box and paste it into a text editor like notepad or anything that's simple. Save it as [whatever you'll recognise].dsa into the following directory ==> Wherever you're PUBLIC Documents are/My DAZ 3D Library\Scripts\Utilities\MoveScripts. The most important thing is the file extension. It MUST be ".dsa" (Dot Dee Ess A). The other suggestions are just to make your life easier. So that after you refresh your resources or just x out daz and start it again, you can find this dashing young scholar and god amongst men's life changing/hair saving script and you can quit pulling your effing hair out over this black eye issue.

    Before and After using this beautiful, beautiful man's script.

    Just felt like it would he helpful to add this instruction here, since I'm an uber noob who was just about to join an Ahmish community and shun all technology forever over this gee dee cee essing seemingly unfixable and never to be patched render ruining bee effing bug. Had I not already spent like a grand on content for this program, I would have given up after a few hours. Fortunately I persisted and found a link to the estemed appostle chris-2599934's here's great gift to all of humanity.

    We need a tip feature for people like Chris. I'd seriously give him a hundred bucks right now.

    Buy him a gift card in the Daz Store and send him the gift card code in a PM.

    Post edited by Chohole on
  • LoonyLoony Posts: 1,631

    I installed the script now, because I launched a new scene and... I am unsure how it works.

    I loaded a G8F to hope it will spawn in the world centre.

    When I select that and use the script nothing happen, when I select the camera over it and launch the script this happens:

    (I selected the G8F AFTER using the script with camera selected). The camera got spawned with default settings,

    So now is the G8F -400 under the zero Point, is THAT what is called World centre O_o

     

    And what do I have to select to get the best result?

    The script want that I select atleast anything.

    Please help me :)

  • chris-2599934chris-2599934 Posts: 1,585

    The script moves the world center to be wherever the selected object is.

    So, for example, add a G8F to your scene, and move her away from world center - e.g. x=500. Create a camera looking at her, and maybe add some other elements to the scene. Look through the camera you created.

    Now select the G8F and run my script. It will rearrange the scene so that the world center is at her position. All the other objects will be in the same position relative to her, so you should see no change in the scene, except that the world center crosshairs will appear at the G9F's origin. This version of the script doesn't move the Perspective View, so you'll see everything move if you're looking through that instead of an actual camera.

  • LoonyLoony Posts: 1,631
    edited May 2020

    You converted the G8F into a G9F nice! :D (typo joke!)

    So... not the elements does move, the world does move?

    And what if I have 2 cameras?

    Post edited by Loony on
  • chris-2599934chris-2599934 Posts: 1,585
    Loony said:

    You converted the G8F into a G9F nice! :D (typo joke!)

    So... not the elements does move, the world does move?

    And what if I have 2 cameras?

    Both cameras move. Basically, everything that you see in the scene tab will be moved along with the recentered figure so they're all kept in the same place relative to eachother.

  • LoonyLoony Posts: 1,631

    I used the script now, because the pins got so big and the eyes of my char got really black :O

    But there was one thing I forgot: Unlock the cameras or they will NOT follow ;) But I could easy reposition them.

    btw. I would change the thread name to "World Centre Script" or something, I wanted to use my browser cache search and did then remember it was something about "my first script" and not about World centre.

    That others see it faster ;)

  • chris-2599934chris-2599934 Posts: 1,585
    Loony said:

    btw. I would change the thread name to "World Centre Script" or something, I wanted to use my browser cache search and did then remember it was something about "my first script" and not about World centre.

    That others see it faster ;)

    Godd idea. Done.

  • LoonyLoony Posts: 1,631

    Hey, I hope youre looking in this thread :D

    To use the script correct, do i need to unlock everything?

    I get atm my "too far away" problems.

    I wanted to use your script, selected my "main Char" and used it, then I had the feeling nothing changed, I moved something (if I remember) and then did dasz Studio just crash :o

    I use a Big police station so its a bit hard to find a good centre :(

  • LoonyLoony Posts: 1,631
    edited October 2020

    Hi the script seems to have 2 problems.

    1. it does NOT move locked things (cameras, walls etc).

    2. its NOT undoable (bad if you forget to save...; I did^^)

     

    The first problem is really bad... so I would have to unlock ALL! and some objects should not be unlocked because there is a reason, why I lock them :/

    Is it possible you can fix it?

    @chris-2599934

     

    Edit: for some reason does it move some items, but not all, even locked ones.

    Maybe... it moves groups but not single stuff?

    See attachment, the Construct is 1 cube +5 instances (duplicated the 1 cube) and textured.

    any idea why that cube did not got "catched".

    I assume because it was the ONLY LOCKED thing outside of a group.

     

    2020-10-09 21_32_12-000858.jpg
    650 x 379 - 45K
    2020-10-09 21_33_18-000859.jpg
    650 x 431 - 51K
    Post edited by Loony on
  • OmnifluxOmniflux Posts: 248
    edited October 2020

    Here is a version that moves locked nodes

    // DAZ Studio version 4.12.1.118 filetype DAZ Script(function(){	function repositionNode (oNode)	{		if (oNode.isRootNode())		{			// Unlock locked controls before repositioning			var aFunctions = [oNode.getXPosControl, oNode.getYPosControl, oNode.getZPosControl];			var bLockTracker = [false, false, false]			aFunctions.forEach (function (control, i) { if (control().isLocked()) control().lock (bLockTracker[i]++) });			// Reposition node			oNode.setWSPos (oNode.getWSPos().subtract (vecOffset));			// Restore locked controls			aFunctions.forEach (function (control, i) { if (bLockTracker[i]) control().lock (true) });		}	}	if (Scene.getNumSelectedNodes() != 1)		MessageBox.warning ("Please select a single item in the scene before invoking this script", "Selection Error", qsTr ("&amp;OK"), "");	else	{		// Get selected nodes offset from world origin		var vecOffset = Scene.getSelectedNode (0).getWSPos();		beginUndo();		Scene.getNodeList().forEach (repositionNode);		acceptUndo ("Reposition to world center");	}})();

     

    Post edited by Omniflux on
  • LoonyLoony Posts: 1,631

    Thanks, I will save it and try when I get the time.

    --

    Tested: Good thing: it works!

    Even MORE good thing: you can UNDO it, that was the problem with the first script, you had to reload the full scene to undo it.

    So your script is better in 2 ways!

    Bad thing: feeling a bit bad about Chris, like betraying him^^....

  • Thank you so much for these scripts, helped me out big time today.

    Have a very large scene, and i was having issues with hair caps and black eyes.

    All resolved :)

  • madrivermadriver Posts: 12

    Damn! This thing really works! Thank you!!!

  • functionfunction Posts: 168

    Omniflux said:

    Here is a version that moves locked nodes

    // DAZ Studio version 4.12.1.118 filetype DAZ Script(function(){	function repositionNode (oNode)	{		if (oNode.isRootNode())		{			// Unlock locked controls before repositioning			var aFunctions = [oNode.getXPosControl, oNode.getYPosControl, oNode.getZPosControl];			var bLockTracker = [false, false, false]			aFunctions.forEach (function (control, i) { if (control().isLocked()) control().lock (bLockTracker[i]++) });			// Reposition node			oNode.setWSPos (oNode.getWSPos().subtract (vecOffset));			// Restore locked controls			aFunctions.forEach (function (control, i) { if (bLockTracker[i]) control().lock (true) });		}	}	if (Scene.getNumSelectedNodes() != 1)		MessageBox.warning ("Please select a single item in the scene before invoking this script", "Selection Error", qsTr ("&OK"), "");	else	{		// Get selected nodes offset from world origin		var vecOffset = Scene.getSelectedNode (0).getWSPos();		beginUndo();		Scene.getNodeList().forEach (repositionNode);		acceptUndo ("Reposition to world center");	}})();

     

    Thanks for the script, just saw this yesterday, and tried it. However, I found a problem, if there are some cameras in a scene, set them to a camera group, after run this world recenter script, all cameras in the group are weird, can not rotate around the aimed object as normal, the orbit track is weird and difficult to control.

    It cost me 2 hours to find out what is the problem, finally I found it is because the camera group has its own none zero coordinate. Even not use this script, as long as you set some cameras as a new group, the orbit problem happened.

    So here I add some content to OMNI's script, to fix camera group problem:

    // DAZ Studio version 4.12.1.118 filetype DAZ Script// Origin by OMNI at https://www.daz3d.com/forums/discussion/284301/world-recenter-script-feedback-sought// modified by function to fix camera group problem/* Use it: select the object that want to put as world center, run this script, done, and can undo.*/(function(){	function repositionNode (oNode)	{		if (oNode.isRootNode())		{			// Unlock locked controls before repositioning			var aFunctions = [oNode.getXPosControl, oNode.getYPosControl, oNode.getZPosControl];			var bLockTracker = [false, false, false]			aFunctions.forEach (function (control, i) { if (control().isLocked()) control().lock (bLockTracker[i]++) });			// Reposition node			oNode.setWSPos (oNode.getWSPos().subtract (vecOffset));			// Restore locked controls			aFunctions.forEach (function (control, i) { if (bLockTracker[i]) control().lock (true) });		}	}	if (Scene.getNumSelectedNodes() != 1)		MessageBox.warning ("Please select a single item in the scene before invoking this script", "Selection Error", qsTr ("&OK"), "");	else	{		// Get selected nodes offset from world origin		var vecOffset = Scene.getSelectedNode (0).getWSPos();		beginUndo();		Scene.getNodeList().forEach (repositionNode);/**********below script is for CameraGroup_to_WorldCenter*************/        // DAZ Studio version 4.16.0.3 filetype DAZ Script/* This script is to reset all Camera Groups' coordinate to world center 0,0,0 in a scene, to avoid camera orbit problem. */// get all scene nodes listvar aAllNodes = Scene.getNodeList();var nAllNodes = aAllNodes.length;// Iterate over all scene nodesfor (var i1 = 0; i1 < nAllNodes; i1 += 1) {    // Get the 'current' node    var oNode = aAllNodes[i1];    // set its parent node    var oParent = oNode.getNodeParent();    // check if current node is a Camera    if (oNode.isViewCamera) {        // check if node has parent        if (oParent) {            // Get all of the child nodes of the camera parent            var aNodes = oParent.getNodeChildren(true);            // Get the number of nodes            var nNodes = aNodes.length;            // Iterate over all cameras            for (var i = 0; i < nNodes; i += 1) {                // Get the 'current' camera                var oCamera = aNodes[i];    /* get each Camera's World position & rotation, clear its Local position & rotation,	delete all its keyframes, and set its Local p & r to its World p & r. */                var p = oCamera.getWSPos();                var r = oCamera.getWSRot();                oCamera.getXRotControl().deleteAllKeys();                oCamera.getYRotControl().deleteAllKeys();                oCamera.getZRotControl().deleteAllKeys();                oCamera.getXPosControl().deleteAllKeys();                oCamera.getYPosControl().deleteAllKeys();                oCamera.getZPosControl().deleteAllKeys();                oCamera.setLocalPos(p);                 oCamera.setLocalRot(r);            }     // finally clear Camera Group's world position and rotation and delete keyframes.            oParent.getXRotControl().deleteAllKeys();            oParent.getYRotControl().deleteAllKeys();            oParent.getZRotControl().deleteAllKeys();            oParent.getXPosControl().deleteAllKeys();            oParent.getYPosControl().deleteAllKeys();            oParent.getZPosControl().deleteAllKeys();        }    }}/**********end of script for CameraGroup_to_WorldCenter**************/                acceptUndo("Reposition to world center");    }})();

    Don't know why, but JS code appears in one line in this forum. If anyone want to see it, just click ' "QUOTE ', the whole codes will appear in your comment box, copy and use it.

    But still, I wonder why setting a new camera Group will has a none zero coordinate, and why this none zero group coordinate will affect all its cameras' orbit rotation track, is this a Bug?

Sign In or Register to comment.