Hi,
I attempted to convert DisplayMaterial to RenderMaterial and assign it to a layer. However, I’m unsure how to update existing RenderMaterial properties (such as texturepath,Diffuse,..) in C#. Below is the relevant code snippet:
Could you advise on the correct approach for this?
Thanks!
private void EnsureMaterialAndAssignToLayer(RhinoDoc doc, DisplayMaterial sourceDisplayMaterial, string materialName, bool shouldUpdate)
{
RenderMaterial customMaterial = null;
foreach (var mat in doc.RenderMaterials)
{
if (mat.Name.Equals(materialName, StringComparison.OrdinalIgnoreCase))
{
customMaterial = mat;
break;
}
}
if (customMaterial == null)
{
Material basicMat = new Material();
basicMat.Name = materialName;
if (sourceDisplayMaterial != null)
{
basicMat.DiffuseColor = sourceDisplayMaterial.Diffuse;
basicMat.Transparency = sourceDisplayMaterial.Transparency;
var texture = sourceDisplayMaterial.GetBitmapTexture(true);
if (texture != null)
{
basicMat.SetTexture(texture, TextureType.Bitmap);
}
var transparencyTexture = sourceDisplayMaterial.GetTransparencyTexture(true);
if (transparencyTexture != null)
{
basicMat.SetTexture(transparencyTexture, TextureType.Transparency);
}
}
// This is the standard way to create a RenderMaterial that can be assigned.
customMaterial = RenderMaterial.FromMaterial(basicMat, doc);
if (customMaterial != null)
{
// Double-check name assignment (sometimes FromMaterial might not copy it perfectly)
customMaterial.Name = materialName;
// Add the newly created render material to the document's material table
if (!doc.RenderMaterials.Add(customMaterial))
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Failed to add new render material '{materialName}' to the document.");
customMaterial = null; // Indicate failure
}
// At this point, if successful, grassRenderMaterial.Id is valid and it's in the doc.
}
else
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Failed to create render material from basic material properties.");
}
}
else if (shouldUpdate)
{
//how to update or modify RenderMaterial??
}
// 3. Assign the found or created material to the layer
if (customMaterial != null && _targetLayerIndex >= 0)
{
Layer targetLayer = doc.Layers.FindIndex(_targetLayerIndex);
if (targetLayer != null)
{
// Set the render material id for the layer
targetLayer.RenderMaterial = customMaterial;
//targetLayer.RenderMaterialIndex = grassRenderMaterial.Id;
// Commit the changes to the layer back to the document
if (!doc.Layers.Modify(targetLayer, _targetLayerIndex, true)) // true means 'quiet'
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Failed to assign material '{materialName}' to layer '{targetLayer.Name}'.");
}
//else
//{
// // Optional: Success message
// // AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, $"Assigned material '{materialName}' to layer '{targetLayer.Name}'.");
//}
}
else
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Could not find layer with index {_targetLayerIndex} to assign material.");
}
}
else if (_targetLayerIndex < 0)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Cannot assign material: Target layer index is invalid.");
}
}
