Is there a way to SET texture coordinates for an existing mesh?
I can read it via Rhino.MeshTextureCoordinates (RhinoScript) but I don’t see a way to set new ones on the existing mesh. Currently rebuilding the mesh and replacing the old one but ideally I need to keep the same object ID etc.
just get the RhinoObject then access the mesh using rh_obj.Geometry. You can change individual coordinates using SetTextureCoordinate and multiple using SetTextureCoordinates.
To keep the same Id, just use RhinoObject.CommitChanges().
Edit: For RhinoScriptSyntax there is indeed no method despite re-recreating the whole mesh.
Hi Clement, thank you very much, I will check these out!
Have you used these methdos before? I am mostly interested in SetTextureCoordinates to set all of them in one go, so it would mean feeding an array of 2D points coordinates that equals the number of mesh vertices? Sorry, I am still not using RhinoCommon but looks like with this one there is no other choice.
Would you happen to have a sample snippet of code for this?
Hi @Jarek, yes, below is a cheesy example which just assigns the vertex positions as viewed from top view as texture coordinates to a sphere mesh. It is as you say, just make sure the amount of vertices equals the amount of texture coordinates in the Point2f type. The result of the example equals a planar projection from top where each tile is one unit large:
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import System
def DoSomething():
# create a mesh sphere
plane = Rhino.Geometry.Plane.WorldXY
sphere = Rhino.Geometry.Sphere(plane, radius=10.0)
mesh = Rhino.Geometry.Mesh.CreateFromSphere(sphere, 32, 64)
t = System.Array.CreateInstance(Rhino.Geometry.Point2f, mesh.Vertices.Count)
for i, v in enumerate(mesh.Vertices.ToPoint3dArray()):
t[i] = Rhino.Geometry.Point2f(v.X, v.Y)
mesh.TextureCoordinates.SetTextureCoordinates(t)
scriptcontext.doc.Objects.AddMesh(mesh)
scriptcontext.doc.Views.Redraw()
DoSomething()
Hi Clement, this is great, thank you so much for your time and help with the sample code.
I should be able to take it from here to my specific application.
there is no build in command to do this but it can be scripted easily. You just need to transfer the vertex positions of the squished mesh to the originating 3d mesh as texture coordinates.
A much easier way is to unwrap the mesh using the _Unwrap command and then control the mapping with the _UVEditor. You might read about this here.
you can code the unwrapping using this class. To do the layout the _UVEditor does you can either script the command or, in case you just need the unrolled meshes without connection to their originals, you could script the _ExtractUVMesh command.