Script OneLayerOff

Is there a way to script OneLayerOff to preselect a layer name?

Currently you need to select an object manually. I’d like to setup a keyboard shortcut to turn off a Layer of a specified name. Is that possible?

Thanks,
A

Hi Andrew
import rhinoscriptsyntax as rs
layername = rs.GetString("")
if rs.IsLayer(layername):
if rs.LayerVisible(layername)==True:
rs.LayerVisible(layername,False)
The answer to what you want?

Thanks for the response!

However, I’m not even a newbie at scripting. I tried 2 methods without success:

  1. I saved your script:
    layername = rs.GetString("")
    if rs.IsLayer(layername):
    if rs.LayerVisible(layername)==True:
    rs.LayerVisible(layername,False)

as an RVB using Notepad. I then used _LoadScript, but kept getting errors.

2… I saved your script as an alias (using the first line “import rhinoscriptsyntax as rs” and also deleting the first line), but neither worked…

I’m obviously doing something very wrong!

Thanks again for your time.

You should be able to at least test the script by running the EditPythonScript command and then paste the following:

import rhinoscriptsyntax as rs
layername = rs.GetString("")
if rs.IsLayer(layername):
    if rs.LayerVisible(layername)==True:
        rs.LayerVisible(layername,False)

To use a python script (*.py) as a toolbar button:
Open button editor and type the following:

! _-RunPythonScript (

<paste the entire script here>

)

Then OK to exit the button editor.

To use a Python script via an alias:

Procedure 1 -
Save the script somewhere known.
In Options>Aliases, create a new alias with the following:

! _-RunPythonScript "<full path to your script including extension>"

Path example: "C:\Users\<username>\Documents\Myscripts\TestScript.py"

-or-

Procedure 2
Create a folder somewhere for all your Python scripts.
Open the Python script editor with the _EditPythonScript command
In Tools>Options>Files tab, set a path to the folder you just created, then OK. You can close the editor.
(you only need to do the above once)

Now in Options>Aliases, you can create your aliases without the full path, just the name will work:

! _-RunPythonScript "TestScript"

HTH, --Mitch

Thanks!

When I copied the entire code verbatim and hit the Play icon in the EditPythonScript dialog, the command line asks for Text:
I type in my layer name (in my case “NoPrint”) and it works!

But, is there a way of preselecting the layer name so I don’t have to keep typing it in?

I tried replacing “layername” in the code with “NoPrint” in various places, but I didn’t have any success.

This should work:

import rhinoscriptsyntax as rs
layername = "NoPrint"
if rs.IsLayer(layername):
    if rs.LayerVisible(layername): rs.LayerVisible(layername, False)
else:
    print "No layer called {} found in document".format(layername)

You can hard-code any name you want in the “layername =…” line, not just “NoPrint”.

HTH, --Mitch

Hi Andrew
First you should run rhinopython,Run the code,CheckListBox Select the layer you want to close,
Cheers

Hi Andrew,
its nice to play with scripting but sometimes a simple Macro will also do:

-_Layer _Off "NoPrint" _Enter

By enclosing the Layer name in double quotes you can address names with spaces.

Thanks to all the awesome replies.

I managed to get Helvetasaur’s Alias procedure working as keyboard shortcut (yay!), and Jess’ concise macro to work as well (nice!).

My holy grail would be whether I can assign the same shortcut as a toggle? So if NoPrint is off, the same script/macro will turn it on? Is that possible?

I’ve assigned Jess’ macro to Ctrl-End
and a modified one
-_Layer _On “NoPrint” _Enter
to Ctrl-Home

This does the trick for me. I super-quick way to get a layer on/off! Love it.

Thanks everyone!

Hi Andrew, for a toggle in this case a script would be needed.
I’ve modified Mitch’ script so it works a toggle:

import rhinoscriptsyntax as rs
layername = "NoPrint"
if rs.IsLayer(layername):
    if rs.LayerVisible(layername): 
        rs.LayerVisible(layername, False)
    else:
        rs.LayerVisible(layername, True)
else:
    print "No layer called {} found in document".format(layername)

Thanks Jess!

LOVING the toggle! I’ve set it as a keyboard shortcut Shift-End. Super convenient- Thanks again!

A