Script to Set Opacity = 0 on Any Selected Surfaces

A script to set the opacity of any selected surfaces (including multiple surfaces) to opacity = 0 would speed up my workflow, but how would you code that in a script?

Comments

  • TaozTaoz Posts: 9,730

    If you select them on both the Scene tab and the Surfaces tab you just have to change Opacity on the one selected on the Surfaces tab, and the rest will follow.  Here I have expanded and selected all objects on both tabs, so that all will be zeroed.  Just make sure that all surfaces have the same shader (Iray or 3DL).  

    https://www.screencast.com/t/TYijPBllJV

    Note that with Iray the shaders settings for some objects may leave some strange shadows in the viewport, you'll usually not see these when you render, but otherwise they can be hidden by setting Glossy Color to black.  See screenshot.

    opacity_shadows.png
    827 x 475 - 90K
  • TaozTaoz Posts: 9,730

    Note btw that some things like the eyes go black on the video, that's because their Opacity already is zeroed by default and then is set to 1.0 when the setting is reversed. 

  • OmnifluxOmniflux Posts: 361

    I had a script that did almost this, so here is a modified version to do exactly this.

     

    // DAZ Studio version 4.12.1.118 filetype DAZ Script(function(){	function clearOpacity (oNode)	{		// Track if we found at least one surface		var bFound = false;		// Shaders are ["DAZ Studio Default & many others", "Iray Uber", "4-Layer Uber PBR MDL"]		var aOpacityNames = ["Opacity Strength", "Cutout Opacity", "Base Cutout Opacity"];		// The opacity properties we are looking for belong to materials which belong to shapes		// which belong to objects which belong to nodes, one of which we were just given.		var oObject = oNode.getObject();		if (!oObject)			return bFound;		var oShape = oObject.getCurrentShape();		if (!oShape)			return bFound;		// For each material we have found		for (var i = 0, nMaterials = oShape.getNumSelectedMaterials(); i < nMaterials; i++)		{			bFound = true;			var oMaterial = oShape.getSelectedMaterial (i);			// Search for an opacity property with a known name			for (var j = 0, oProperty = null; !oProperty && j < aOpacityNames.length; j++)				oProperty = oMaterial.findProperty (aOpacityNames[j]);			// If we did not find an opacity property for this material			if (!oProperty)				App.debug (qsTr ("Opacity property name not known for shader %1").arg (oMaterial.getMaterialName()))						// If we found an opacity property and it is already cleared			else if (oProperty.getValue() == 0)				App.log (qsTr ("Skipping already clear opacity for %1 (%2)")					.arg (oNode.getLabel())					.arg (oMaterial.getLabel()));			// If we found an opacity property and it is not cleared			else			{				oProperty.setValue (0);				App.log (qsTr ("Cleared opacity for %1 (%2)")					.arg (oNode.getLabel())					.arg (oMaterial.getLabel()));			}		}		return bFound;	}	// Get selected nodes	var aNodes = Scene.getSelectedNodeList();	if (aNodes.length > 0)	{		beginUndo();		var bFoundAny = aNodes.map (clearOpacity).some (Boolean);		acceptUndo (qsTr ("Clear Opacity on Material(s)"));		if (bFoundAny)			return;	}	MessageBox.warning (qsTr ("You must select one or more materials to perform this action."), qsTr ("Selection Error"), qsTr ("&OK"), "");})();

     

  • 3dward3dward Posts: 32
    edited October 2020
    Omniflux said:

    I had a script that did almost this, so here is a modified version to do exactly this.

    Thanks. I realize should have asked it to toggle opacity between 100 and 0 based on which it already was. How would you do that? I tried adding oProperty.setValue (100); in the code block with the comment

    // If we found an opacity property and it is already cleared

    after its else if statement, but that just causes an error.

    Post edited by 3dward on
  • OmnifluxOmniflux Posts: 361

    The value is between 0 and 1.

    // DAZ Studio version 4.12.1.118 filetype DAZ Script(function(){	function invertOpacity (oNode)	{		// Track if we found at least one surface		var bFound = false;		// Shaders are ["DAZ Studio Default & many others", "Iray Uber", "4-Layer Uber PBR MDL"]		var aOpacityNames = ["Opacity Strength", "Cutout Opacity", "Base Cutout Opacity"];		// The opacity properties we are looking for belong to materials which belong to shapes		// which belong to objects which belong to nodes, one of which we were just given.		var oObject = oNode.getObject();		if (!oObject)			return bFound;		var oShape = oObject.getCurrentShape();		if (!oShape)			return bFound;		// For each material we have found		for (var i = 0, nMaterials = oShape.getNumSelectedMaterials(); i < nMaterials; i++)		{			bFound = true;			var oMaterial = oShape.getSelectedMaterial (i);			// Search for an opacity property with a known name			for (var j = 0, oProperty = null; !oProperty && j < aOpacityNames.length; j++)				oProperty = oMaterial.findProperty (aOpacityNames[j]);			// If we did not find an opacity property for this material			if (!oProperty)				App.debug (qsTr ("Opacity property name not known for shader %1").arg (oMaterial.getMaterialName()))						// If we found an opacity property but it is not set to either 0 or 1			else if (oProperty.getValue() != 0 && oProperty.getValue() != 1)				App.log (qsTr ("Skipping opacity for %1 (%2) because it is set to %3")					.arg (oNode.getLabel())					.arg (oMaterial.getLabel())					.arg (oProperty.getValue()));			// If we found an opacity property and it *is* set to either 0 or 1			else			{				oProperty.setValue (oProperty.getValue() == 0 ? 1 : 0);				App.log (qsTr ("Inverted opacity for %1 (%2)")					.arg (oNode.getLabel())					.arg (oMaterial.getLabel()));			}		}		return bFound;	}	// Get selected nodes	var aNodes = Scene.getSelectedNodeList();	if (aNodes.length > 0)	{		beginUndo();		var bFoundAny = aNodes.map (invertOpacity).some (Boolean);		acceptUndo (qsTr ("Invert Opacity on Material(s)"));		if (bFoundAny)			return;	}	MessageBox.warning (qsTr ("You must select one or more materials to perform this action."), qsTr ("Selection Error"), qsTr ("&OK"), "");})();

     

  • 3Diva3Diva Posts: 11,287
    Omniflux said:

    The value is between 0 and 1.

    // DAZ Studio version 4.12.1.118 filetype DAZ Script(function(){	function invertOpacity (oNode)	{		// Track if we found at least one surface		var bFound = false;		// Shaders are ["DAZ Studio Default & many others", "Iray Uber", "4-Layer Uber PBR MDL"]		var aOpacityNames = ["Opacity Strength", "Cutout Opacity", "Base Cutout Opacity"];		// The opacity properties we are looking for belong to materials which belong to shapes		// which belong to objects which belong to nodes, one of which we were just given.		var oObject = oNode.getObject();		if (!oObject)			return bFound;		var oShape = oObject.getCurrentShape();		if (!oShape)			return bFound;		// For each material we have found		for (var i = 0, nMaterials = oShape.getNumSelectedMaterials(); i < nMaterials; i++)		{			bFound = true;			var oMaterial = oShape.getSelectedMaterial (i);			// Search for an opacity property with a known name			for (var j = 0, oProperty = null; !oProperty && j < aOpacityNames.length; j++)				oProperty = oMaterial.findProperty (aOpacityNames[j]);			// If we did not find an opacity property for this material			if (!oProperty)				App.debug (qsTr ("Opacity property name not known for shader %1").arg (oMaterial.getMaterialName()))						// If we found an opacity property but it is not set to either 0 or 1			else if (oProperty.getValue() != 0 && oProperty.getValue() != 1)				App.log (qsTr ("Skipping opacity for %1 (%2) because it is set to %3")					.arg (oNode.getLabel())					.arg (oMaterial.getLabel())					.arg (oProperty.getValue()));			// If we found an opacity property and it *is* set to either 0 or 1			else			{				oProperty.setValue (oProperty.getValue() == 0 ? 1 : 0);				App.log (qsTr ("Inverted opacity for %1 (%2)")					.arg (oNode.getLabel())					.arg (oMaterial.getLabel()));			}		}		return bFound;	}	// Get selected nodes	var aNodes = Scene.getSelectedNodeList();	if (aNodes.length > 0)	{		beginUndo();		var bFoundAny = aNodes.map (invertOpacity).some (Boolean);		acceptUndo (qsTr ("Invert Opacity on Material(s)"));		if (bFoundAny)			return;	}	MessageBox.warning (qsTr ("You must select one or more materials to perform this action."), qsTr ("Selection Error"), qsTr ("&OK"), "");})();

     

    Oh my goodness! This works so well! It works on DS default 3DL shaders as well as Iray! Wow - this is hugely helpful, thank you!

Sign In or Register to comment.