Get material image path from Ceramic Schema

Get material image path from Ceramic Schema

lavicka.milan
Contributor Contributor
978 Views
2 Replies
Message 1 of 3

Get material image path from Ceramic Schema

lavicka.milan
Contributor
Contributor

Hi,

 

I am trying to get all image material path from project materials. 

Thanks to Jeremy: https://thebuildingcoder.typepad.com/blog/2017/10/material-texture-path.html

https://thebuildingcoder.typepad.com/blog/2019/04/set-material-texture-path-in-editscope.html

I was able to get a image path from Generic schema asset.

 

 

            FilteredElementCollector fec
                = new FilteredElementCollector(doc);

            IEnumerable<Material> targetMaterials
              = fec.OfClass(typeof(Material)).OfType<Material>();

            List<string> mat = new List<string>();
            
            foreach (Material material in targetMaterials) {
                using (Transaction t = new Transaction(doc)) {
                    t.Start("Change texture path");
                    try
                    {
                        ElementId appearanceAssetId = material.AppearanceAssetId;
                        AppearanceAssetElement appearanceAssetElem
                            = doc.GetElement(appearanceAssetId) as AppearanceAssetElement;

                        mat.Add("appearanceAsset "+appearanceAssetElem.Name.ToString());
                        
                        using (AppearanceAssetEditScope editScope
                            = new AppearanceAssetEditScope(doc))
                        {
                            Asset editableAsset = editScope.Start(appearanceAssetId);
                            mat.Add("editableAsset "+editableAsset.Name.ToString());

                            AssetProperty assetProperty = editableAsset.
                                FindByName("generic_diffuse");
                            Asset connectedAsset = assetProperty.
                                GetSingleConnectedAsset() as Asset;
                            mat.Add("ConnectedAsset "+ connectedAsset.Name.ToString());
                            if (connectedAsset == null)
                            {
                                assetProperty.AddConnectedAsset("UnifieldBitmap");
                                connectedAsset = assetProperty.GetSingleConnectedAsset();
                            }
                            else
                            {
                                AssetPropertyString path = connectedAsset.
                                    FindByName("unifiedbitmap_Bitmap")
                                    as AssetPropertyString;
                                mat.Add("Path "+path.Value.ToString());
                            }
                        }
                    }
                    catch (Exception ex) {

                    }
                    mat.Add("-------------------------");
                    App.WriteToTxt(mat);
                    t.Commit();
                    t.Dispose();
                }

 

For the test purpouse I have created project with 5 materials. 3of them are creating manualy using .png from user/desktop folder. And another two are downloaded form Autedesk material library using images from: C:\Program Files (x86)\Common Files\Autodesk Shared\Materials\Textures\2\Mats. 

Here is a Log file from this code.

Komentár 2020-03-14 141417.jpg

You can see there is a problem with CeramicSchema(Autodesk Material Library). 

So is there a way to get image path from this materials? 

 

2.) what does ["string"] stand for, for example in: connectedAsset["unifiedbitmap_Bitmap"] , because when I write it like this, error: "cannot convert from string to int"pop out

 

Thanks

0 Likes
979 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

To address your question two: in the term connectedAsset["unifiedbitmap_Bitmap"] , I would assume that connectedAsset is a dictionary, and it takes string-valued key, so "unifiedbitmap_Bitmap" is the key string to look up in the dictionary.

 

Have you analysed your model using RevitLookup?

 

You should be able to navigate to this data and explore interactively what entries are available and what keys are being used.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 3

danielpinheiro860
Contributor
Contributor

Very great solution! Thank you. I used your solution and tried to use a workaround related with your question two, iterating over the AssetProperty twice (I dont know if this is the best pratice) but for the sake of some contribution, I am sharing the code, it worked well for me: 

 

 public void FindTextureBitmapPaths(Document doc)
        {
            FilteredElementCollector fec
                = new FilteredElementCollector(doc);

            IEnumerable<Material> targetMaterials
              = fec.OfClass(typeof(Material)).OfType<Material>();

            foreach (Material material in targetMaterials)
            {
                try
                {
                    ElementId appearanceAssetId = material.AppearanceAssetId;
                    AppearanceAssetElement appearanceAssetElem = doc
                        .GetElement(appearanceAssetId) as AppearanceAssetElement;
                    Asset asset = appearanceAssetElem
                        .GetRenderingAsset();

                    int size = asset.Size;

                    for (int assetIdx = 0; assetIdx < size; assetIdx++)
                    {
                        AssetProperty aProperty = asset[assetIdx];

                        if (aProperty.NumberOfConnectedProperties < 1)
                            continue;

                        Asset connectedAsset = aProperty
                          .GetConnectedProperty(0) as Asset;

                        if (connectedAsset.Name == "UnifiedBitmapSchema")
                        {
                            size = connectedAsset.Size;

                            for (int assetIdx2 = 0; assetIdx2 < size; assetIdx2++)
                            {
                                aProperty = connectedAsset[assetIdx2];

                                if (aProperty.Name == "unifiedbitmap_Bitmap")
                                {
                                    AssetPropertyString path = connectedAsset[assetIdx2] as AssetPropertyString;
                                    break;
                                }
                            }
                        }
                    }
                }
                catch { }
            }            
        }

 

0 Likes