Surface Selection Script

Before I ask my question I would like to state that I have no idea how to make script or where to even start.

Can a script be made to select all surfaces with a particular named texture map or via material ID colour. This could be for surfaces that cannot be selected via the normal methods easily, say a light bulb in a shade, multiples of. Yes there is a reason I would like to know how or if it is even possible.

 

If it is possible would anyone like to help me achieve this.

«13

Comments

  • IceCrMnIceCrMn Posts: 2,130

    I'm not sure if it's what you are asking for, but if you have the surface selection tool active and you right click on (for example) your characters arms, you get lots of selection options.

    Untitled.jpg
    1917 x 1033 - 479K
  • SzarkSzark Posts: 10,634

    Cheers yes I am well aware of what tools and what functions we have in DS, been using it for over 6 years now, but thank you anyway. For that to work one needs to be able to select the surface first.  The thing is not all surfaces can be accessed via the surface selection tool if the surface is blocked by another, in this case a light shade. To do so is to select the closet surface and use the Frame function and then manaully position the camera so the surface can be seelcted.

    What I would like to offer is an easy way for say Light Bulbs to be selected in one go via a script using material ID or a named texture map. Ideally I would like to be able to do both. This is so a Light On preset can be applied without the need to jump though hoops to select a hidden surface.

  • IceCrMnIceCrMn Posts: 2,130

    ok, I follow you now, that would be handy to have.I can think of a few times something like that would have saved me much clicking.

  • rbtwhizrbtwhiz Posts: 2,267

    Yes, its possible.

    -Rob

  • SzarkSzark Posts: 10,634
    rbtwhiz said:

    Yes, its possible.

    -Rob

    well that is a good start. Do you have time to help Rob?

  • EsemwyEsemwy Posts: 578

    It looks like DzMaterial has a select(bool) method. So, you could iterate through all DzNodes to DzShapes to DzMaterials, look for the attribute you want, then set selected on the material to true.

  • EsemwyEsemwy Posts: 578

    Actually, reading further, it looks like you can iterate directly over DzMaterials without knowing what owns them. DzMaterial has two static methods, getNumMaterials() and getMaterial(int). 

  • SzarkSzark Posts: 10,634

    Esemwy I have no experience in scripting or coding at all, never, so all that great info went straight over me head.

     

    I also had a thought that the script needs to be either by surface name (that appears in the Surface Pane) and/or Material ID. The sets I am working on can have different texture maps for the same light bulbs or the same map but different node names. It would have been great if I we could select a surface in the Surface Pane and then have the option of choosing all mats with the same Mat ID but alas we can't.

    I will need it so I can change the surface name or Mat ID myself for each project.

  • EsemwyEsemwy Posts: 578
    edited September 2015

    I think I can help you out. You'd have to be specific about what criteria you want to select on. Selecting on surface name is easy. Mat ID takes a couple more steps.

    var nNodes = Scene.getNumNodes();for (var i=0; i < nNodes; i++) {	var oNode = Scene.getNode(i)	var oObj = oNode.getObject();	if (! oObj) continue;	var nShapes = oObj.getNumShapes();	for (var j=0; j < nShapes; j++) {		var oShape = oObj.getShape(j);		var nMat = oShape.getNumMaterials();		for (var k=0; k < nMat; k++) {			var oMat = oShape.getMaterial(k);			var sName = oMat.getName();			print(sName);			// or extract properties or whatever then			// if (sName == "SecretSurface") {				// oMat.select(true);			// else...		}	}}

     

    Post edited by Esemwy on
  • SzarkSzark Posts: 10,634

    Oh sweet thanks, man I really appreciate this Esemwy. We can leave at surface name and if there are more than one name I can make mulitple presets to suit each set.

    First off I gather the surface name would go where "SsecretSurface" is?

    and secondly how do I turn that coding in to a DS preset?

    Just one other thing I just thought of, if possible, could this be extended to call up a Material Preset and load that material to the selected surface name. So in my case Turn the light bulb into a Emissive Surface as that is what I would be using it for. So instead of using the script to select and then load the mat preset can it be done in one go. No problem if it can't of if is too much work. I know I am being cheeky but hey if you don't ask. LOL

     

  • EsemwyEsemwy Posts: 578

    Ok, if using the surface name is sufficient, I'll finish it off here. Just copy and paste the script into a .dsa file like "SelectLights.dsa" and put it wherever you need it. Create the 91x91 icon however you usually do.

    var nNodes = Scene.getNumNodes();for (var i=0; i < nNodes; i++) {	var oNode = Scene.getNode(i)	var oObj = oNode.getObject();	if (! oObj) continue;	var nShapes = oObj.getNumShapes();	for (var j=0; j < nShapes; j++) {		var oShape = oObj.getShape(j);		var nMat = oShape.getNumMaterials();		for (var k=0; k < nMat; k++) {			var oMat = oShape.getMaterial(k);			var sName = oMat.getName();			// Change surface name here			if (sName == "SecretSurface") {				oMat.select(true);			}		}	}}

    In answer to your last question, it's probably possible, but I don't know it off the top of my head. I'm willing to look into it, but it'll have to be in the morning.

  • SzarkSzark Posts: 10,634

    Oh Esemwy thanks again. And please don't rush on my account. I gather I can do all this in Notepad and save it as a name of my choosing. I am off to bed myself. Nearly 5 am here and I better get some sleep.

  • EsemwyEsemwy Posts: 578

    Yep, notepad works. You can also paste it into the Script IDE in DAZ and save it from there. It's just a regular text file. Only the .dsa extension is important. 

  • SzarkSzark Posts: 10,634

    yeah sweet I got you on the dsa part too. Thanks again for taking the time to help this scripting dummy.

  • SzarkSzark Posts: 10,634

    Esemwy I did more thinking on this and it might be best to leave at this. Just so people can select the named surfaces again and adjust the light settings.

  • EsemwyEsemwy Posts: 578

    You're quite welcome. I practically had it already written. I did something similar a month or so back.

    Makes sense to stop where we are. I think I learned how to apply a preset, and I'll probably test it out of curiosity, so let me know if you decide you need it. I did find a small bug in my sleep though (happens more often than you'd think).

    Where the script says:

                if (sName == "SecretSurface") {                oMat.select(true);            }

    Should say:

                if (sName == "SecretSurface") {                oMat.select(true);            }            else {                oMat.select(false);            }

    Otherwise, any surfaces that the user already has selected will also be in the selection when the script is done.

  • SzarkSzark Posts: 10,634

    Ok cheers. I have added that now.

    where it says "// Change surface name here" is there any formatting that needs doing or just plain text?

    Personally I think it would be better just to select and not load a mat preset. Then it remains flexible.

     

  • SzarkSzark Posts: 10,634

    Can you do me one little favour please and post the whole script as Notepad is throwing up some issues.

  • EsemwyEsemwy Posts: 578

    The // line is just a comment. It's meant to refer to the line below.

    Here's a link to the file to save you the copy and paste.

  • EsemwyEsemwy Posts: 578

    Now that I'm not so sleepy, I think you may have to do edits in WordPad instead of Notepad. Notepad will probably throw the entire script onto a single line.

  • EsemwyEsemwy Posts: 578

    In case you need it later. You can drop this at the end of the file. It applies the Emission shader to the selected surfaces.

    var oCMgr = App.getContentMgr();var sFullName = oCMgr.findFile("/Shader Presets/Iray/DAZ Uber/Emissive.duf");oCMgr.openNativeFile(sFullName, true);

     

  • SzarkSzark Posts: 10,634
    Esemwy said:

    The // line is just a comment. It's meant to refer to the line below.

    Here's a link to the file to save you the copy and paste.

    Okey pokey so do I need to remove the line "// Change surface name here". So "SecretSurface" that is where I enter the surface name, cool. Oh yeah notepad keeps the formatting but to make sure I am going to use the Script IDE in DS.

    I will test this script tomorrow as the PC is tied up today so I will let you know how I get on. This is going to make life easy for others to load a pre-made mat preset.

  • EsemwyEsemwy Posts: 578

    DAZScript will ignore the // line. That's what the // is for, so that we can put human readable text amid all the gibberish. 

  • SzarkSzark Posts: 10,634

    Oh that  is cool.

  • rbtwhizrbtwhiz Posts: 2,267

    Connection issues and other priorities prevented me from responding again last night; my first response was made while standing on a moving train, on my way home from the office.  Although I see that @Esemwy was able to help, I'll offer an alternate solution (see the Select By Tag sample) that uses the "Tags" property on materials to determine whether or not to select.  This solution is a bit more like the "Select Surfaces with the Tag(s)..." option that is available from the viewport context menu when the Surface Selection Tool is active and you right-click on a surface that has a value in its "Tags" property.  You'll notice that the main function provides a few options that allow you to change how it operates.

    -Rob

  • SzarkSzark Posts: 10,634

    No problem Rob and thanks even more knowing that. :)  I like selecting via Surface name as it is one less step to worry about. But Taging could be an alternative soloution. So which one of thise examples would work for what I want to do. Just in case I need to expand across different surface names?

  • EsemwyEsemwy Posts: 578

    Rob's is certainly comprehensive, and tags are a bit more elegant than surface names. You could either use his as shown, or I can update mine to use tags if you prefer.

  • SzarkSzark Posts: 10,634

    Esemwy as I don't know which of Rob's examples to use could you do a Tag one too, if isn't any trouble. And please don't rush.

  • EsemwyEsemwy Posts: 578

    I added some of Rob's optimizations and cleaned up a bit.

    Download here

  • SzarkSzark Posts: 10,634

    LOL I love the EDIT Here bit....that's what I like idiot proof. a BIG THANK YOU  Esemwy for doing this for me and for the ones that will benifit from the ease of use in the future. Anytime you need my help just shout, if I can I will or at least try. wink

Sign In or Register to comment.