Grasshopper

algorithmic modeling for Rhino

Hi All,

I am having trouble writing scripts in the python component for grasshopper.  Can this component use the class and def structure of normal python scripts?  It seems that my problem occurs when I use the def Main() to setup my script.  Is this script structure compatible?

Views: 1687

Replies to This Discussion

In the editor check Help->Samples->RhinoCommon basics.

If your script returns a specific error or you suspect the code to be wrong, just post your code, please... it's hard to help by just guessing.

The samples for rhinoCommon and rhinoscriptsyntax worked fine.  I see that the rhinoCommon has the def Main() structure, so I am not sure what I am doing wrong.  I can't even get it to work with something as simple as:

import rhinoscriptsyntax as rs

def Main():

      a = x

Main()

Yes, GhPython does work with function and class definitions.

Are you receiving an error? Can you post the error you receive?

There is no error.. but nothing comes out of the a output except <null>.  

I am just trying to develop an understanding of how to use the python component in grasshopper.  I have learned some python scripting before, but none of my scripts work in the component.  I can write scripts in the component that work as long as I do not try to used the functions or class definitions.

Should the simple script above work?  I'm not sure if there is something very simple that I am missing that this component requires.

Yes, it seems like it should work.

Obviously, make sure something is going into the x input.

Also, you can right click on the input and set the data type you expect. Make sure you do that correctly.

Try a basic print statement, like 

print "hello"

does that work?

what do you see when you attach a Panel component to the "out" output?

or

a = 2.0

is it the input or the script?

Benjamin, thank you for your help.  I just tried some of your advice.  It printed 'hello' in the script, and I am able to have it print 'x'.  If I set 'a = x' nothing happens unless I also have 'print a' in the script.  Then the x value comes out of the 'out', but a is still <null>.

I guess I am just having trouble setting up the output to 'a'.

import rhinoscriptsyntax as rs

def Main():

a = x
print a

Main()

It sounds like everything is working, but that there is some basic step you haven't taken. You should post screenshots of the definition and the file itself if you still want help.

Here is a screen shot of the simple python script, the GH setup and the referenced surface.  I'm still not sure why I am unable to get the output to work.  Again, thank you for the help so far.

Attachments:

Hi Newsum,

There are a couple of things to be aware of here:

The Python component input and outputs are essentially variables living in the outer most scope of the script. These are also called global variables (because they can be accessed from within the entire scope if the script). However Python does not allow you to overwrite a global variable from within a function (which is what you are trying to do). That is, unless you use the global keyword. See the second example in the attached file for how to do this.

So while it is possible to overwrite a global variable it is not really recommended. I'm not sure what you are doing, but it suspect that doing something similar to the third example in the attached file will be better in most cases. That is, when you define a function have it take some variable(s) as input, do something with this input and finally return something.

Hope that helps,
/Anders



Attachments:

Wow... Anders, that makes so much sense.  Thank you.  I knew I was doing something wrong; and yes, that third example really shows how to use the inputs and outputs. 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service