Python: Centroid and Modules part 2
This is the second part to my explanation of Centroid and Modules in python. The first part went through the conversion of a centroid function into python. It ended with a python script that had some functions, but none of the functions were called. This part will show a simple way to split functions into different files. This can come in handy in many ways including sharing files and organizing code so one change does not effect everything. If you are interested in the Centroid part click here: Part 1
After reading more about python I found out how simple it was to call functions from another file. As long as the file is in the same directory you do not have to change any files paths within the python environment (so is the case for me). This has let me split my functions into different files, and then concentrate on one file with less code making it easier to manage. After I setup my centroid file, I then needed a curve as input to pass to my centroid function. Currently I am having a user select the curve but eventually it would be part of a larger program. If you skipped over part one you might ask, why would you write a centroid function in rhino, they already have that module. Well, when your really tired and programming, sometimes writing your own function makes more sense than carefully looking at the Curve Module list. So my efforts dont go to waste, I thought making this would be a useful activity as some the topic of python modules in separate files came up in our Generative Computing class.
Before linking the files I needed to get my input curve. I used the GetObject() module internal to rhino so the user can select a curve (must be closed, since it is a polygon). This file is called “UserInput.py” and is probably the shortest file I have written:
#User Inputs import rhinoscriptsyntax as rs def Curve(): curve_sel=rs.GetObject("Select Polygon") return curve_sel
Now I have a function that will ask for a curve and then return that curve. Yes, the GetObject() does this, so why would I separate the file? Well, because eventually I will not have the GetObject() module, instead I will have a longer more complex code that figures out which curve to select, but it may also be useful to have a function that can ask for input if I need to debug. This is also not the best example for implementing multiple files, but its easy enough to understand.
My next task is to create a file that will link the two functions that are in separate files. To do this I create “CentroidMain.py” and I will import my other files. The previous files are located in the same directory and I can import them the same way I import rhinoscriptsyntax. I also can import the prefix to them just as I use “as rs” so GetObject is called by rs.GetObject, I can call my Curve() function from my UserInput.py by saying import UserInput as ui, and calling ui.Curve():
#Centroid Script Main import rhinoscriptsyntax as rs import UserInput as ui import Centroid as ctr def Centroid_Main(): my_curve=ui.Curve() ctr.Centroid(my_curve) Centroid_Main()
This code finally calls a function, which happens to be the only one in this file, Centroid_Main(). When I assign my_curve=ui.Curve(), the Curve() function from my UserInput.py script is called. Since it returns a curve, my_curve is now = to the returned curve. I can then pass my_curve into the Centroid function using ctr.Centroid(my_curve). In fact, another nice thing about our function is that we can access just one part of the centroid file. If we want just the area of the polygon, we can call ctr.Curve_Area(my_curve) and we would get the area of the polygon. This comes in handy for a few reasons, one that I used was figuring out why my function was wrong during the early steps. I then would call just the area function and just the centroid function. I found out that my area function was returning correctly, so I was able to concentrate on my centroid function!
I hope someone finds this information easy to understand and maybe learned something from it. Check out the first part on the bottom of the page for the full centroid code.