bvn-architecture / grasshopper_mirror

0 stars 0 forks source link

find a way to trigger the process without being wired into the rest of the graph #4

Open notionparallax opened 1 week ago

notionparallax commented 1 week ago

So that the documentation nodes can be isolated, but then still trigger on every update cycle

notionparallax commented 1 week ago

Copilot says:

Yes, you can connect your Python node to the update cycle in Grasshopper using the Grasshopper API. This way, your script will run whenever the graph updates, without relying on a fixed timer. Here's a general approach:

  1. Use the GH_Document Events: Grasshopper's GH_Document class has events that you can subscribe to, such as SolutionStart and SolutionEnd. These events are triggered whenever the Grasshopper document (graph) updates.

  2. Create a Custom Component: You can create a custom Grasshopper component in Python that subscribes to these events. Here's a basic example:

    import Grasshopper
    import GhPython
    import Rhino
    
    class CustomComponent(GhPython.Component):
        def __init__(self):
            self.OnPingDocument().SolutionStart += self.OnSolutionStart
    
        def OnSolutionStart(self, sender, e):
            # Your custom code here
            print("Graph updated!")
    
        def RunScript(self, input):
            # Your main script logic here
            pass
    
    # Instantiate the component
    component = CustomComponent()
  3. Add the Component to Grasshopper: You can add this custom component to your Grasshopper canvas. It will automatically run your custom code whenever the graph updates.

This approach ensures that your Python script runs only when the graph updates, without needing to be connected to the main graph or relying on a fixed timer.

Would you like more detailed instructions or help with a specific part of this process?

Source: Conversation with Copilot, 05/09/2024 (1) refreshing the python script engine - Grasshopper. https://www.grasshopper3d.com/forum/topics/refreshing-the-python-script-engine. (2) Rhino News, etc.: Python Scripting for Rhino/Grasshopper online .... https://blog.rhino3d.com/2022/12/python-scripting-for-rhinograsshopper.html. (3) RAM | STAAD Forum - RAM.Concept API - Grasshopper Plugin. https://bentleysystems.service-now.com/community?id=community_question&sys_id=9530f2911be5ca10f3fc5287624bcbe3.