I’m having trouble with a script that helps me to simplify some sketchup files that we get from time to time in the office.
Hundreds of thousands of individual meshes that I want to join by layer.
I saw the script above which should be faster than the normal join command and I would like it to automatically join all the meshes for each layer.
this is the script snippet I am starting from:
def JoinByLayer() :
layer_list = rs.LayerIds()
rs.EnableRedraw(False)
for layer_id in layer_list:
rs.UnselectAllObjects()
rs.ObjectsByLayer(layer_id,True)
objs=rs.SelectedObjects()
if objs and len(objs)>1: rs.Command("_Join")
What I would like is a way to join a large number of single meshes in the fastest way possible, hypothetically even 1 million single meshes by layer.
Up to a few thousand meshes everything seems to work fine with the command line join, but if we are talking about hundreds of thousands of meshes, then it hangs forever…
I am using rhino 7 latest sr.
Can anyone help me with this?
# get meshes from rhino objects
# meshes = List[Rhino.Geometry.Mesh]([rs.coercemesh(obj.Id) for obj in rc])
meshes = List[Rhino.Geometry.Mesh]([obj.Geometry for obj in rc])
it should be possible to get the geometry directly from the obj.
I was using this method, thanks to @Helvetosaur
but yours it’s even a bit faster.
def JoinMeshesByLayer():
rs.EnableRedraw(False)
layers=rs.LayerNames()
for layer in layers:
layer_meshes=[obj for obj in rs.ObjectsByLayer(layer) if rs.IsMesh(obj)]
print "Found {} meshes on layer {}".format(len(layer_meshes),layer)
if layer_meshes and len(layer_meshes)>1:
joined=rs.JoinMeshes(layer_meshes,False)
if joined:
rs.ObjectLayer(joined,layer)
try:
rs.DeleteObjects(layer_meshes)
except:
pass
JoinMeshesByLayer()
Before the Join I am running this to create layers by material names and put the meshes in the same layer by material:
wouldn’t it be better to assign the material to the layer in your last script ? By default objects without materials use their layer’s material if one is assigned to the layer. You could do it once after the new layer is created using:
if this is not practical, post a small example so i can see what type of materials (or render materials) you need to be assigned. Which version of Rhino do you have ?
If above works, you should have the material assigned to the layer. The question is if you then get obj files with the correct material assignment once exported. Therefore i’ve asked for a small example file.
if new_layer_index is smaller than zero then it could be that adding the new layer failed because it already exists. Do you have multiple materials with the same name in the materials Panel ?
All of the skp file I am using are under nda so I can’t share, I am sorry, I have no timeat the moment to prepare a sample file.
For now I have solved by putting the material code line in the middle of a try except, and skipping that error, it is not crucial if a material is missing eventually.