Adding name to newly created mesh?

Is there a way to also affect the “name” attribute for this newly created mesh?

mesh = Rhino.Geometry.Mesh()

mesh.Vertices.Add(0.0,0.0,0.0)
mesh.Vertices.Add(xdim, 0.0, 0.0)
mesh.Vertices.Add(xdim, ydim, 0.0)
mesh.Vertices.Add(0.0, ydim, 0.0)
mesh.Vertices.Add(0.0, 0.0, zdim)
mesh.Vertices.Add(xdim, 0.0, zdim)
mesh.Vertices.Add(xdim, ydim, zdim)
mesh.Vertices.Add(0.0, ydim, zdim)

mesh.Faces.AddFace(0,1,2,3)
mesh.Faces.AddFace(0,1,5,4)
mesh.Faces.AddFace(1,2,6,5)
mesh.Faces.AddFace(2,3,7,6)
mesh.Faces.AddFace(3,0,4,7)
mesh.Faces.AddFace(4,5,6,7)

mesh.Normals.ComputeNormals()
mesh.Compact()

You can set a name on the ObjectAttributes when you add the mesh to the document.

I tried :
attributes = Rhino.DocObjects.ObjectAttributes()
Rhino.DocObjects.Tables.AddMesh(mesh, attributes)

Opposed to the
doc.Objects.AddMesh(mesh) from scriptcontext

And had two problems :
Message: attribute ‘AddMesh’ of ‘namespace#’ object is read-only
And setting the Mesh name in objectattributes

http://4.rhino3d.com/5/rhinocommon/html/P_Rhino_DocObjects_ObjectAttributes_Name.htm

Try this:

/// C#
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  double xdim = 15.0;
  double ydim = 10.0;
  double zdim = 5.0;

  Rhino.Geometry.Mesh mesh = new Rhino.Geometry.Mesh();
  mesh.Vertices.Add(0.0, 0.0, 0.0);
  mesh.Vertices.Add(xdim, 0.0, 0.0);
  mesh.Vertices.Add(xdim, ydim, 0.0);
  mesh.Vertices.Add(0.0, ydim, 0.0);
  mesh.Vertices.Add(0.0, 0.0, zdim);
  mesh.Vertices.Add(xdim, 0.0, zdim);
  mesh.Vertices.Add(xdim, ydim, zdim);
  mesh.Vertices.Add(0.0, ydim, zdim);

  mesh.Faces.AddFace(0, 1, 2, 3);
  mesh.Faces.AddFace(0, 1, 5, 4);
  mesh.Faces.AddFace(1, 2, 6, 5);
  mesh.Faces.AddFace(2, 3, 7, 6);
  mesh.Faces.AddFace(3, 0, 4, 7);
  mesh.Faces.AddFace(4, 5, 6, 7);

  mesh.Normals.ComputeNormals();
  mesh.Compact();

  Rhino.DocObjects.ObjectAttributes attribs = doc.CreateDefaultAttributes();
  attribs.Name = "Sample Mesh";

  doc.Objects.AddMesh(mesh, attribs);
  doc.Views.Redraw();

  return Result.Success;
}

That worked great, for both c# and translating it to python.

So does Rhino.DocObjects.Tables.ObjectTable.AddMesh(mesh)
actually do anything?

it seems to want two member variables
but does it work the same way as : doc.Objects.AddMesh(mesh, attribs);

… I guess its a more general question
Rhino.DocObjects.Tables.ViewTable.Redraw()
since that also wants an arguement

I guess I am missing something fundamentally about using Rhino.DocObjects.

This is the definition of a function. You need to call that function with an instance of the RhinoDoc class. This is where ‘doc’ comes into play.

@stevebaer … love the blog :smile:

… that makes sense
Can you access an instance of RhinoDoc in python from Rhino.DocObjects?

in python you access the doc through scriptcontext.

import scriptcontext
...
scriptcontext.doc.Objects.AddMesh(mesh)

Cool, thanks.
I noticed you can also do something like this:
Rhino.DocObjects.Tables.ObjectTable.AddMesh(scriptcontext.doc.Objects,mesh, attribs)

So scriptcontext.doc is the only way to access the current ObjectTable and ViewTable then I guess.

Yes, that is just different syntax for the same thing

That is the “correct” way to access these tables. There is a RhinoDoc.ActiveDoc function, but I would almost always recommend using scriptcontext.doc instead.

because the ActiveDoc can change right?

Yes, or…

  • there could be multiple docs when running on OSX
  • scriptcontext.doc may be a “Grasshopper Doc” when running inside of Grasshopper