Batch Mesh to Sublayer

Hello all!

Does anyone have a script that will run a mesh for all objects on varying layers and move the mesh to a the respective parent sublayer with the format “parentlayer”-mesh?

Any help would be appreciated!

Thanks,

dsuhay

Try something like this:

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext

filter = rs.filter.polysurface
objIds = rs.GetObjects('Select objects for meshing', filter)

params = Rhino.Geometry.MeshingParameters()

for objId in objIds:
    objLayName = rs.ObjectLayer(objId)

    

    brep = rs.coercerhinoobject(objId)
    
    if brep.ObjectType == Rhino.DocObjects.ObjectType.Extrusion:
        brep = Rhino.Geometry.Brep.TryConvertBrep(brep.Geometry)
    else:
        brep = brep.Geometry

    try:
        meshes = Rhino.Geometry.Mesh.CreateFromBrep(brep, params.Default)
        
        layers = rs.LayerNames()
        #print layers
        meshLayName = "{}_MESH".format(objLayName)
        if not "{}::{}".format(objLayName,meshLayName) in layers:
            rs.AddLayer(meshLayName)
            rs.ParentLayer(meshLayName, objLayName)
            
        for mesh in meshes:
            id = scriptcontext.doc.ActiveDoc.Objects.Add(mesh)
            rs.ObjectLayer(id, meshLayName)
    except:
        continue