Hi, while getting familiar with hops with the wonderful video
I was wondering how to define one hops component per file ?
In the following code, I define 2 components in the same python code. I was unable to split the code in two files. I apologize in advance if the answer is obvious but I am a python beginner.
import rhino3dm
from flask import Flask
import ghhops_server as hs
# register hops as middleware
pointatServer = Flask(__name__)
hops = hs.Hops(pointatServer)
@hops.component(
"/pointatopposite",
name="PointAtOpposite",
description="Get Opposite Point Along the Curve",
icon="pointat.png",
inputs=[
hs.HopsCurve("Curve", "C", "Curve to evaluate"),
hs.HopsNumber("t", "t", "Parameter on curve", default=2.0),
],
outputs=[hs.HopsPoint("P", "P", "Opposite point on curve at t")],
)
def pointatOpposite(curve: rhino3dm.Curve, t):
newT = curve.Domain.T1 - t
return curve.PointAt(newT)
@hops.component(
"/pointat",
name="PointAt",
description="Get Point Along the Curve",
icon="pointat.png",
inputs=[
hs.HopsCurve("Curve", "C", "Curve to evaluate"),
hs.HopsNumber("t", "t", "Parameter on curve", default=2.0),
],
outputs=[hs.HopsPoint("P", "P", "Point on curve at t")],
)
def pointat(curve: rhino3dm.Curve, t):
return curve.PointAt(t)
if __name__ == "__main__":
pointatServer.run(debug=True)
I’d be interested to know why you’d want something like this…
I prefer to have all my Hops functions in a single file and all the utility functions in another (or few others), but I may be missing something
I want something like this to have a better configuration management of GH definitions.
I have GH definitions that grows to >500 components. This is a proving difficult to manage. Last week, I made a modification in a very big .gh file and the output fell apart with no apparent reason.
This is the main reason I am very exited by Hops and I want to switch to GH/Hops ASAP. I will be able to break down a GH definition into as many files I want and each configuration change will be human readable as a python code change. I will loose the fun of drawing the components / links but I have to find the sweet spot between fun and configuration management.
In order to do that, I need to spread my python/hops code into as many files I want, hence this topic.
Apologize if my question wasn’t clear…
I wanted to ask: why would you want to have one Hops function per python files?
It’s a true question, I’m not saying it’s wrong, just wondering why it could be better
I guess it’s easier to maintain your code, at the cost of duplicating the import of libraries on every files?
Here’s an example where I have all my Hops functions in one file, and I can still call any of them in any Gh definitions…
you’are right, my point is to avoid too large python files. I translated this into one hops component per python file (an intellectual shortcut for sure).
I had a look at your scripts in your hops repo (nice project BTW). Your biggest script is hops_plots.py which 582 SLOCs (Source Line of Code) which is still manageable.
Best practices suggest that the max SLOCs per file should be between 300 & 2000. It is a matter of taste probably. In my case, I tend to stay below 500 SLOCs per file. My idea for the port of my very complex GH file to hops is to break it down into python file of no more than 500 SLOCs. I surmise I will one hops component per file but I might be wrong on that.
I’ve learning about hops recently. It is a great option to actually have proper version control on GH components. Nevertheless there are few considerations that I’m not hable to understand so far.
Here some questions:
.- Can I use more that one Hops component in the same definition when connecting to Cpython via flask?
.- How to add the URL path to the Hops component automatically?
.- Can I build a library of hops components that connect automatically to the cpython / flask app?
I’d love to be pointed to some direction… @stevebaer