Grasshopper

algorithmic modeling for Rhino

I'm sure this is due to my ignorance about how the component works, but I'm having trouble defining a function within the python component.

Say I have the following script:

import rhinoscriptsyntax as rs

def main( points, height):

    vector = rs.VectorCreate([0,0,height], [0,0,0])
    
    translated_up = rs.CopyObject(points, vector)
    translated_down = rs.CopyObjects(points, rs.VectorReverse(vector))
    
    return translated_up, translated_down

if ( __name__ == '__main__' ):
    main( points, weave )

If I take away the "def main():" and just keep the operations within the definition than it works fine. Am I doing something wrong or does the component work in a different way?

Views: 3007

Replies to This Discussion

Hi William

yes, as Marcus says, the Grasshopper-Python document is invisible and the ghPython component allows you to choose what you want to pass out of it. This makes RhinoScriptSyntax much faster in Grasshopper, more than what it is in Rhino. This way weu are able to use sliders, keep the definition responsive, etc...

I hope this helps,

Giulio

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

Thank you Marcus and Giulio for your replies.

So just to clarify, the structure of a Python script within GhPython should be:

  1. Import modules
  2. Define functions and classes
  3. output = function(input)

That is to say, the outputs are generated outside of any functions, and the order of definition matters, as it seems that it won't recognise a function at a point in the script if it hasn't encountered it already.

Thanks again for your help.

the order of definition matters,

Yes, that's how Python works. It's an interpreted language and functions are just callable variables. It's completely another world than statically compiled languages.

Order can be as you want. There is no defined structure in Python. Within a function, you can define a class that defines methods. All up to you. Of course you should do it carefully, though!

Giulio

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

Understood, thanks!

Hi Marcus,

the global variable method is fine: it's not usually considered a very great practice... but it works. The main reason I would discourage people from using it, is that it will very probably not work, or work differently, in the upcoming compilation mode (based on OOP) we would like to introduce.

We still need to exactly see how that should work, so I will not make this a fixed statement, for now! :)

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