Forgot password?

Create an account!

Forum

« back

RhinoScript – Batch Export Objects by Layers

Messages

Please log in to write a message.

  • 15. glengalla (Sep 18, 2016 09.09):

    Hello! Thanks my friend Sissi in advanced!!

    I add an export (x my use STEP .stp) of sublayer object like:

    Untiled.3dm

    LAYER01
    LAYER02
    LAYER03
    |_LAYER04
    |_LAYER05
    |_LAYER06
    |_LAYER07

    EXPORT FILE:

    Untitled_Layer 01.stp
    Untitled_Layer 02.stp
    Untitled_Layer 03@Layer 04.stp
    Untitled_Layer 03@Layer 05.stp
    Untitled_Layer 03@Layer 06@Layer 07.stp
    ---------------------------------------



    Option Explicit

    Sub ExportLayerObjects

    ' Declare local variables
    Dim strPath, strFile
    Dim arrLayers, strLayer
    Dim arrSelected

    ' Get the path to and name of the current document.
    ' Surround with double-quotes in case path includes spaces.
    strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)

    ' Get names of all layers
    arrLayers = Rhino.LayerNames

    ' Disable redrawing
    Rhino.EnableRedraw False

    ' Process each layer
    For Each strLayer In arrLayers
    Rhino.Print strLayer

    ' Unselect all
    Rhino.Command "_-SelNone", 0

    ' Select all objects on layer. Surround layer name
    ' with double-quotes in case it includes spaces.
    Rhino.Command "_-SelLayer " & Chr(34) & strLayer & Chr(34), 0

    ' Make sure some objects were selected
    arrSelected = Rhino.SelectedObjects
    If IsArray(arrSelected) Then

    Rhino.Print "layer contains objects.. exporting it"

    ' Generate a modified path string
    ' that includes the layer name
    strFile = strPath
    strFile = Replace(strFile, ".3dm", "_" & strLayer & ".stp")
    strFile = Replace(strFile, "::", "@")

    Rhino.Print "filename:"
    Rhino.Print strFile




    ' Export the selected objects
    Rhino.Command "_-Export " & strFile, 0

    End If
    Next

    ' Unselect all
    Rhino.Command "_-SelNone", 0

    ' Enable redrawing
    Rhino.EnableRedraw True

    End Sub

  • 14. tommy92120 (Sep 11, 2016 18.13):

    Hello guys,

    I know this thread is old, but I can't manage to get this script to work, i tried to copy paste it from here : wiki.mcneel.com/developer/scriptsamples/selnamedobject.
    And then change the export format as explained. But it doesn't work. I am trying to export with layers in fbx to render with 3DS max, but it keeps telling me "No objects added to selection". Any suggestions ? Thanks a lot !

    Option Explicit

    Sub ExportLayerObjectsFBX

    ' Declare local variables
    Dim strPath, strFile
    Dim arrLayers, strLayer
    Dim arrSelected

    ' Get the path to and name of the current document.
    ' Surround with double-quotes in case path includes spaces.
    strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)

    ' Get names of all layers
    arrLayers = Rhino.LayerNames

    ' Disable redrawing
    Rhino.EnableRedraw False

    ' Process each layer
    For Each strLayer In arrLayers

    ' Unselect all
    Rhino.Command "_-SelNone", 0

    ' Select all objects on layer. Surround layer name
    ' with double-quotes in case it includes spaces.
    Rhino.Command "_-SelLayer " & Chr(34) & strLayer & Chr(34), 0

    ' Make sure some objects were selected
    arrSelected = Rhino.SelectedObjects
    If IsArray(arrSelected) Then

    ' Generate a modified path string
    ' that includes the layer name
    strFile = strPath
    strFile = Replace(strFile, ".3dm", "_" & strLayer & ".fbx")

    ' Export the selected objects
    Rhino.Command "_-Export " & strFile, 0

    End If
    Next

    ' Unselect all
    Rhino.Command "_-SelNone", 0

    ' Enable redrawing
    Rhino.EnableRedraw True

    End Sub

  • 13. Switch & Lever (May 03, 2016 13.54):

    If you're having the same problem as @ajewitt you can add the following line:

    strFile = Replace(strFile, "::", "__")

    after this line:

    strFile = Replace(strFile, ".3dm", "_" & strLayer & ".3dm")

    and it should all work fine.

  • 12. ajewitt (Apr 27, 2016 19.41):

    @pulgarata or anyone else who can help,
    when using the script below to export sublayers, I get an error because the script is trying to write the sublayers into the filename using colons
    example: filename\Root_Layer::sublayer::subsublayer.stp

    In creating the filename, how can I replace the double colon with an acceptable character?

  • 11. pulgarata (Oct 30, 2013 19.20):

    Hello, I think you have to correct this two lines:

    strFile = Replace(strFile, ".3dm", "_" & strLayer & ".stp")
     
                            Rhino.Command "_-Export "& strFile &" Enter"

    Sebastian

  • 10. ajewitt (Oct 30, 2013 18.40):

    I'm very interested in this script. Did anyone get this to work? I used the following script but it doesn't succeed in exporting the files. It seems like its trying to overwrite the existing file.

    Option Explicit
     
    Sub ExportLayerObjectsSTEP
     
            ' Declare local variables
            Dim strPath, strFile
            Dim arrLayers, strLayer
            Dim arrSelected
     
            ' Get the path to and name of the current document.
            ' Surround with double-quotes in case path includes spaces.
            strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)
     
            ' Get names of all layers
            arrLayers = Rhino.LayerNames
     
            ' Disable redrawing
            Rhino.EnableRedraw False
     
            ' Process each layer
            For Each strLayer In arrLayers
     
                    ' Unselect all  
                    Rhino.Command "_-SelNone", 0
     
                    ' Select all objects on layer. Surround layer name
                    ' with double-quotes in case it includes spaces.
                    Rhino.Command "_-SelLayer " & Chr(34) & strLayer & Chr(34), 0
     
                    ' Make sure some objects were selected
                    arrSelected = Rhino.SelectedObjects
                    If IsArray(arrSelected) Then
     
                            ' Generate a modified path string
                            ' that includes the layer name
                            strFile = strPath
                            strFile = Replace(strFile, ".stp", "_" & strLayer & ".stp")
     
                            ' Export the selected objects
                            Rhino.Command "_-Export "
     
                    End If
            Next
     
            ' Unselect all
            Rhino.Command "_-SelNone", 0
     
            ' Enable redrawing
            Rhino.EnableRedraw True
     
    End Sub
  • 9. Johannes (Jan 22, 2013 08.01):

    Hey, sorry when i was too direct. i didn't considered your first script. Take it easy ;)
    Are you still confused or everything fine with renaming your objects?

    johannes

  • 8. palosanto (Jan 21, 2013 21.01):

    Joannes,

    I'm confused, OF COURSE it's copy and paste. I'm the same person that asked you for help yesterday ....
    The original script is posted in message no 1 o THIS thread and I wanted to modified to do something different.

    I wasn't claiming that I wrote the script, come on ...

  • 7. Johannes (Jan 21, 2013 20.49):

    that looks like copy paste!!! where did you get that? i am not sure if it's helpful to correct it!?
    forget everything with path, filename, allLayer,... and do not use command scripting when you have some good script methods. Looks better when you write a new script.

    Option Explicit

    Call RenameLayerObjects()

    Sub RenameLayerObjects()

    Dim strLayerName, strObject, arrSelected

    arrSelected = Rhino.GetObjects("Pick some objects")

    For Each strObject In allObjects
    strLayerName = Rhino.ObjectLayer(strObject)
    Call Rhino.ObjectName(strObject, strLayerName)
    Next

    End Sub

    you can read the script even if you don't know much about scripting in vb. try to understand what these to scripts are doing. its a powerful tool and not hard to learn!

  • 6. palosanto (Jan 21, 2013 20.16):

    Nor very proficient in vb as you can see ...
    There are no errors but the objects don't get renamed. What's wrong?
    Here is the code. And thanks for your help

    Option Explicit
     
      Sub RenameLayerObjects
     
        ' Declare local variables
        Dim strPath, strFile
        Dim arrLayers, strLayer, objLayer
        Dim arrSelected
     
        ' Get the path to and name of the current document.
        ' Surround with double-quotes in case path includes spaces.
        strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)
     
        ' Get names of all layers
        arrLayers = Rhino.LayerNames
     
        ' Disable redrawing
        Rhino.EnableRedraw False
     
        ' Process each layer
        For Each strLayer In arrLayers
     
          ' Unselect all  
         Rhino.Command "_-SelNone", 0
     
          ' Select all objects on layer. Surround layer name
          ' with double-quotes in case it includes spaces.
          Rhino.Command "_-SelLayer " & Chr(34) & strLayer & Chr(34), 0
     
          ' Make sure some objects were selected
          arrSelected = Rhino.SelectedObjects()
          If IsArray(arrSelected) Then
             For each objlayer in arrSelected
               Rhino.ObjectName strLayer
             next
          End If
        Next
     
        ' Unselect all
        Rhino.Command "_-SelNone", 0
     
        ' Enable redrawing
        Rhino.EnableRedraw True
     
      End Sub
  • 5. Johannes (Jan 21, 2013 08.03):

    Hi, there is a Rhino.LayerName(strLayer)-, the Rhino.ObjectLayer(strObject)- and the Rhino.ObjectName(strObject) function. Get the object layer, get the layer name and rename the object with the new name. You can use SelectedObjects() for it.

    arrSelected = Rhino.SelectedObjects()
    For each strObject in arrSelected
    ...
    Next

    johannes

  • 4. palosanto (Jan 21, 2013 03.43):

    I wonder how could I use this script for a different purpose:
    I'm trying to mass rename all objects of each layer to the layer's name.
    So, is there a command or script function to name/rename objects? Then it would be a matter of apply it to the selection already made in the script as Rhino.SelectedObjects

    Your help would be very much appreciated.

  • 3. T0_ofic (Jan 06, 2012 10.20):

    Thank you I was sure it was something like this.

  • 2. Hanno (Jan 06, 2012 09.14):

    Hi,

    if this is your complete script, I would say you simply forgot to actually call your Sub...

    Hanno

  • 1. T0_ofic (Jan 05, 2012 19.09):

    Hello my friends!

    I'm trying to write a script in Rhino that will export all my scene objects but will do it on a per layer basis. (1 file per layer, if 10 layers->10 files to export)

    Unfortunatly the script doesn't do anything :)
    Could you take a look at this script and help me out if you find errors in it?

    Here is the script

    Option Explicit
     
     Sub ExportLayerObjects
     
       ' Declare local variables
       Dim strPath, strFile
       Dim arrLayers, strLayer
       Dim arrSelected
     
       ' Get the path to and name of the current document.
       ' Surround with double-quotes in case path includes spaces.
       strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)
     
       ' Get names of all layers
       arrLayers = Rhino.LayerNames
     
       ' Disable redrawing
       Rhino.EnableRedraw False
     
       ' Process each layer
       For Each strLayer In arrLayers
     
         ' Unselect all  
         Rhino.Command "_-SelNone", 0
     
         ' Select all objects on layer. Surround layer name
         ' with double-quotes in case it includes spaces.
         Rhino.Command "_-SelLayer " & Chr(34) & strLayer & Chr(34), 0
     
         ' Make sure some objects were selected
         arrSelected = Rhino.SelectedObjects
         If IsArray(arrSelected) Then
     
           ' Generate a modified path string
           ' that includes the layer name
           strFile = strPath
           strFile = Replace(strFile, ".3dm", "_" & strLayer & ".3dm")
     
           ' Export the selected objects
           Rhino.Command "_-Export " & strFile, 0
     
         End If
       Next
     
       ' Unselect all
       Rhino.Command "_-SelNone", 0
     
       ' Enable redrawing
       Rhino.EnableRedraw True
     
     End Sub
Recommend

Why are these buttons gray?