2024 Method
Changes 4
M

AssetProperty.GetSingleConnectedAsset

Description:
Gets the single connected asset attached to this asset property, if it exists.
Remarks:
Throws if there is more than one connected asset.
public Asset GetSingleConnectedAsset()
nullNothingnullptr The connected asset, or null Nothing nullptr a null reference ( Nothing in Visual Basic) if there is no connected asset.
private void SetBumpmapBitmap(Material material, String bumpmapImageFilepath)
{
   ElementId appearanceAssetId = material.AppearanceAssetId;

   AppearanceAssetElement assetElem = material.Document.GetElement(appearanceAssetId) as AppearanceAssetElement;

   using (Transaction t = new Transaction(material.Document, "Change material bumpmap bitmap"))
   {
      t.Start();

      using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(assetElem.Document))
      {
         Asset editableAsset = editScope.Start(assetElem.Id);   // returns an editable copy of the appearance asset

         AssetProperty bumpMapProperty = editableAsset.FindByName("generic_bump_map");

         // Find the connected asset (with a shortcut to get the only one)

         Asset connectedAsset = bumpMapProperty.GetSingleConnectedAsset();

         if (connectedAsset == null)
         {
            // Add a new default connected asset
            bumpMapProperty.AddConnectedAsset("UnifiedBitmap");

            connectedAsset = bumpMapProperty.GetSingleConnectedAsset();
         }

         if (connectedAsset != null)
         {
            // Find the target asset property
            AssetPropertyString bumpmapBitmapProperty = connectedAsset.FindByName("unifiedbitmap_Bitmap") as AssetPropertyString;

            if (bumpmapBitmapProperty.IsValidValue(bumpmapImageFilepath))

               bumpmapBitmapProperty.Value = bumpmapImageFilepath;
         }


         editScope.Commit(true);
      }

      t.Commit();
   }
}