Rhino mac threading multiple documents

I tried to handle multiple documents issues on mac in a method called by a background thread:

def create_results(results, callback_data, msg=None):
    parent_layer = callback_data["parent_layer"]
    rs.AddLayer(parent_layer)
    docId = callback_data["docId"]
    old_doc = scriptcontext.doc
    scriptcontext.doc = scriptcontext.doc.FromId(docId)
    .....

    except Exception as e:
        print(e)
        print("error happened in create_results")
    finally:
        scriptcontext.doc = old_doc

but somehow the results are still created sometimes in the wrong document. Do I need to use RhinoCommon to create the geometries or is there a way to use rhinoscriptsyntax

Hi @bar,

You should be able to use RhinoScriptSyntax as it get the required document from the scriptcontext.

For example, here is the source code to rhinoscriptsyntax.AddLayer.

I will add that Rhino documents are not thread safe.

When retrieving documents, I prefer to use RhinoDoc.FromRuntimeSerialNumber. You should validagte the return value too.

– Dale