From Python into Rhino Common

Continuing the discussion from Is this the best way to automatically update linked blocks?:

Hi Jorgen,

Below a description on how I find my way in RhinoCommon through Python:

starting with the code:

import rhinoscriptsyntax as rs
blockList=rs.BlockNames()

I put a debug breakpoint at the last line:

Run the code with the green arrow (F5).
The script stops at line 2 and you get options to continue, StepInto SetOver over …etc
Choose StepInto

The script will step into the code rs.BlockNames() and the containing scriptfile is opened in a new tab:

You see what rs.BlockNames() does and how it interacts with scriptcontext.
scriptcontext is the context where this script is running in thus the current active file.
It’s a means to read information from the current file.
As you can see below scriptcontext.doc is referencing Rhino.DocObjects for the current file.

Now keep stepping until you reach line 172:
In the list at the bottom we see what our different variables are.

Notice how scriptcontext.doc.InstanceDefinitions.Getlist(True) looks like a method. That is passed the argument ‘True’ it returns a list of instance definitions:

If we dig into the list we find single objects identifying the blocks in our file:

Back to the rest of the script:

We Step Into the method rs.BlockStatus()

Notice how again first the scriptcontext.doc.InstanceDefinitions is used to find the block by it’s name.
In line 218 for the instance definition the property ArchiveFileStatus is converted into an integer.

Lets find out what this is about.
I copy ArchiveFileStatus and search for it on this page:
http://developer.rhino3d.com/api/RhinoCommon/html/N_Rhino.htm#!

To find this:

To get the status of my blocks I can now do this:

import scriptcontext
for idef in scriptcontext.doc.InstanceDefinitions :
    status=int(idef.ArchiveFileStatus)
    print status

Now for another approach to find out if there is a way in RhinoCommon to update a block definition.
On the left tree view of the scripteditor I browse forInstanceDefinition

The object type has both methods and properties.
A property is retrieved like so:
InstanceDefinition.Index

Whereas a Methods ends with parantheses with or without arguments
InstanceDefinition.GetObjects()

If you click a property or method you get some info in the console:

We were looking for a way to update and apparently there is no way to update an instancedefinition by itself.
I revert to browsing the tree yet now for the InstanceDefinitionTable

There I find a method to Update a definition. I see a list of what arguments this method needs.
After some trial and error and looking up more info on the InstanceDefinitionTable here:
http://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_DocObjects_Tables_InstanceDefinitionTable.htm

I got the script I posted in the original topic.

HTH
-Willem

6 Likes

In addition to all the great stuff Willem just posted, if you want to see “what’s inside” some of this stuff you can also use Python’s “dir” function. For example if you run:

import scriptcontext
for item in dir(scriptcontext.doc): print item

You will get the following:

ActiveDoc
AddCustomUndoEvent
AddRhinoObject
AdjustModelUnitSystem
AdjustPageUnitSystem
BeforeTransformObjects
BeginOpenDocument
BeginSaveDocument
BeginUndoRecord
Bitmaps
ClearRedoRecords
ClearUndoRecords
CloseDocument
CreateDefaultAttributes
DateCreated
DateLastEdited
DeleteRhinoObject
DeselectAllObjects
DeselectObjects
DimStyles
DistanceDisplayPrecision
DocumentId
DocumentPropertiesChanged
EarthAnchorPoint
EndOpenDocument
EndOpenDocumentInitialiViewUpdate
EndSaveDocument
EndUndoRecord
ExtractPreviewImage
FindFile
Fonts
FromId
GetMeshingParameters
GetRenderPrimitives
GetUnitSystemName
GroundPlane
GroupTableEvent
Groups
HatchPatterns
InstanceDefinitionTableEvent
'InstanceDefinitions'
IsLocked
IsReadOnly
IsSendingMail
LayerTableEvent
Layers
LightTableEvent
Lights
Linetypes
MaterialTableEvent
Materials
MeshingParameterStyle
ModelAbsoluteTolerance
ModelAngleToleranceDegrees
ModelAngleToleranceRadians
ModelDistanceDisplayPrecision
ModelRelativeTolerance
ModelUnitSystem
Modified
ModifyObjectAttributes
Name
NamedConstructionPlanes
NamedViews
NewDocument
Notes
Objects
OpenFile
PageAbsoluteTolerance
PageAngleToleranceDegrees
PageAngleToleranceRadians
PageDistanceDisplayPrecision
PageRelativeTolerance
PageUnitSystem
Path
PurgeRhinoObject
ReadFile
ReadFileVersion
RenderContentTableEventArgs
RenderContentTableEventType
RenderEnvironmentTableEvent
RenderEnvironments
RenderMaterialAssignmentChangedEventArgs
RenderMaterials
RenderMaterialsTableEvent
RenderSettings
RenderTextureTableEvent
RenderTextures
ReplaceRhinoObject
SelectObjects
SetCustomMeshingParameters
Strings
TemplateFileUsed
TextureMappingEvent
TextureMappingEventArgs
TextureMappingEventType
UndeleteRhinoObject
UndoRecordingEnabled
UndoRecordingIsActive
Views
WriteFile
__class__
__delattr__
__doc__
__format__
__getattribute__
__hash__
__init__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__

And if you scroll down and see something interesting like “InstanceDefinitions”, you can run:

import scriptcontext
for item in dir(scriptcontext.doc.InstanceDefinitions): print item

You will get:

ActiveCount
Add
Compact
Count
Delete
Document
Find
GetEnumerator
GetList
GetUnusedInstanceDefinitionName
MakeSourcePathRelative
Modify
ModifyGeometry
Purge
Undelete
UndoModify
'UpdateLinkedInstanceDefinition'
__add__
__class__
__contains__
__delattr__
__doc__
__format__
__getattribute__
__getitem__
__hash__
__init__
__iter__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__

Note that the editor autocomplete also has all of this stuff once you start typing.

–Mitch

4 Likes

Wow… that’s… :astonished:
THANKS!

I have printed it out and pinned it to the wall!

Nice Mitch, THANKS!
that’s similar to how I dug through the system configuration data for Holomark, but I never thought of using that process in Rhino. Great!

Oh, and BTW, I have made a direct Windows start page link to the RhinoCommon Online SDK - as posted by Willem above and which I use all the time. Also the direct link to the python rhinoscriptsyntax library which is located at

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

and looks like this:

You can open any file in a text editor and look at the function definitions without having to step into them in the script editor:

–M

1 Like

Your example texteditor is not vanilla windows notepad though :wink:
I’m not sure what editor you use, but I have been happy to use
Notepad++ for the last couple of years.
https://notepad-plus-plus.org/
I run the portable version via the PortableApps platform.

-WIllem

Yeah, a lot of people seem to use Notepad++… I am using Notepad2, but I use it basically as a reader. There’s no autocomplete, but it has syntax highlighting, which is all I really needed. You don’t need to install it, just run the .exe.

–Mitch