Change Transparency in Material Appearance

Change Transparency in Material Appearance

ryam.marques
Participant Participant
1,246 Views
5 Replies
Message 1 of 6

Change Transparency in Material Appearance

ryam.marques
Participant
Participant

I'm trying to change the transparency of a material's appearance. The image below depicts the point I'm dealing with; I want to change the amount of transparency.

ryammarques_0-1701286193183.png

First I search for the material in the material manager, then I go to the appearance properties and finally I edit the transparency amount

 

The approach via C# I'm trying is this:

string targetMaterialName = "Telhado cerâmico";
FilteredElementCollector collector = new FilteredElementCollector(document)
.OfClass(typeof(Material));

Material targetMaterial = collector.OfType<Material>().FirstOrDefault(m => m.Name == targetMaterialName);

AppearanceAssetElement appearanceAsset = doc.GetElement(targetMaterial.AppearanceAssetId) as AppearanceAssetElement;

 

But when I enter the element, I can't find properties to operate the transparency of the appearance. It's worth saying that I want to change the transparency of the appearance and not that of the material (Material.Transparency)

0 Likes
Accepted solutions (1)
1,247 Views
5 Replies
Replies (5)
Message 2 of 6

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @ryam.marques 
    

      Autodesk had provided special API to edit the Material Appearance. Kindly refer the PDF and Tutorial video for additional reference.

 

URL : https://www.autodesk.com/autodesk-university/class/New-API-Modify-Visual-Appearance-Materials-Revit-... 

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 3 of 6

GaryOrrMBI
Advisor
Advisor

See @Mohamed_Arshad 's reference for the full scope and capabilities available to you.

 

The part that is critical is that you need to enter an "EditScope" mode to access those additional properties, even when you are just collecting some values.

Here is an example (it's in VB but it will show you the missing pieces at least):

 

			'enable editing of Appearance Assets within the document
			'where RDV is a reference to Autodesk.Revit.DB.Visual
			Using editScope As New RDV.AppearanceAssetEditScope(thisDoc)

				'add the Appearance Asset Element (where "thisAsset" is the AppearanceAssetElement
				'to the editing session and set it to a variable
				Dim editableAsset As RDV.Asset = editScope.Start(thisAsset.Id)

				'Make your edits here:

				editScope.Commit(False)
			End Using

 

-G

Gary J. Orr
GaryOrrMBI (MBI Companies 2014-Current)
aka (past user names):
Gary_J_Orr (GOMO Stuff 2008-2014);
OrrG (Forum Studio 2005-2008);
Gary J. Orr (LHB Inc 2002-2005);
Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes
Message 4 of 6

ryam.marques
Participant
Participant

The documentation indicated by @Mohamed_Arshad was really what I was looking for, it is very complete and gives an overview of how to edit the appearance properties of the material. The part of starting and closing the editing scope is also covered in the documentation. I was able to change the parameter I wanted, thank you very much for the replies. 😁

Message 5 of 6

Mohamed_Arshad
Advisor
Advisor

HI @ryam.marques 
    

        Thank you so much for the kind reply, Can you Post the code which you have use to Adjust Transparency and mark it as a solution. So that we get a sample code snippet for Material Transparency. In future it will be helpful for others to get an direct answer for Adjusting Material Transparency 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 6 of 6

ryam.marques
Participant
Participant

Certainly, here is the code snippet that changes the material's visibility parameter.

 

private void ChangeMaterialVisibility(string materialName, Document document)
        {

            //catches the collector of all materials
            FilteredElementCollector collector = new FilteredElementCollector(document).OfClass(typeof(Material));

            //Get the desired material by filtering by name
            Material targetMaterial = collector.OfType<Material>().FirstOrDefault(m => m.Name == materialName);

            //Gets the appearance parameters of the material
            AppearanceAssetElement appearanceAsset = doc.GetElement(targetMaterial.AppearanceAssetId) as AppearanceAssetElement;

            //get the asset of the material
            Asset assetMaterial = appearanceAsset.GetRenderingAsset();

            //edit the parameter using the parameter name
            using (Transaction trans = new Transaction(this.doc, "edit the material parameters"))
            {
                trans.Start();
                using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(appearanceAsset.Document))
                {
                    Asset editableAsset = editScope.Start(appearanceAsset.Id);
                    string paramName = "generic_transparency";
                    AssetPropertyDouble transparency = editableAsset.FindByName(paramName) as AssetPropertyDouble;
                    if (transparency.Value == 0)
                    {
                        transparency.Value = 1;
                    }
                    else if (transparency.Value == 1)
                    {
                        transparency.Value = 0;
                    }
                    editScope.Commit(true);
                }
                trans.Commit();
            }
        }

 

The code can be improved by adding conditionals to check if the material with the given name exists, and also by adding a conditional to check if the called parameter exists.