Grasshopper Python: Can't Move objects on one different Layer

Hello, here is my first post, beeing new to Python but rather skilled on Rhino and Grasshopper.
My project is to automate import of large number of iGES files, made of many surfaces. That I need to join in polysurfaces. I then need to store them in different layers and sublayers.
I found how to code in a GH Python Component the importing of files and joining the surface objects, but the moving to layers does not work.
I am stuck with this for hours… My research on Forums and web seem to show that ObjectLayer may not work in Grasshopper, but no clue on how to achieve this…
Many thanks for your help.

I have following error code
Runtime error (TypeErrorException): Parameter must be a Guid or string representing a Guid

Traceback:

  • line 890, in coerceguid, “C:\Users\d3h\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\utility.py”*
  • line 1062, in coercerhinoobject, “C:\Users\d3h\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\utility.py”*
  • line 910, in ObjectLayer, “C:\Users\d3h\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\object.py”*
  • line 23, in RunScript, “”*

Here is the code in GH component:

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
from scriptcontext import doc

class MyComponent(component):

def RunScript(self, LayerPath, igesfile):

    __author__ = "d3h"
    __version__ = "2021.04.29"
    
    import rhinoscriptsyntax as rs
    import os
    rs.EnableRedraw(False)
    rs.Command('_-Import "{}" _Enter'.format(igesfile), False)
    NewObj=rs.Command('_SelLast')
    if NewObj:
        #Join surfaces in a Polysurface
        NewObj_Join=rs.Command('_Join')
        rs.ObjectLayer(NewObj,LayerPath)
    rs.EnableRedraw(True)
   
    return a

Looks like you need to change the script context to the Rhino document:

Also, welcome onboard :slight_smile: We generally encourage people to follow these directions when posting (i.e. attach a Grasshopper file with your GHPython code that we can help you debug):

Hello, and Thanks for your prompt answer.
Make sure I will try to follow the rules.
Anyhow, I have implement what you suggest.
I don’t have any error message now, but my component does no longer do what I’m expecting, meaning importing the IGES file, joining the surfaces inside and send them to the required layer.
I’m sure I did not put all this together in the right place… Can you help again?
here is the new code:

tests_gh3_creation_calques.gh (14.4 KB)
from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System

class MyComponent(component):

def RunScript(self, LayerPath, igesfile):
            
    __author__ = "d3h"
    __version__ = "2021.04.29"

    def ObjInLayer(LayerPath, igesfile):
    
        import Rhino as rc
        import scriptcontext as sc
        import rhinoscriptsyntax as rs
        import os
    
    # Set context to Rhino document and disable redraw
        sc.doc = rc.RhinoDoc.ActiveDoc
        rs.EnableRedraw(False)
        
        NewObj=rs.Command('_-Import "{}" _Enter'.format(igesfile), False)
        #NewObj=rs.Command('_SelLast')
        if NewObj:
            #Join surfaces in a Polysurface
            rs.Command('_Join')
            
            #rs.Command('_SelLast')
            #print rs.SelectedObjects()
            if NewObj_Join:
                ObjInLayer= rs.ObjectLayer(rs.SelectedObjects,LayerPath)

        # Set context back to Grasshopper
        rs.EnableRedraw(True)
        sc.doc = ghdoc
        ResultOK=ObjInLayer
        
        # return outputs if you have them; here I try it for you:
        return ResultOK