This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-first-template.html
executable file
·255 lines (200 loc) · 17.6 KB
/
my-first-template.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My First Template – Livestock3D</title>
<!-- Meta -->
<meta name="description" content="Livestock3D – ">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Social -->
<meta property="article:author" content="Christian Kongsgaard" />
<meta property="article:section" content="posts" />
<meta property="article:published_time" content="2018-03-22" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="My First Template"/>
<meta property="og:description" content="Template Now when the Grasshopper component is written, it is time to write the template and the CPython script. Navigate to where you put the templates.py and open it. It can easily be found by in explorer typing: %appdata%\McNeel\Rhinoceros\5.0\scripts\livestock3d Python scripts can be …"/>
<meta property="og:site_name" content="Livestock3D" />
<meta property="og:url" content="https://livestock3d.github.io/my-first-template.html"/>
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="My First Template">
<meta name="twitter:description" content="Template Now when the Grasshopper component is written, it is time to write the template and the CPython script. Navigate to where you put the templates.py and open it. It can easily be found by in explorer typing: %appdata%\McNeel\Rhinoceros\5.0\scripts\livestock3d Python scripts can be …">
<meta name="twitter:url" content="https://livestock3d.github.io/my-first-template.html">
<!-- Feed -->
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Open+Sans:regular,bold">
<link rel="stylesheet" type="text/css" href="https://livestock3d.github.io/theme/css/w3.css">
<link rel="stylesheet" type="text/css" href="https://livestock3d.github.io/theme/css/style.css">
<link rel="stylesheet" type="text/css" href="https://livestock3d.github.io/theme/css/jqcloud.css">
<link rel="stylesheet" type="text/css" href="https://livestock3d.github.io/theme/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="https://livestock3d.github.io/theme/css/pygments-highlight-github.css">
<link rel="stylesheet" type="text/css" href="https://livestock3d.github.io/static/custom.css">
<!-- Icon -->
<link rel="shortcut icon" type="image/x-icon" href="https://livestock3d.github.io/favicon.ico">
<!-- JavaScript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://livestock3d.github.io/theme/js/jqcloud.min.js"></script>
</head>
<body>
<div class="w3-row w3-card w3-white">
<header id="header">
<a href="https://livestock3d.github.io" id="header-logo" title="Home">CK</a>
<nav id="header-menu">
<ul>
<li><a href="https://livestock3d.github.io/pages/contributing-to-livestock.html">Contributing to Livestock</a></li>
<li><a href="https://livestock3d.github.io/pages/how-is-it-working.html">How is it Working?</a></li>
<li><a href="https://livestock3d.github.io/pages/welcome-to-livestock3d.html">Welcome to Livestock3D</a></li>
<li class="w3-bottombar w3-border-white w3-hover-border-green" style="font-weight: bold;"><a href="https://livestock3d.github.io/category/posts.html">posts</a></li>
</ul>
</nav>
</header>
</div>
<br><br><br>
<article>
<header class="w3-container col-main">
<h1>My First Template</h1>
<div class="post-info">
<div class="w3-opacity w3-margin-right w3-margin-bottom" style="flex-grow: 1;">
<span><time datetime="2018-03-22T00:00:00+01:00">to 22 marts 2018</time> in <a href="https://livestock3d.github.io/category/posts.html" title="All articles in category posts">posts</a></span>
</div>
<div>
<span class="w3-tag w3-light-grey w3-text-green w3-hover-green">
<a href="https://livestock3d.github.io/tag/python.html" title="All articles with Python tag">#python</a>
</span>
<span class="w3-tag w3-light-grey w3-text-green w3-hover-green">
<a href="https://livestock3d.github.io/tag/grasshopper.html" title="All articles with Grasshopper tag">#grasshopper</a>
</span>
<span class="w3-tag w3-light-grey w3-text-green w3-hover-green">
<a href="https://livestock3d.github.io/tag/tutorial.html" title="All articles with Tutorial tag">#tutorial</a>
</span>
<span class="w3-tag w3-light-grey w3-text-green w3-hover-green">
<a href="https://livestock3d.github.io/tag/my_first_component.html" title="All articles with My_First_Component tag">#my_first_component</a>
</span>
</div>
</div>
</header>
<br>
<div class="col-main w3-container">
<section id="content">
<h2>Template</h2>
<p>Now when the Grasshopper component is written, it is time to write the template and the CPython script.
Navigate to where you put the templates.py and open it.
It can easily be found by in explorer typing:</p>
<blockquote>
<p>%appdata%\McNeel\Rhinoceros\5.0\scripts\livestock3d</p>
</blockquote>
<p>Python scripts can be opened and edited with a range of editors: Notepad, <a href="https://notepad-plus-plus.org/download/v7.5.6.html">Notepad++</a>
or Spyder, which comes with the Anaconda installation, is among the simplest. </p>
<p>The top function in templates.py is the function we used in the component: pick_template()</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">pick_template</span><span class="p">(</span><span class="n">template_name</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Writes a template given a template name and path to write it to.</span>
<span class="sd"> :param template_name: Template name.</span>
<span class="sd"> :param path: Path to save it to.</span>
<span class="sd"> """</span>
<span class="n">template_name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">template_name</span><span class="p">)</span>
<span class="k">if</span> <span class="n">template_name</span> <span class="o">==</span> <span class="s1">'my_first_template'</span><span class="p">:</span>
<span class="n">my_first_template</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">template_name</span> <span class="o">==</span> <span class="s1">'plot_graph'</span><span class="p">:</span>
<span class="n">plot_graph</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">NameError</span><span class="p">(</span><span class="s1">'Could not find template: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">template_name</span><span class="p">))</span>
<span class="k">return</span> <span class="bp">True</span>
</pre></div>
<p>pick_template is a convenient way of writing out different templates with the same function.
You just need to give the name of the template and a path, where the template should be written.
At the moment it is possible to write out two templates. For this tutorial we will only use the first one.
When pick_template receives the template_name: "my_first_template", the function my_first_template() will be called.
Therefore, we need to create a function called my_first_template()</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_first_template</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Writes a template.</span>
<span class="sd"> :param path: Path to write it to.</span>
<span class="sd"> :type path: str</span>
<span class="sd"> :return: The file name</span>
<span class="sd"> """</span>
<span class="n">file_name</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'/my_first_template.py'</span>
<span class="nb">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span> <span class="o">+</span> <span class="n">file_name</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"# Imports</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"import sys</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"sys.path.insert(0, r'C:\livestock3d')</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"import livestock3d as ls</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"# Run function</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"ls.my_function(r'"</span> <span class="o">+</span> <span class="n">path</span> <span class="o">+</span> <span class="s2">"')</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"# Announce that template finished and create out file</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">"print('Finished with template')</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">return</span> <span class="n">file_name</span>
</pre></div>
<p>The function takes a path as an input. The path specifies, where the template should be written.
We define a variable with the file name of the template and creates that file with the open statement.</p>
<p>To let Python know that we want to use scripts from the C:\livestock3d folder, we need to add it to the PYTHONPATH.
We do that with sys.path.insert(). After that we can import livestock3d.</p>
<p>After the imports we can call the function my_function from livestock3d.
The function is not yet created, but we will do that in the next paragraph.</p>
<h2>Livestock3D in CPython</h2>
<p>We have now written both the code for the component and the template. The only piece missing is the CPython part.
The CPython code is what does the actual functionality that we want. In this case to dublicate a text string.
We will write the code in the C:\livestock3d\livestock3d.py file.</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_function</span><span class="p">(</span><span class="n">folder</span><span class="p">):</span>
<span class="nb">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">folder</span> <span class="o">+</span> <span class="s1">'/data_file.txt'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span>
<span class="n">my_lines</span> <span class="o">=</span> <span class="p">[</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">file</span><span class="o">.</span><span class="n">readlines</span><span class="p">()]</span>
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">repeat</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">my_lines</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
<span class="n">line_to_write</span> <span class="o">=</span> <span class="n">my_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">result_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">folder</span> <span class="o">+</span> <span class="s1">'/result.txt'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">repeat</span><span class="p">):</span>
<span class="n">result_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line_to_write</span> <span class="o">+</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span><span class="p">)</span>
<span class="n">result_file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">return</span> <span class="bp">None</span>
</pre></div>
<p>my_function takes a path to a folder as input. It assumes that there is a file in that folder called: "data_file.txt".
That file is created by the Grasshopper component, so make sure that the name is correct, otherwise my_function() can not run.
After opening data_file.txt, the code turns the file into a list called my_lines. To be explicit we name two variables:
"line_to_write" and "repeat". They are the first and second line in the list, respectively. Then we open "result.txt" and
we write "line_to_write" "repeat" number of times. Thereafter, the file is closed and we are done.</p>
<p>Now we should test our Grasshopper component! We will do that in the next post.
<a href="https://livestock3d.github.io/my-first-component-run.html">First Component Run</a></p>
</section>
<br><br><br>
<footer>
<div class="adjust-width">
<div id="author-block" class="w3-light-grey w3-border">
<div id="author-info">
<a href="http://ocni-dtu.github.io"><img style="width: 60px; height: 60px;" src="https://github.com/livestock3d/livestock3d.github.io/blob/master/images/profil.jpg" onerror="this.src='theme/images/avatar.png'" alt="Avatar"></a>
<div style="margin-left: 20px; margin-top: 15px;">
<a href="http://ocni-dtu.github.io"><span id="author-name" class="w3-hover-text-dark-grey">Christian Kongsgaard</span></a>
<p id="author-story">I'm a former Master Student of the Technical University of Denmark (DTU).
I'm developed Livestock for my thesis.</p>
</div>
</div>
</div>
</div>
<br><br><br>
<p style="font-size:10pt; font-style: italic;">Did you like this article? Share it with your friends!</p>
<div id="share" class="share">
<a href="http://www.facebook.com/sharer.php?u=https%3A//livestock3d.github.io/my-first-template.html&t=Livestock3D%3A%20My%20First%20Template" target="_blank" class="w3-btn w3-indigo">
<i class="fa fa-facebook"></i> <span>Facebook</span>
</a>
<a href="http://twitter.com/share?url=https%3A//livestock3d.github.io/my-first-template.html&text=Livestock3D%3A%20My%20First%20Template" target="_blank" class="w3-btn w3-blue">
<i class="fa fa-twitter"></i> <span>Twitter</span>
</a>
<a href="https://plus.google.com/share?url=https%3A//livestock3d.github.io/my-first-template.html" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;" class="w3-btn w3-red">
<i class="fa fa-google-plus"></i> <span>Google</span>
</a>
</div>
<br><br><br>
</footer>
</div>
</article>
<footer id="footer">
<div id="footer-copyright" class="w3-center w3-small w3-text-grey w3-padding-48">
<span>
©
2018 Christian Kongsgaard
</span>
</div>
</footer>
</body>
</html>