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.