[Bug?] Replacing ObjectAttributes gives RhinoObject Guid.Empty

I noticed that assigning a new instance of ObjectAttributes to an existing RhinoObject will give the object an ID of Guid.Empty, after which any changes are no longer propagated. On the other hand, changing values the existing instance of ObjectAttributes on the object does work.

To reproduce, open a default document (in which there is Layer 03) and draw two objects on the Default Layer. Then run the command below, selecting first the one object, the the other. The first object will not be moved to Layer 03, whereas the other will be moved to layer 03.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
    ObjRef gRef;
    Result r = RhinoGet.GetOneObject("Select an object", false, ObjectType.AnyObject, out gRef);
    if (r != Result.Success) return r;

    int layerIndex = doc.Layers.Find("Layer 03", true);
    ObjectAttributes attr = new ObjectAttributes { LayerIndex = layerIndex, Name = "Attributes replaced" };

    Guid id = gRef.ObjectId;
    RhinoObject obj = doc.Objects.Find(id);
    obj.Attributes = attr;
    RhinoApp.WriteLine("obj.ID"+obj.Id); // replacing the Attributes sets Id to Guid.Empty
    obj.CommitChanges(); // has no effect

    doc.Objects.UnselectAll(true);
    doc.Views.Redraw();

    r = RhinoGet.GetOneObject("Select another object", false, ObjectType.AnyObject, out gRef);
    if (r != Result.Success) return r;

    id = gRef.ObjectId;
    obj = doc.Objects.Find(id);
    obj.Attributes.LayerIndex = layerIndex; // assigning attributes is OK
    obj.Attributes.Name = "Attributes changed";
    RhinoApp.WriteLine("obj.ID" + obj.Id);
    obj.CommitChanges();
            
    return Result.Success;
}

I believe the Rhino.DocObjects.Tables.ObjectTable.ModifyAttributes() is there to assign new attributes to an existing doc object even though it does appear you should be able to ‘set’ it directly.

I see the bug and am working on a fix. I probably this probably won’t make SR6 since we just branched for releasing SR6 yesterday, so stick with the workaround or set the Id of the attributes manually to the object’s original Id.

Thanks for finding this @menno and all of the other recent corner-case bugs you’ve found.

Ok, no problem if it doesn’t get to SR6, I know the workaround now. I only came across it recently after starting to use a custom mesh object, to which I assigned the attributes before adding it to the document. Previously, I would add any geometry to the document together with an attributes object like so:

doc.Objects.Add(geom, new ObjectAttributes{ LayerIndex=layerIndex});

which obviously does work.

Anyway, glad to see that it will get fixed and of course happy to help improving Rhino :smile:

1 Like