Select newly baked object by ghpython

hi, everyone. m new to ghpython, and trying to bake some geometry to rhino with attributes.
after baking ,i want to select the newly baked objects. but i have no idea how to do that.
please give me some advices.
here is my bake code:

if bake:
    geo = geometry
    attr = Rhino.DocObjects.ObjectAttributes()
    attr.SetUserString(key,value)
    sc.doc = Rhino.RhinoDoc.ActiveDoc
    obj_id = sc.doc.Objects.Add(geo,attr)

m using a button to trigger the baking , after click the bake button, bake = False, so i can not pass the “obj_id” out for further use.
i konw i could bake it to a layer, and select it by layerName. but there are other objects on that layer.
neither by the userString.


I guess You are looking for ObjectTable.Select Method:

import Rhino
doc = Rhino.RhinoDoc.ActiveDoc
if bake:
    geo = geometry
    attr = Rhino.DocObjects.ObjectAttributes()
    attr.SetUserString(key,value)
    id = doc.Objects.Add(geo,attr)
    doc.Objects.Select(id)

Bake.gh (7.5 KB)

3 Likes

thanks for your reply. and I also need to get the object bake to grasshopper. something like this.
image

You mean something like this?

import Rhino
doc = Rhino.RhinoDoc.ActiveDoc
if bake:
    geo = geometry
    attr = Rhino.DocObjects.ObjectAttributes()
    attr.SetUserString(key,value)
    guid = doc.Objects.Add(geo,attr)
    doc.Objects.Select(guid)
if 'guid' in globals():
    a = guid

Bake.gh (8.9 KB)

3 Likes

hi @Mahdiyar please help in this case

    if (bake)
    {var attr = new Rhino.DocObjects.ObjectAttributes();
      var his = new Rhino.DocObjects.HistoryRecord(Rhino.Commands.Command.DisplayHelp, 0);
      attr.SetUserString(key, value);
      var id = Rhino.RhinoDoc.ActiveDoc.Objects.AddBrep(geo, attr, his, true, spit);
      Rhino.RhinoDoc.ActiveDoc.Objects.Select(id);
    }

BrepBake(not aplitKinkyFaces-new.gh (12.0 KB)

BakeWithoutSplitingKinkySurfaces

private void RunScript(Brep brep, bool bake, ref object A)
{
  if (!bake) return;
  var id = RhinoDocument.Objects.AddBrep(brep);
  RhinoDocument.Objects.Replace(id, brep, false);
}

BakeWithoutSplitingKinkySurfaces.gh (4.4 KB)