GH_BakeUtility

Hello
Why baking don’t work in this case?

GH_Document gh_doc = Grasshopper.Instances.ActiveCanvas.Document;
var doc = Rhino.RhinoDoc.ActiveDoc;
var att = doc.CreateDefaultAttributes();
var bakeutil = new Grasshopper.Kernel.GH_BakeUtility(gh_doc);
List<Guid> obj_ids = new List<Guid>();
if(bake)
{
  bakeutil.BakeObjects(objs, att, doc);
  obj_ids.AddRange(bakeutil.BakedIds);
}
  A = obj_ids;

Not 100% sure, but you are usually not working with Guids in GH. Guids are identifying RhinoObjects, which contain geometry information but also other things like layer, color information etc. In GH you just deal with the raw geometry. Baking just means to add raw geometry in the RhinoDocument, assigning a new RhinoObject to a geometry instance. So baking means to use the Add function of the active RhinoDocuments ObjectTable.

Rhino.RhinoDoc.ActiveDoc.Objects.Add(…)

Where do you define objs?

Objs are meshes in this case

I need bake with attributes: material.
I already use this Rhino.RhinoDoc.ActiveDoc.Objects.Add
But it is not useful in the case i need

That won’t work, the objs collection need to contain objects which implement both the IGH_BakeAwareData and IGH_Goo interfaces.

RhinoDoc.Objects.Add() is ultimately what needs to be called, either by you directly or by Grasshopper. So we need to figure out how to create the ObjectAttributes you want, which is a pure RhinoCommon problem. What sort of material do you want to assign?

Thanks David
I want apply
Rhino.Render.RenderMaterial mat ;
to the mesh
In this code i set mat.Xml as second output to use it with Custom Preview and bake them
But i prefer bake the meshes with materials directly from the component if that possible and fast as baking from Custom Preview

The CustomPreview component bakes the geometry without material, then assigns it afterwards. I’m not 100% sure why it works this way, if there’s a better way or not. There were a lot of changes to materials and stuff at some point due to RDK updates.

So (VB I’m afraid):

Dim obj As RhinoObject = doc.Objects.FindId(id)
obj.RenderMaterial = AddOrUseMaterial(doc, Material)
obj.CommitChanges()

Where you can simplify the AddOrUseMaterial() call because you already have Xml which needs to be converted into a render material. Something along the lines of:

Dim content As RenderContent = RenderContent.FromXml(xml, Nothing)
Dim material As RenderMaterial = TryCast(content, RenderMaterial)
Return material.ToMaterial(RenderTexture.TextureGeneration.Disallow)

I’m typing this on a machine without Rhino source so it’s probably not fully correct.

2 Likes

The CustomPreview component also don’t duplicate materials if they are already added unlike using
ActiveDoc.Objects.Add with attributes

Is the idea here: adding objects to Rhino document then find them by id and assign the materials?

Dim obj As RhinoObject = doc.Objects.FindId(id)
obj.RenderMaterial = AddOrUseMaterial(doc, Material)
obj.CommitChanges()

Yeah, bake them, then retrieve them from document and assign material. Sadly the bake logic inside the Custom Preview component is not accessible from the outside.

Is there a way to call CustomPreview component and use it from c#?

This works fine but we need first adding the material to the document

doc.RenderMaterials.Add(mat);

void Bake(Mesh mesh, Rhino.Render.RenderMaterial mat, Rhino.RhinoDoc doc)
{
    GH_Document gh_doc = Grasshopper.Instances.ActiveCanvas.Document;
    GH_BakeUtility bakeUtility = new GH_BakeUtility(gh_doc);

    var attributes = new Rhino.DocObjects.ObjectAttributes();
    attributes.RenderMaterial = mat;

    IGH_GeometricGoo convertedMesh = null;
    if (mesh.IsValid)
    {
        convertedMesh = GH_Convert.ToGeometricGoo(mesh);
    }
    bakeUtility.BakeObject(convertedMesh, attributes, doc);
}