Script to toggle "Visible in Viewport" for all lights/cameras?

MartirillaMartirilla Posts: 181

From DAZ Studio 4.11.0.36 (2017) and higher there is an on/off setting in Parameters called "Visible in Viewport". This removes the wireframe of a light and a light's ray-indicators in the Viewport, but does not turn off the light in the scene. This also works the same for cameras.

I now wish to bodge together a script that toggles this visibility on-and-off, for all lights and cameras in a scene, with one click.

But I am a Python and not a Daz scripter. So initially I'm asking: is the official template script for 'node visibility toggle' (linked below) a good starting point for this?  Also, does "Visible in Viewport" need to be referenced in any special way or have a special name in a Daz script?

http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/nodes/node_visibility_toggle/start

Post edited by Martirilla on

Comments

  • Richard HaseltineRichard Haseltine Posts: 101,482
    edited March 2022

    DzNode has setVisibleInViewport(bool) and bool isVisibleInViewport() methods if you go diving. These are not documented in the current docs, so subject to change, but I think they have been in the relevant change logs so should be fairly safe.

    To make sure you target the right things add a check that oNode.inherits( "DzCamera" ) - scene lights are all a special kind of camera.

    Post edited by Richard Haseltine on
  • MartirillaMartirilla Posts: 181

    Thanks. Here is the working script. It does what's needed without errors. But as I said... it's probably a bodge in several respects.

     

    // Script to toggle the visibility of lights and cameras in the Daz Studio scene Viewport. // Just toggles their wireframe visibility on/off only, in the Viewport. Does not destroy // scene lights or cameras - e.g. lights remain still present and shining on the scene.// Requires DAZ Studio 4.11.0.383 or higher. // Derived from Daz's official Node_Visibility_Toggle.dsa script - and has same license.// Define an anonymous function; serves as our main loop, and 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;	}; 	/*********************************************************************/	// Declare working variable for lights	var toggleLights;	// Get the selectable light nodes in the scene	var LightList = Scene.getLightList();	// Capture the number of selected lights	var allLights = LightList.length;	// If the scene has no lights to select	if( allLights < 1 ){		// Then inform the user		MessageBox.warning(			text( "You must have one or more scene lights present, to run this script." ),			text( "Selection Error" ), text( "&OK" ), "" );	// If lights have been selected, then continue.	} else {		// Start collecting user undo changes		beginUndo(); 		// Iterate over the collected list of scene lights		for( var i = 0; i < allLights; i += 1 ){			// Step through each light in turn, temporarily considering each light to be 'current'			toggleLights = LightList[i];			// Toggle Viewport visibility of the 'current' light, then go on to do the same on the next			toggleLights.setVisibleInViewport( !toggleLights.isVisibleInViewport() );		} 		// Finish collecting user undo changes and add the named undo to the stack		acceptUndo( text( "Toggle Viewport Light Visibility" ) );	}  	// Declare working variable for cameras	var toggleCameras;	// Get the selectable camera nodes in the scene	var CameraList = Scene.getCameraList();	// Capture the number of selected cameras	var allCameras = CameraList.length;	// If the scene has not even one camera to select	if( allCameras < 1 ){		// Then inform the user		MessageBox.warning(			text( "You must have one or more scene cameras present, to run this script." ),			text( "Selection Error" ), text( "&OK" ), "" );	// If lights have been selected, then continue.	} else {		// Start collecting user undo changes		beginUndo(); 		// Iterate over the collected list of scene cameras		for( var i = 0; i < allCameras; i += 1 ){			// Step through each camera in turn, temporarily considering each camera to be 'current'			toggleCameras = CameraList[i];			// Toggle Viewport visibility of the 'current' camera, then go on to do the same on the next			toggleCameras.setVisibleInViewport( !toggleCameras.isVisibleInViewport() );		} 		// Finish collecting user undo changes and add the named undo to the stack		acceptUndo( text( "Toggle Viewport Camera Visibility" ) );	}// Finalize the function and invoke})();

     

  • MartirillaMartirilla Posts: 181
    edited March 2022

    Oh great... code insertion no longer displays the code properly. How to view as a script, and not as one single long and thoroughly garbled line?

    Post edited by Martirilla on
  • MartirillaMartirilla Posts: 181

    Ok, I'm now seeing if a DSA file attachment will work better, if the code can't display properly in the post itself.

     

     

    dsa
    dsa
    toggle-viewport-lights-camera-working.dsa
    3K
  • MartirillaMartirilla Posts: 181

    Here's how to de-mangle the one-line code-posting, which the Daz forum's scripting sub-forum seems to have adopted since I was last here.

    1. Click on the "Quote" button, on such a post.

    2. The code and script now displays as it should. 

    3. Right-click on the code. "Copy". Exit and don't post the quote in the forum.

    4. Paste what you copied to Notepad++ or Visual Studio Code or some other other useful free code editor. The basic Windows Notepad works, keeping the required indenting.

    5. Top-and-tail the script. Because you will likely have a blank line that needs to be removed at the start of the script, and also a line reading "Click and drag to move" at the bottom.

  • Richard HaseltineRichard Haseltine Posts: 101,482

    Please make sure you obey the CC 3 by attribution terms for the use of sample scripts - I thought you had, but that must have been another script. You can most readily do this by downloading the code from the sample page (link above the code on the page) which will include the necessary sections in the opening header, then make your changes.

  • MartirillaMartirilla Posts: 181

    Attribution is given in the script header, on both posted versions of the script: "Derived from Daz's official Node_Visibility_Toggle.dsa script". 

  • The link and license type are not given, though

Sign In or Register to comment.