Hi,
This function is part of a class that replaces an existing object with a new one.
It works as expected, but when the layer is turned off, it seems like Rhino can’t find the object.
public static RhinoObject FindOrCreateMeshObject(RhinoDoc doc, string meshName, Layer parentLayer, Guid previousId = default)
{
// Create a unique context identifier using the parent layer's full path
string layerContext = GetLayerFullPath(parentLayer);
string meshIdentifier = $"{layerContext}::{meshName}";
// If a previous ID is known, try to find the mesh by that ID first
if (previousId != default)
{
// Use FindId instead of Find to get objects even when their layer is hidden
var existingMesh = doc.Objects.FindId(previousId);
if (existingMesh != null)
{
// Verify the mesh belongs to the current context
string storedContext = existingMesh.Attributes.GetUserString("AlembicContext");
if (storedContext == layerContext)
{
return existingMesh;
}
}
}
// Search for mesh by context and name, including hidden objects
foreach (var obj in doc.Objects.GetObjectList(Rhino.DocObjects.ObjectType.Mesh))
{
string storedContext = obj.Attributes.GetUserString("AlembicContext");
string storedMeshName = obj.Attributes.GetUserString("AlembicMeshName");
if (storedContext == layerContext && storedMeshName == meshName)
{
return obj;
}
}
// If no existing mesh is found, return null to indicate a new mesh needs to be created
return null;
}