Grasshopper

algorithmic modeling for Rhino

How to add a warning or error message to a component in Python?

After checking the post How to throw a warning or error to turn the component orange or red?, I've noted that the method AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Warning message here"); appears to work for either VB or C#. How do I do the same thing for raising warnings or errors in Python instead?

Views: 3795

Replies to This Discussion

import Grasshopper.Kernel as gh


e = gh.GH_RuntimeMessageLevel.Error
w = gh.GH_RuntimeMessageLevel.Warning

ghenv.Component.AddRuntimeMessage(w, 'this is a warning!')

ghenv.Component.AddRuntimeMessage(e, 'this is an error!')

Okay, this works in my Python scripting component, but ghenv is not recognized when I try to turn the script component into a custom component (i.e. when I try to compile it). I get the following error message:

     Solution exception: global name 'ghenv' is not defined

Is there a namespace I should be adding before the ghenv or something else I should be importing?

I've been able to find a way to call the AddRuntimeMessage method as follows:

gh.GH_ActiveObject.AddRuntimeMessage(w, 'Incorrect input.')

However, I appear to be missing one input. After checking the GH SDK, it only lists two necessary inputs (warning level, text). What would be the third input I'm missing?

AddRuntimeMessage is an instance method, but you're invoking it on the type like a static method. I'm guessing the third argument is the actual component instance this warning is supposed to be recorded by.

What form of the active component do I input? It appears to be expecting a GH_ActiveObject, but I don't know how to get the address/GUID/address of the actual component instance itself.

Sorry, that goes beyond my python knowledge.

I also am working with a python component that, after compiling in Rhino WIP6, (6.0.17192.6471, 07/11/2017), I get the same error.  

What is the proper way to declare/import ghenv in a python component...to prepare for compiling in Rhino 6?

Not sure if you saw the response below, but the solution eventually came to:

self.AddRuntimeMessage(w, 'Component failed to collect rule instance input.')

where 'self' references the RunScript class that you usually put all your scripts in to be compiled, i.e. 

class MyComponent(component):

     def RunScript(self, S, T, Sc):

         ...

         self.AddRuntimeMessage(w, 'etc')

Okay, this appears to work best for what I want to achieve. The solution I reached was:

self.AddRuntimeMessage(w, 'Component failed to collect rule instance input.')

where 'self' references the GH_Component.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service