OsnapMode in Python

I’m attempting to replicate this Rhinoscript code in python:

Call Rhino.OsnapMode(1 + 256)

This turns on the “near” and “perpendicular” OSNAPs.

In python I tried this:

rs.OsnapMode(1 + 256)

and it shuts all of the OSNAPs off. If I try various numbers in the parenthesis, I get unpredictable results. For example, according to the help file, using 1 should turn on “Near” but 2 does. If I try this with “Near” turned on:

mode = rs.OsnapMode()
print mode

it returns 50190. With “Near” and “Perpendicular” on I get a value of 574478 returned.

What am I missing? I tried to replicate the example but it gave an error. Clearly I still have a lot to learn about python!!



Hey Dan,

Maybe this will help:

    """Returns or sets the object snap mode. Object snaps are tools for
    specifying points on existing objects
      mode [opt] = The object snap mode or modes to set. Object snap modes
                   can be added together to set multiple modes
                   0     None
                   2     Near
                   8     Focus
                   32    Center
                   64    Vertex
                   128   Knot
                   512   Quadrant
                   2048  Midpoint
                   8192  Intersection
                   0x20000   End
                   0x80000   Perpendicular
                   0x200000   Tangent
                   0x8000000  Point
      if mode is not specified, then the current object snap mode(s)
      if mode is specified, then the previous object snap mode(s) 


And maybe this:

0x20000 = 131072
0x80000 = 524288
0x200000 = 2097152
0x800000 = 8388608

And @stevebaer, I think there is a typo in the application.py docstring, the last item has one too many zeros…?

Hi Mitch,

Where did you get that information? I’m using the help file that comes with the Rhino Python editor.



From the following place:

C:\Users\(username)\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

That folder contains all the files that constitute Rhinoscriptsyntax for Python. If you open any of them in a text editor, it shows all the Rhinocommon methods for that category that the rhinoscriptsyntax is calling. An excellent guide for delving into RhinoCommon as well, and perhaps for detecting the occasional bug. I created a shortcut to this folder (or a Win 8 tile) as I consult stuff in there rather often.

rs.OsnapMode is in application.py and what I posted is part of the docstring (long comment) with a description of the method.


That is going to be a useful resource as I move forward with python. Thanks for pointing that out.


It’s a gold mine… :smile:

The other thing to check out is the online RhinoCommon SDK


I’m so far in over my head on this, but I’m going to bookmark that page for when this makes more sense! :smile:



Dan you can also just step through the rs.Whatever() by running it in the Python editor with a break point- this (potentially) takes you through all the relevant py files at the relevant locations as you step through it, so you can see the RhinoCommon stuff go by.


Thanks Pascal, I will give that a try. This Python stuff is starting to make more sense everyday, but I still have a long way to go.