At last I have some time to pick up on RhinoPython.
My current challenge: set text to display a detail scale.
What I have so far already halts after selecting a detail.
Can anyone help to show me on how to approach this, furthermore any reading to prevent me asking apparent beginners questions like this.
Thanks
-Willem
What I have so far:
import rhinoscriptsyntax as rs
import Rhino
def DetailScaleToText():
# Get the detail
DetailID = rs.GetObject("Select Detail for scale", rs.filter.detail, True, False)
if( DetailID==None ): return
# I understand that DetailID is now a System.GUID
# But how to go about getting the ration of that Detail it refers to
# per Rhino.Geometry.DetailView.PageToModelRatio
DetailObject = ?
ratio = DetailObject.PageToModelRatio
print ratio
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
DetailScaleToText()
In order to get at objects that are in the document, you should import scriptcontext at the top. Scriptcontext refers to the active document and the objects in it. Then have a look at the following and see if it makes sense. It takes awhile to get your head wrapped around this system, but once you do it’s pretty logical…
Let me know if you don’t understand something here…
–Mitch
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
def DetailScaleToText():
# Get the detail
DetailID = rs.GetObject("Select Detail for scale", rs.filter.detail, True, False)
if( DetailID==None ): return
# I understand that DetailID is now a System.GUID
# But how to go about getting the ration of that Detail it refers to
# per Rhino.Geometry.DetailView.PageToModelRatio
"""You need to find the object that the GUID represents in the document.
For that, you can use scriptcontext which refers to the active document
and objects in it. sc.doc.Objects.Find(GUID) will return the object."""
DetailObject = sc.doc.Objects.Find(DetailID)
"""Afterwards set a breakpoint at the line below where I put "pass".
You will then see all of the properties and atttributes that the object has.
In this case, to get at PageToModelRatio, you need to go through object.Geometry
"""
pass
ratio = DetailObject.Geometry.PageToModelRatio
print ratio
if( __name__ == "__main__" ):
DetailScaleToText()
This is equivalent to Mitch’s suggestion. rs.coercerhinoobject() basically calls sc.doc.Objects.Find() as you can see if you step into the function using the debugger.
There is yet another option, which does not return the document-object (with all the attributes like layer, object name) but it’s geometry part, i.e. DetailObject.Geometry:
DetailGeomObject = rs.coercegeometry(DetailID)
ratio = DetailGeomObject.PageToModelRatio
Thanks for these insights. Can you tell me how you got this knowledge, I’m looking to find a way to jumpstart
my skills with python and its connection these type of options…
You’re very welcome, Willem. Actually I attended a weekend course, that helped a lot.
I don’t know if I can help you. Just some thoughts:
There are the Rhino objects, look here for a description: http://4.rhino3d.com/5/rhinocommon/ If you look into “Rhino.DocObjects Namespace” you find the objects stored in the Rhino document.
Each of those DocObjects (or most or at least those I dealt with so far) has got a Geometry attribute: an object of a type you would find under “Rhino.Geometry Namespace”. They represent the geometry as opposed to attributes like layer, colour and so on. Many operations obviously are methods of those geometry objects. There are Python examples to be found there.
The module rhinoscriptsyntax contains wrapper functions to simplify the access of the above mentioned objects. The built-in help is very useful, once you looked a bit here and there, you get an idea where to look for specific things. And as I mentioned before you can step into those functions with the debugger, which sometimes explains what’s happening exactly and on the other hand helps if you want something slightly different.
Ahh, and for Python in general: Look at docs.python.org (for version 2.7). This documentation is excellent. Including tutorials and so on.