Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everybody,
i'm trying to modify the appearance of the "Default" material with the AppearanceAssetEditScope class. I've created the SetDiffuseColor class:
private static void SetDiffuseColor(Material material, Color color)
{
ElementId appearanceAssetId = material.AppearanceAssetId;
AppearanceAssetElement assetElem = material.Document.GetElement(appearanceAssetId) as AppearanceAssetElement;
// A transaction is necessary. Multiple changes to the same asset can be made in one transaction if required.
using (Transaction t = new Transaction(assetElem.Document, "Change material color"))
{
t.Start();
using (AppearanceAssetEditScope editScope = new AppearanceAssetEditScope(assetElem.Document))
{
Asset editableAsset = editScope.Start(assetElem.Id); // returns an editable copy of the appearance asset
// unlike AppearanceAssetElement.GetRenderingAsset(), the edit scope
// sets up the asset for editing using the classes and utilities from
// MaterialsManager. The scope can keep a mapping from asset to Protein
// class members that are obtained from the asset, to permit use of
// Protein validation. If the Asset was not obtained from an edit scope,
// the setter operations for asset properties must throw.
// Try to change values
AssetPropertyDoubleArray4d genericDiffuseProperty = editableAsset["generic_diffuse"] as AssetPropertyDoubleArray4d;
genericDiffuseProperty.SetValueAsColor(color); // 4 length double asset properties often represent colors.
// Shortcut methods should be added to work directly with Color objects
// for these types.
// Commit the entire edit scope. If the appearance asset element is used in one or more materials, they will be
// updated to match any changes made.
editScope.Commit(true);
}
t.Commit();
}
}
and use it in my main Command class:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication app = commandData.Application;
UIDocument uidoc = app.ActiveUIDocument;
Document doc = uidoc.Document;
CreateMaterial(doc);
//Collect Materials
FilteredElementCollector coll2 = new FilteredElementCollector(doc).OfClass(typeof(Material));
IList<Element> mats = coll2.ToElements() as IList<Element>;
Material myMaterial = null;
Color matColor = new Color(255,255,255);
foreach (var m in mats) if (m.Name == "Default")
{
myMaterial = m as Material;
}
SetDiffuseColor(myMaterial, matColor);
return Result.Succeeded;
}
The result is the following :
i don't understand what am i doing wrong, any help is appreciated.
Thank you very much!
Solved! Go to Solution.