{"id":1473,"date":"2021-01-10T23:52:43","date_gmt":"2021-01-10T23:52:43","guid":{"rendered":"http:\/\/www.pycheung.com\/weblog\/?p=1473"},"modified":"2022-02-06T17:07:08","modified_gmt":"2022-02-06T17:07:08","slug":"ghpython-execute-in-steps","status":"publish","type":"post","link":"https:\/\/www.pycheung.com\/weblog\/post\/1473","title":{"rendered":"ghpython execute in steps"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"960\" height=\"540\" src=\"https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps-1.gif\" alt=\"\" class=\"wp-image-1480\"\/><\/figure>\n\n\n\n<p>A very simple example to illustrate a basic code structure to create iterative algorithmic sketches with ghpython in combination to the usual components within Rhino Grasshopper (GH).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full wide\"><img loading=\"lazy\" width=\"1123\" height=\"459\" src=\"https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps_template.png\" alt=\"\" class=\"wp-image-1475\" srcset=\"https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps_template.png 1123w, https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps_template-800x327.png 800w, https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps_template-1024x419.png 1024w, https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps_template-270x110.png 270w, https:\/\/www.pycheung.com\/weblog\/wp-content\/uploads\/2021\/01\/python_execBySteps_template-768x314.png 768w\" sizes=\"(max-width: 1123px) 100vw, 1123px\" \/><\/figure>\n\n\n\n<p>The basic idea is to mimick the way Processing and Unity structure the code in two sections 1. Setup &#8211; initial configuration (processing: setup() &amp; unity: Start()) 2. Draw &#8211; Code that repeats (processing: draw() &amp; Unity: Update()). A third section is added which could be attached to &#8220;setup&#8221; or &#8220;draw (loop)&#8221; to visualise the initial configuration or the current state of the system coorespondingly (remove the link between makeDirty &#8211; dirty for the former (Note: &#8220;dirty&#8221; is a term from Maya hypergraph when a node further up the graph trigger changes to a node further down the graph, I believe the GH community calls it &#8220;expires&#8221;). this link forces GH to execute the middle block first given the way the GH graph is structured).<\/p>\n\n\n\n<p>The setup block contains a python node with a python object. The outputs are instances of the object (either as one instance or multiple instances in a list as in the example). Changes made to these instances are retained (e.g. position and vector in the example) for the next iteration &#8211; this makes it possible to record information while we execute part of the GH graph.<\/p>\n\n\n\n<p>To run the &#8220;draw (loop)&#8221; block either induce a change by changing one of its inputs (for example a button connected to &#8220;run&#8221;) or use the GH timer component to trigger the first node within the block repeating at a defined time interval.<\/p>\n\n\n\n<p>GH file with python code:<br><a href=\"http:\/\/pycheung.com\/temp\/python_execBySteps_template.zip\" data-type=\"URL\" data-id=\"http:\/\/pycheung.com\/temp\/python_execBySteps_template.zip\" target=\"_blank\" rel=\"noreferrer noopener\">pycheung.com\/temp\/python_execBySteps_template.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A very simple example to illustrate a basic code structure to create iterative algorithmic sketches with ghpython in combination to the usual components within Rhino Grasshopper (GH). The basic idea&#8230;<\/p>\n<div class=\"royalwp-readmore-wrap\"><a href=\"https:\/\/www.pycheung.com\/weblog\/post\/1473\" class=\"royalwp-readmore\">Explore more <i class=\"far fa-hand-point-right\"><\/i><\/a><\/div>\n","protected":false},"author":2,"featured_media":1475,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[155,158,157],"tags":[],"_links":{"self":[{"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/posts\/1473"}],"collection":[{"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/comments?post=1473"}],"version-history":[{"count":8,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/posts\/1473\/revisions"}],"predecessor-version":[{"id":1566,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/posts\/1473\/revisions\/1566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/media\/1475"}],"wp:attachment":[{"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/media?parent=1473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/categories?post=1473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pycheung.com\/weblog\/wp-json\/wp\/v2\/tags?post=1473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}