Setting AppearanceAsset properties of newly created Revit Material

By Naveen Kumar

Question: One can change the AppearanceAsset properties of a UI-generated Revit material. However, the AppearanceAssetId value is null if the material is API-generated. The AppearanceAsset properties cannot be set for the material created by the API. Is it possible to modify or set the AppearanceAsset properties of an API-generated Material?

Answer: Since the API-generated material lacks an AppearanceAssetId , it should be set explicitly. In this case, the simplest way to create a new material is to duplicate an existing one and then modify it.

In the code below, red carpet material has been duplicated, and the AppearanceAsset properties of the duplicated material have been modified.

 

public void DuplicateAndModifyMaterial(Material material)
{
ElementId appearanceAssetId = material.AppearanceAssetId;
AppearanceAssetElement assetElem = material.Document                .GetElement(appearanceAssetId) as AppearanceAssetElement;
ElementId duplicateAssetElementId = ElementId.InvalidElementId;
using (Transaction t = new Transaction(material.Document))
{
t.Start("Duplicate Red Carpet Material");
// Duplicate the material
Material duplicateMaterial = material.Duplicate("Blue Carpet");
           // Duplicate the appearance asset and the asset in it
AppearanceAssetElement duplicateAssetElement = assetElem.Duplicate("Blue Carpet");
           // Assign the asset element to the material
duplicateMaterial.AppearanceAssetId = duplicateAssetElement.Id;
duplicateAssetElementId = duplicateAssetElement.Id;
t.Commit();
}
// Make changes to the duplicate asset
using (Transaction t2 = new Transaction(material.Document))
{
t2.Start("Change blue carpet material assets");
using (AppearanceAssetEditScope editScope                     = new AppearanceAssetEditScope(assetElem.Document))
{                // returns an editable copy of the appearance asset
Asset editableAsset = editScope.Start(duplicateAssetElementId);
             // Description
AssetPropertyString descriptionProperty =editableAsset                   .FindByName("description") as AssetPropertyString;
descriptionProperty.Value = "blue carpet";
             // Diffuse image
AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset                  .FindByName("generic_diffuse") as AssetPropertyDoubleArray4d;
              genericDiffuseProperty                 .SetValueAsColor(new Autodesk.Revit.DB.Color(0x00, 0x00, 0xFF));
              Asset connectedAsset = genericDiffuseProperty.GetSingleConnectedAsset();
AssetPropertyString bitmapProperty = connectedAsset                   .FindByName("unifiedbitmap_Bitmap") as AssetPropertyString;
             //Appearance tab >>> Generic >>> Image
bitmapProperty.Value = @"Your image Path here";
editScope.Commit(true);
}
t2.Commit();
}
}

Please note: Here in this instance, the “generic_diffuse” property has been accessed. However, not every AppearanceAsset will have the “generic_diffuse” property. So, please use the RevitLookup tool to explore the material element, find the relevant property, and modify it accordingly.


Comments

8 responses to “Setting AppearanceAsset properties of newly created Revit Material”

  1. I highly appreciate your information. I am looking for many useful post like this.

  2. SamRoberts Avatar
    SamRoberts

    Is the diffuse image accessed differently for a PBR assett, been trying this method with multiple of the PBR assets and all return null.

  3. I have attempted this method with several PBR objects, but they all return null. The diffuse image is accessed differently for a PBR item.

  4. Bruce Hans Avatar
    Bruce Hans

    Is there any way to regenerate material thumbnail after using API modify it’s property? Since only UI-modified material’s thumbnail can be regenerated.

  5. Hi Bruce Hans,
    If you’re looking for new or enhanced Revit API functionality, the Revit Idea Station is the best place to submit feature requests. While searching, I came across an idea that aligns with what you’re looking for:
    https://forums.autodesk.com/t5/revit-ideas/save-material-thumbnails-after-modified-by-api/idi-p/10250065
    I encourage you to vote for the idea, as the Revit development team actively monitors the Idea Station for community input.

  6. Bruce Hans Avatar
    Bruce Hans

    Hi Naveen Kumar,
    Thanks for replying my question. I am the guy who post the idea. It’s been a while, I just wonder if you can come up some solutions.

  7. Naveen Kumar T Avatar
    Naveen Kumar T

    Hi Bruce Hans,
    I checked with the Revit Engineering team, and they said that they are aware of this issue. At the moment, there are no updates available.

  8. Bruce Hans Avatar
    Bruce Hans

    Hi Naveen Kumar,
    Glad they are aware of this. Thanks for the help. Still waiting.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading