Python code sometimes work and sometimes doesn't, same input

I have a simple piece of code that sometimes does not work and after a while does. It’s driving me insane as it is impossible to debug. The original code comes from here

I am trying to get the Scale value of Blocks. I am using BlockInstanceXform() to get the form. I am getting a ‘unable to find InstanceObject’ … until I am no longer getting the error and the code works. No change in the script, no change in the inputs, nothing! How is it possible that the exact same piece of code will sometimes fail and sometimes not fail? This is deeply frustrating and I am happy to find out what I am doing wrong to be causing this behavior.

The simple code:

“”“Provides a scripting component.
x: The x script variable
y: The y script variable
a: The a output variable”""

author = “pascal”
version = “2018.03.02”

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
from scriptcontext import doc

U = False

if y:
xform = rs.BlockInstanceXform(x)

scaleX = Rhino.Geometry.Vector3d(xform.M00,xform.M10,xform.M20).Length
scaleY = Rhino.Geometry.Vector3d(xform.M01, xform.M11,xform.M21).Length
scaleZ = Rhino.Geometry.Vector3d(xform.M02, xform.M12, xform.M22).Length

if scaleX == scaleY == scaleZ: U = True

print "X scale = " + str(scaleX) + " Y scale = " + str(scaleY) +" Z scale = " + str(scaleZ)

A small, equally frustrating update. After I failed to execute the code, then succeeded in making it work (no change of the code itself) now I am getting a new type of error:

  1. Solution exception:2305f484-347c-43eb-a2fd-8aa818451aef does not exist in ObjectTable

Again, same code, no change. Same file, same inputs, same everything.

After restarting Rhino.Inside (that’s where I am using the code from) I got back to the original error:

  1. Solution exception:unable to find InstanceObject

And not sure if it’s connected in any ways, but if I try to execute:
obj = rs.GetObject(“Select block to query”)
xform = rs.BlockInstanceXform(obj)
as a way to test my solution, I get another error:

  1. Solution exception:The method or operation is not implemented.

Hi @dnenov

Are you running this in Rhino? Or is this an attempt to run this in Grasshopper? Pascal’s sample is for Rhino.

If it’s in Grasshopper, please attach the definition. And also please have a read at Help us help you. Again, if it’s in Grasshopper, then it’s trickier to make this work. There are no native GH components relating to blocks for a reason! But if the goal is limited, we can probably make it work with the due care. It’s important to notice that only the Rhino document has blocks.



Giulio Piacentino
for Robert McNeel & Associates

Hi, Giulio!

Thank you for the response. Here are the files that can be used to reproduce the case: Case.3dm (117.9 KB) (11.6 KB)

I think I have already guessed the lack of reliability. But I have found traces of this particular example and the code behind it seems straightforward enough. It’s the lack of consistency that baffles me.

Any insights are super welcomed!

OK; so you are trying to adapt that script to Grasshopper. You see, the Grasshopper rhinoscriptsyntax document does not handle blocks, because Grasshopper itself does not have a block item to pass around. We can however switch to use the Rhino document, so we can make your sample work.

First, I’ll change x → Type Hint → Guid. This is really only a Guid for Grasshopper, as it doesn’t know anything about the shape or geometry or attributes.

Then, in the script, we can target the Rhino document:
sc.doc = Rhino.RhinoDoc.ActiveDoc

before the script ends, we must put back the variable doc in the scriptcontext module to its original value, which is ghdoc.

Now the script work! More info on ghdoc.



Giulio Piacentino
for Robert McNeel & Associates (7.1 KB)

1 Like

Giulio, thank you for your intervention! I was looking at this solution and that’s how I must have gotten it to work those few times, before breaking it again for one reason or another.

I am still mystified about the ghdoc - where doest this object come from? We use it out of thin air, no imports needed. I suppose it’s part of the context … or it is the context itself?

Other than that, really clear! Thank you so much for your clarification and patience! Much appreciated!


There’s some information in

The only meaningful usage of the ghdoc variable (that is also an instance attribute in SDK mode) is shown at: