Bake mesh from GH_Mesh has null value

Hello!

We are trying to automate some part of mesh creation but the GH_Mesh.Value that we get from an IGH_Param is always null. So far, we have built a Rhino.Inside .Net webapi which is able to open our GH file. We retrieve the input params which we want to modify with their Nickname that were passed from our client. To update their value, we run the following lines on the IGH_Param with the correct Nickname:

param.VolatileData.ClearData();
var result = param.AddVolatileData(pathing, 0, value);

Where value is mapped to its GH_dataType representative (ie.: int → GH_Integer).

Once the values are all updated, we run the following:

    foreach (var parameters in activeDocument.Objects.OfType<IGH_Param>())
    {
      if (parameters is not null
      && parameters.Name.ToLower().Contains("mesh")
      && parameters.NickName.ToLower().Contains("_export"))
      {
        resultItem.Add(parameters);
      }
    }

...

activeDocument.Enabled = true;
activeDocument.NewSolution(false, GH_SolutionMode.CommandLine);

foreach (var res in resultItem)
      {
        if (res is null) return new RhinoCoreError("Could not get meshed object as GH_Mesh in Grasshopper, res is null!?");

        var meshParam = res as Grasshopper.Kernel.Parameters.Param_Mesh;
        if (meshParam is null) return new RhinoCoreError("Could not get meshed object as Param_Mesh from Grasshopper");

        GH_Mesh resultMesh = new GH_Mesh(meshParam.ComponentGuid);

        if (resultMesh is null) return new RhinoCoreError("Could not get meshed object as GH_Mesh in Grasshopper");

        // resultMesh has no value ? -> always exits here, with the Mesh being null.
        if (resultMesh.Value is null) return new EmptyGrasshopperComponent(res.NickName);

        var attributes = new Rhino.DocObjects.ObjectAttributes();
        Guid reference = new();
        if (resultMesh.BakeGeometry(Rhino.RhinoDoc.ActiveDoc, attributes, ref reference))
        {
          references.Append(reference);
        }
      }
      if (references is null) return new EmptyGrasshopperComponent("Guid references of baked object");
      if (references.Count() > 0) return references; 

We have validated that the Volatile data are indeed added to the input params, but we haven’t found a way to validate that they are properly added and that the computation was successfully completed with the new values. We presume that the error we get resides either between the addition of volatile data, the computation with the added values, or the IGH_Params with which we try to get the mesh reference in grasshopper.

All help is appreciated.

Thank you for your time!

For anyone reading this in the future, we fixed our bug by changing this:

    foreach (var parameters in activeDocument.Objects.OfType<IGH_Param>())
    {
      if (parameters is not null
      && parameters.Name.ToLower().Contains("mesh")
      && parameters.NickName.ToLower().Contains("_export"))
      {
        resultItem.Add(parameters);
      }
    }

to this :

    foreach (var objects in activeDocument.Objects.OfType<GH_Component>())
    {
      foreach (var parameter in objects.Params.Output)
      {
        if (parameter is not null
        && parameter.Name.ToLower().Contains("mesh")
        && parameter.NickName.ToLower().Contains("_export"))
        {
          resultItem.Add(objects);
        }
      }
    }

It’s unclear to me how changing to the 2 loops by requesting the GH_Component instead of the GH_Param directly grants the access to the mesh in the GH_Mesh Component.

Hope this helps!

1 Like