Error executing Python script from alias

I’ve got a Python script that I’m trying to run from an alias.

When I attempt to use the alias, I get an error.

Capture

Is there something obvious here that I’m doing wrong?

That means you have an error in your script in the first line. Try posting the Python code you’re running.

What happens when you run it directly, without the alias?

It works fine if invoked from the editor.

import rhinoscriptsyntax as rs
import scriptcontext as sc

import System
import System.Collections.Generic
import Rhino

def ProcessBlock(guid, biName):
    currLayer = rs.CurrentLayer()
    blockChildrenIDs = rs.BlockObjects(biName)

    # print ("processing block: " + biName)
    # print ("")

    for bcId in blockChildrenIDs:
        if rs.IsBlockInstance(bcId):
            bcName = rs.BlockInstanceName(bcId)
            ProcessBlock(bcId, bcName)
            rs.ObjectColorSource(bcId, source=3)
            rs.ObjectLayer(bcId, currLayer)
        else:
            objType = rs.ObjectType(bcId)
            # print("Processing non-block-object: " + str(bcId) + " : " + str(objType))
            rs.ObjectColorSource(bcId, source=3)
            rs.ObjectLayer(bcId, currLayer)

    rs.ObjectLayer(guid, currLayer)

    # print ("----------------------------------------------------")
    
    return True


def main():
    objs = rs.SelectedObjects()
    
    if len(objs) == 0:
        rs.MessageBox("Please select an object")
        return False

    if len(objs) == 1:
        objGUID = objs[0]
        if rs.IsBlockInstance(objGUID):
            currLayerId = rs.CurrentLayer()
            biName = rs.BlockInstanceName(objGUID)
            currLayerName =rs.LayerName(currLayerId)
            rs.RenameLayer(currLayerName, biName)


    # Make sure we've got a BlockInstance
    # if not rs.IsBlockInstance(objGUID):
    #     rs.MessageBox("Object is not a BlockInstance")
    #     return False

    for i in objs:
        if rs.IsBlockInstance(i):
            ProcessBlock(i, rs.BlockInstanceName(i))
        rs.ObjectColorSource(i, source=3)
    
    return True


main()

I’m not sure what it’s supposed to be doing, but it appears to run without errors for me:

It recursively goes through block instances and sets the visibility for the child objects to the parent’s visibility. This gets around my issue where if you move a block instance from Layer A to Layer B, the object will become invisible if you turn Layer A off, even though you moved the instance to layer B.

Normally it isn’t a problem, but when you’re importing a STEP file with thousands of block instances, it makes it really hard to organize stuff by layer.

Maybe something in the alias syntax ?

IIRC RunPythonScript requires a file name (without parentheses), or Python code enclosed in parentheses.
… Dont’ know if a file name enclosed in parentheses might be a valid syntax. :confused:

That’s my guess. It’s not that big of a deal though. I can handle hitting the play button in the script editor :slight_smile:

I’d remove the parentheses from the alias: !-RunPythonScript "E:\path\to\yourscript.py"

Just for giggles, I tried to bind the script to a button. I also added the directory where the script is to the search path.

The button command looks like this:

! -RunPythonScript "RecursiveSetObjectPropertiesToParent.py"

Output in the Rhino command window is:

Command: -RunPythonScript
Python Script <RecursiveSetObjectPropertiesToParent.py> ( ResetEngine ): "RecursiveSetObjectPropertiesToParent.py"

So I’m kinda at a loss. I can run it from the script editor, but I really would like to get it bound or aliased.

Does it work if you run just RunPythonScript on the command-line, like @AndersDeleuran did in his video? Pick your script when asked for it.

Yes, it works just fine if I run it from the command-line.

Ah. Putting the contents of the script into the toolbar button worked.

Not sure why it works, but it does.

This works for me:

I tried exactly that, and for some reason it didn’t work for me. It’s possible I had a typo in there though, so I’ll try it again.

Rhino 8.11.24

I have two scripts, one written in Python 3, and the second one in Python 2. Both of them work when executed from the Script Editor. However, only the Python 2 version works when executed through Alias.
Is this expected behavior?


Python 3

import rhinoscriptsyntax as rs

def activate_layer(layer_name):
    # Check if the layer exists
    if not rs.IsLayer(layer_name):
        # Optionally create the layer if it does not exist
        rs.AddLayer(layer_name)
        print(f"Layer '{layer_name}' created.")

    # Set the layer as current
    rs.CurrentLayer(layer_name)
    print(f"Layer '{layer_name}' is now the active layer.")

# Specify the layer name
layer_name = "test"
activate_layer(layer_name)

python error


Python 2

import rhinoscriptsyntax as rs

def activate_layer(layer_name):
    # Check if the layer exists
    if not rs.IsLayer(layer_name):
        # Optionally create the layer if it does not exist
        rs.AddLayer(layer_name)
        print("Layer '{}' created.").format(layer_name)

    # Set the layer as current
    rs.CurrentLayer(layer_name)
    print("Layer '{}' is now the active layer.").format(layer_name)

# Specify the layer name
layer_name = "test"
activate_layer(layer_name)

-RunPythonScript is the older Rhino command. Its default behaviour is to use Python 2. Make sure your python 3 scripts contain #! python 3 on the first line or use .py3 file extension.

You can use -ScriptEditor command with Run option to run scripts of any language.