How to Set Shader in DazScript?
I've cobbled together some informal DazScript to find the shader name for each surface selected (see attached). Now, how do I set the shader to a new value for the selected surfaces? For instance, how do I change the shader from "Iray Uber" to "DAZ Studio Default" or vice versa? Thanks in advance for any help!
--John
sel = Scene.getPrimarySelection()
var oObj = sel.getObject();
var oShape = oObj.getCurrentShape();
var aMaterials = oShape.getAllSelectedMaterials();
if( aMaterials.length < 1 ){
aMaterials = oShape.getAllMaterials();
}
alen= aMaterials.length;
for( i=0 ; i<alen ; i++ ){
mat = aMaterials[i];
debug( "mat:", i, "--", mat.getName(), "--", mat.getMaterialName() )
}

Comments
Have you seen this sample https://docs.daz3d.com/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/material/deep_copy/start ?
Thank you, Richard -- promising example. I'll update once I've played with the script some.
Thank you for the assistance, Richard. The script didn't directly fit what I needed, but playing with it did lead me to a solution, that I'll share here in case it helps anyone searching the forum later.
The reason I wanted to set the shader myself in code was so I could do a better job handling opacity maps, especially when doing image masks to identify which pixels of the output image came from which figures/surfaces in the scene.
So two use cases:
Below are 2 informal DAZ script functions and a third debugging function while I tested them.
getDictOpa = function getDictOpa(){
sel = Scene.getPrimarySelection()
var oObj = sel.getObject();
var oShape = oObj.getCurrentShape();
var aMaterials = oShape.getAllSelectedMaterials();
if( aMaterials.length < 1 ){ aMaterials = oShape.getAllMaterials(); }
var dictOpa = [];
alen= aMaterials.length;
for( i=0 ; i<alen ; i++ ){
mat = aMaterials[i];
key= mat.getName()
opa= mat.getOpacityMap();
dictOpa[ key ] = opa;
}
return dictOpa
}
-----
setDictOpa = function setDictOpa( dictOpa ){
sel = Scene.getPrimarySelection()
var oObj = sel.getObject();
var oShape = oObj.getCurrentShape();
var aMaterials = oShape.getAllSelectedMaterials();
if( aMaterials.length < 1 ){ aMaterials = oShape.getAllMaterials(); }
alen= aMaterials.length;
for( i=0 ; i<alen ; i++ ){
mat = aMaterials[i];
key = mat.getName();
mat.setOpacityMap( dictOpa[key] );
}
}
-----
dumpDictOpa = function dumpDictOpa( dictOpa ){
for( var key in dictOpa ){
if( key=="pushIfNotExists" ) continue; // hack
if( key=="find" ) continue; // hack
if( key=="findValue" ) continue; // hack
debug( "dumpdict:", key, "--", dictOpa[key] );
}
}
-----
Usage:
- select figure and optionally surfaces (if no surfaces selected, all are used)
- run this in Script IDE window:
tempopa = getDictOpa()
- select the figure you want to copy the opacity maps to
- run this in the Script IDE window:
setDictOpa( tempopa );
- it applies the copied opacity maps by surface name
These don't directly set the shader like I originally asked (it looked a little hairy assembling a full shader from scratch to apply), but they are workarounds that get the job done -- which is generally good enough
I thought the name used to match, but certainly in DS 6 it now shows as name = Cutout Opacity. I may have been thinking of something else, or it may have been modified (which seems fairly unlikely).