How to change surface name by using script correctly?

Here is the problem:

When I set surface name with DzMaterial.setName() funciton, The new name can show at the tool setting pannel but not at surface panel,when i save file,the texture will lost.

 

Comments

  • As far as I understand, DzMaterial is not the surface group - note that a given material can belong to an array of shapes DzMaterial.getShapeList() . Unfortunately DzShape does not currently have documentation - I think a lot of the geometry-related objects were removed to avoid spilling the beans during a previous development cycle - so if you need to access those features you may have to go object-diving, bearing in mind that what is not docuemnted is subkject to change without notice (and indeed, if changes planned/in progress that may be another/the actual reason for the lack of documentation).

  • OmnifluxOmniflux Posts: 360

    For the first part of your question, I suspect the issue is name vs label.

    var oMaterial = Scene.getPrimarySelection().getObject().getCurrentShape().getMaterial (0);oMaterial.setName ('Alpha');oMaterial.setLabel ('Beta');

    This will result in

    image

    However, the label is not a saved property. If you save and reload this node, it will load with both name and label set to Alpha.

     

    It is not clear what you mean by the texture being lost after saving.

     

     

    Untitled.png
    307 x 563 - 51K
  • Problem solved,Thank your very much!

  • Silas3DSilas3D Posts: 554
    edited February 2022

    Hi everyone, sorry to hijack this thread but I'm trying to do a similar thing and rename a surface with a particular name. For simplicities sake lets use the same names as above, so I want to rename the `Alpha` surface to `Beta`.

    As someone that's new to DAZ scripting I find the samples pretty difficult to understand, but the deepCopyMaterial script is at least partially understandable. This script has multiple variables for getting the current shape etc, so in context of these how would I rename the surface?

    // Get the objectsvar oSrcObject = oSrcNode.getObject();var oTgtObject = oTgtNode.getObject();		// If we don't have both objectsif( !oSrcObject || !oTgtObject ){	// We're done...	return false;}		// Get the current shapesvar oSrcShape = oSrcObject.getCurrentShape();var oTgtShape = oTgtObject.getCurrentShape();// If we don't have both shapesif( !oSrcShape || !oTgtShape ){	// We're done...	return false;}		// Get the materialsvar oSrcMaterial = oSrcShape.findMaterial( sSrcMaterial );var oTgtMaterial = oTgtShape.findMaterial( sTgtMaterial );

     

    EDIT: Sorry about the formatting, the forum doesn't save the code snippet as its displayed in edit mode :o( have attached a screenshot.

    Screenshot 2022-02-08 at 07.02.23.png
    1014 x 1038 - 156K
    Post edited by Silas3D on
  • OmnifluxOmniflux Posts: 360
    edited February 2022
    var oTgtNode = Scene.getPrimarySelection();if (!oTgtNode)	return false;var oTgtObject = oTgtNode.getObject();if (!oTgtObject)	return false;		var oTgtShape = oTgtObject.getCurrentShape();if (!oTgtShape)	return false;		var oTgtMaterial = oTgtShape.findMaterial ('Alpha');if (!oTgtMaterial)	return false;oTgtMaterial.setName ('Beta');oTgtMaterial.setLabel ('Beta');

    Repeated without a code block to get around forum issue...

    var oTgtNode = Scene.getPrimarySelection();
    if (!oTgtNode)
    return false;

    var oTgtObject = oTgtNode.getObject();
    if (!oTgtObject)
    return false;

    var oTgtShape = oTgtObject.getCurrentShape();
    if (!oTgtShape)
    return false;

    var oTgtMaterial = oTgtShape.findMaterial ('Alpha');
    if (!oTgtMaterial)
    return false;

    oTgtMaterial.setName ('Beta');
    oTgtMaterial.setLabel ('Beta');
    Post edited by Omniflux on
  • Silas3DSilas3D Posts: 554

    @Omniflux thank you so much! I was sooooo close, just missed that last bit :) 

  • DaremoK3DaremoK3 Posts: 798

    @Silas3D :

    Since you stated you were new to scripting, I just wanted to point out from your snippet above one thing for you going forward with your scripting :

    For both of your if statements...

    // If we don't have both objects

    if( !oSrcObject || !oTgtObject ){

        // We're done...

        return false;

    }

    ...Is giving conditional argument that if either one of those are true, cancel, instead of if both of these conditions, then cancel.

    Use && (and) if you need both to be true, and the || (or) if you only need one of those to be true.

    Hope this helps.  I know when I was first starting out, there was a lot of little things I had to learn to transition syntax to from my Python syntax for correct results.

  • Silas3DSilas3D Posts: 554

    @DaremoK3 many thanks for the advice smiley

    This actually comes from one of the DAZ snippets you mentioned in your other post wink

Sign In or Register to comment.