Grasshopper

algorithmic modeling for Rhino

Mmmm...after lot of work done and time expended working with Python and Python for GH, I have serious doubts about how Python component deals with type hint. So, writing an small script today I found again this problem to deal with and I decide to open a discussion that could help other people with the same issue. I've read other post about the problem, but usually the issue is solved using the type-hint option or some coerce commands.

Sometimes I have solved it just playing with type hint options in the component inputs or using "coerce" rhinoscript functions, other times if it can not be fixed I just swap to rhinocommon...but, what's the real problem behind the "type casting" in python components?

For instance (consider rhinoscriptsyntax imported as rs):

This function generates this error: "Runtime error (ValueErrorException): unable to convert 80f07b57-00b0-43a5-a0e3-1f5c7a903a23 into Curve geometry"

def customSplitCrvs(crv,param):
   crvs = rs.SplitCurve(crv,param)
   newCrvs = flipCurves(crvs)
   return newCrvs

Ok!, but if I force the script to work with curves using "coerce" functions I got this: "Runtime error (TypeErrorException): Parameter must be a Guid or string representing a Guid"

def customSplitCrvs(crv,param):
   CoerCrv = rs.coercegeometry(crv)
   crvs = rs.SplitCurve(CoerCrv,param)
   newCrvs = flipCurves(crvs)
   return newCrvs

The problem is given by rs.SplitCurve()...I know, using rhinocommon ".Split" method I can perform the operation without any problem, the thing is that could be very interesting to be able to teach or develop scripts that could be merge together in a seamless "python/rhinoscript" environment without this nightmare about TypeErrors.

Thanks in advance.

Cheers,

Ángel.

Views: 1583

Replies to This Discussion

Hi Andres

it sounds like one of these situations, when it's usually helpful to have a debugger at disposal. I hope we manage to have this hooked up (relatively) soon. While I do not know about the situation in this case (there is no complete sample), there might be simply some logic error, or a specific problem about the library, the library usage, etc. You can still use the _EditPythonScript editor for the time being.

Because I've seen this error come up at times in a place where the user is not expecting it, I'll try to make sure: are you trying to read geometry directly from the Rhino 3dm document? (This needs special code).

So, to answer your question, the hint to choose is simple to remember. The only input you should use with rhinoscriptsyntax calls is the "Guid-rhinoscriptsyntax" one. All the rest are RhinoCommon types. We could consider Point3d and Vector3d partially exceptions to this, as rhinoscriptsyntax sometimes uses these to represent the coordinates of points or the components of vectors: when points are not added to the document, or when vectors are used to move geometry, for example.

rhinoscriptsyntax is able to also be supplied other types of object and still work, but all types need to match. Normally, you do not need to use any coerceX functions, as these just convert from rhinoscriptsyntax Guid to RhinoCommon types (like Curve, Brep, etc).

Does this help? Is there anything else I can help with? 

I hope this helps,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi!, thanks for your fast and detailed reply! For sure a debugger could make the work easier :) The geometry is linked from Rhino into GH using a standard curve parameter.

The problem that I have is that using "Guid-rhinoscriptsyntax", the method rhinoscriptsyntax.SplitCurve() doens't want the ID that python component is taken from the curve parameter and tells me that he cannot convert the GUID into a curve. So, that's because I try to convert it manually using coerceX functions. But when I convert the GUID into a nurbs curve then, it asks me for the opposite, a GUID (that was the thing I was giving him in the first time)

I've print the variable before feeding SplitCurve method and it always contains the correct data type: if I use the data coming from the GH component input it prints a GUID, in the case of using coerceX command it prints a Rhinocommon nurbs curve ID.

And that's because I'm so confuse...

I've sent to you the complete code and the GH file.

Thanks in advance.

Cheers,

Ángel.

Hi Angel

did you forget attaching the file?

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

I sent to you a private message.

Hi again Angel,

You sent a fairly large script. It would be beneficial if we could discuss what is happening in public, so more people could benefit from this.

From what I could see, the first problem is simply that

rs.SplitCurve(crv,param)

actually delete its input (line 61). This is because its third default argument is "delete_input=True". This means that, next, it's impossible to find that curve ID in the document, and you get the "impossible to convert (id) to Curve geometry".

Is this enough to get you going?

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hey! Woops! I missed that default parameter. Thanks to point that. I'll review some old scripts searching for functions deleting input data; for sure I was making copies of objects to deal with this. It was not about bipolar behavior but my lack of attention to the documentation.

Thanks for your fast and useful reply.

Great :)

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service