Grasshopper Python inside Rhino 7 - Beginners Tutorial - 001

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back i hope this will be a quick one i want to talk about a tool which well didn't save my life but it definitely definitely saved me a lot of time in the past i'm talking about python for the people who don't know python python is a scripting slash programming language which also exists or which also has an interface within rhino and there is a scripting node within grasshopper and that's what i want to talk about what i like with python is that it's very easy to learn and to understand and i will show you very simple examples which you can do with the normal grasp of scripting but sometimes it just saves time and when you cannot find the component or you would need to install a new grasshopper component and sometimes this this tool could save you could save you that time in this tutorial you will not learn how to script everything in python python is a huge universe we will just i will just go to go through very simple basics which can be very powerful still so i want to cover three things four things finding the maximum of a list which is actually not that simple in grasshopper the maximum and the minimum and and then i will show you how a while loop works and a for loop so these are the four things that's we're gonna look at very quickly and we will make some a very simple a script which does something that's what i also like with pi with learning so my problem in the past was that i learned i tried to learn python but then it didn't do much it it just shows you numbers and you calculate some numbers and but then so i was always thinking how can i make an application with it and that's very hard at the beginning so what i like that that you can access it with through grasshopper is that you can see a result right away because that makes you want to learn more so this is the thing this is the thing we're gonna build we built a a tool which can offset but it can just it can offset like a more in a way that you can control a bit more so in that in that case offset like in pairs or always like two two numbers to get these like paired circles you can do that of course in in grasshopper and it's more about to show how how powerful the scripting could be in python there are other scripting tools here as you can see there is a c-sharp there's a visual basic script the difference it's it's a bit different to learn you have much more stuff at the beginning which is very confusing but it's faster when you have uh bigger stuff to calculate you can start with any to learn any less scripting language i think they all have things in common what i like with python so if i choose this here grasshopper python script it's kind of much more simpler here and everything here in green is just a comment basically it just tells you what kind of inputs you have and outputs which are here you actually don't need this that's that's not necessary and these are variables they're also not necessary so in theory you could start like this and it still works fine so you don't need the rest so that that's already makes it much more simpler and it already gives you it already gives you uh the first line import rhino script syntax srs which we will actually not use for this um script but we keep it in here and i can test here and there's and then you have an output output window and that will show you the results um so in that script i in that script i created a loop two loops actually i created my own inputs my own outputs and the first loop is a while loop which goes through goes for a function and generates these numbers which i then use as an offset and then i have another loop a for loop which goes through that list and adds a number to this so so that creates me the second ring here and then i just combine it and then there's there's another there's another thing i want to show you is the maximum and minimum that could be sometimes very very useful okay so let's let's try to recreate this here i will just i will disable this function here and we'll just rebuild the script so yes so let's start with the component here we open it up and we can create a while loop a while loop for works this way so i'm gonna write here while first i sorry first i need to actually define a variable a variable you can define with for example i as item let's call it item or let's call it number it doesn't really matter you can choose whatever you want and i say n is one or n is zero and the while loop works in a way that for example i can i can define a condition so i can say as long as n is smaller sorry smaller than a certain number for example 100 then the number is the number plus and the num oh sorry the is the number plus one so it means at the moment at the beginning the number n is zero and as long as the number n is below 100 we adding always one and this is basically a loop so it goes through that loop until n is is a hundred it can't go it can't go grow bigger than that and we can so this is this very simple kind of uh way to work with it and i can test this and forgot something of course and now i could say print and print the number and now it printed all the numbers until 100 and but i want to actually see a zero also here in this list so let's start with zero that means i take this i'm going to cut it out and i put it before here so it prints first my number the number n and then it adds one and then it it prints it out and it adds one and it prints it out as one and so on and that creates me this list from from 0 to 100 okay and and when i look here in the output i can also add here the output and it will show me this list and then a is nothing because i didn't define anything for a so and at the moment it's all output the output and um by the way it's going from it goes from 0 to 99 because the definition says it shouldn't be larger than 100 or it shouldn't it's as long as smaller than 100. so what if i want to and by the way so this works very similar to creating a serie a series here so the the math set you know sets here series works very similar so what if i don't what if i want to change that number here the starting point on the fly i i don't want to always have zero at the beginning and i don't want to always go into the script what i can do is i can actually add a slider and i could call this start value you could have min minus 100 and 100 it's you can choose anything you want so now i have a slider from -100 to 100 and i can add this here on x and then instead of me saying n is zero i could say n is x okay now because it's hundred it already doesn't work because then n would be already bigger but if i go now down here then you can see that um it creates my list depending on my input it is already updating it perfectly so so my while loop works quite nice and it starts where i want it to start you can also change this here you don't have to call this x but keep in mind if you change that here you also need to change then your variable so if i call this start value then the n is then n is start value now if i want to also change my final my goal number then i can do the same i could for example say here and as long n is not is smaller than y that also works so i could add a number here let's do this first i could say here and value place this in here and then i can change this to y and now you can see that my my list only goes to 77. let's make it a more a smaller list so it's easier to see so now my list goes from -3 to 8 to 17. and i could i could use that already and create let's create a circle i mean theory could do this we don't need an offset really you can also play with an offset of course you can use these numbers offset but you could also feed it in and degrade your circles so that's that that's a possibility and then if you so you could play with this like like this now let's say i want to not just control the step size so at the moment we just always add one what if we want to change the step size to a number and we want to have a slider then it works exactly the same however we need to add something first so we need to add here we need to add a new number and we can copy this value here and then we call this step size and add this here and maybe this is better it's not a minus minimum maybe one so we can add this here and now we can change this one to set and now you can see that it basically can change the distance between the circuits but it doesn't go further than 23 at the moment so we can go back to something here here the minus doesn't really makes any sense in that in that case but it could be of course if it's a more linear kind of object if it's like lines parallel to each other so we have with a very simple scripting with very simple scripting we basically recreated this one so here the first number is zero so we could do the same here here's the step size and here is it's a bit different it's actually counting the how many you how many circles you want in here it's it's actually slightly different it goes as as long as you reach a certain distance which can be an advantage sometimes so if you want to fit something within a certain area then this makes more sense okay now if we want to add more functionality to this then we can first of all what we can we can get a specific output we can create a specific output for for for this for these numbers yes we have an output that takes basically whatever whatever is in in here and and adds it here but if you want to be more specific then there's another way to do that so first what we need to create is like a variable again and in this case we create a because we have the output a but we could of course create a different name let's do that maybe so instead of a we call that offset and offset is a list and the list is empty actually it's uh sorry it's not empty because uh i'll show you how that works so at the moment this list has nothing in there and i want to add things and i won't want to add what's instead of like printing it and having a list in here or in the output i want to collect it into a list and to collect it there's a simple function it's called append it's called sorry offset dot append and then n n and that means every time when it goes through a loop it takes the result and adds it to the offset and if we then we could then write here print offset then it creates this list at the end the problem is that it always takes because it's a loop it takes already the um the result from the first function so it doesn't it doesn't include the first number and that's why instead of like putting here nothing i put in here n so that means the offset already has a first the first number in so if i run that again you can see it starts now from one now of course this now has a problem because it's it it doesn't understand what what's going on if i do this then it works so then there's no problem now in the offset is my offset list i will uh i need to sorry i changed that again forget it that it's a wrong thinking of mine so don't do that it's actually good that this happened because yes no so that's we don't want that because we want this to be here so first of all let's get rid of the print we don't need to print just take this offset append n that's good and then because we don't want anything which is larger than y in the list because that's what it did no it's it was adding the last number which was already bigger than 100 so forget what i said before or keep it in mind that's it's actually good that this happened the mistake so that means while the number is smaller than y which is in that case 100 or smaller of course or any value you want depending on your slider please add the number n to the list offset which is defined here and then add a value which i defined with set here with 16 or again the number you can then choose and then add it to and then continue with your with your loop until until this is reached here i hope it's clear i think i think and then at the end we have print offset now it's now you can see that it's combined here so the numbers are combined as one list and not each value in one big sausage so basically it's it already creates more clarity also we don't need this anymore we don't need to print it anymore really now if i want to then so if i want to finish this loop and then start another loop and go through that list which i just created and add a number as i said before so then i could use a different loop for that we could use the for loop the for loop works like this for a number which then we call let's call it mamba let's call it the m for m in offset what it means is go through each value in the list offset go for each item in the list offset and do something with it for m in offset is m plus sorry m is m plus and now we could add a number or another variable so we could add now um okay now it of course it creates this um arrow we could add another one here let's add another one here and wait this is u let's just keep let's just use that and u let's add this u here now if i test this um it doesn't work it creates me a runtime error and this is because this while function isn't hasn't stopped really so we can add another condition here and say if n is larger than y break if n is larger sorry it's larger or same as y that's what you need to write if n is larger then it should work now yeah okay i'm sorry yeah there was something wrong with the fall the for loop is not finished yet so first of all we need a value here we don't have a value n and we need we need to define m first so as you can see here first of all we want to have m needs to have a value need to be defined we can say m is zero so that need to be done first and now m is defined and we also need to provide something for you so let's take this i don't know what i call it what to call it let's put this in four now now it worked so in order to see what's in here what's in this for loop we could say print print m okay so now you can see that it was creating so this is my output from the while loop so i have a while loop here that creates me this list the offset list and and then there you can see the values coming out of the for loop the for loop goes through that list and adds the variable u to that value which is in that case it's four so it's it's one plus four is five 31 plus five is a four is 35 and so on 61 plus 5 is f plus 4 is 65 and 91 plus 4 is 95. that's what it does so in order to get that also out here we create another list pair is an empty list and we adding we do the same here you know we add the offset in that case we add the opposite afterwards i need to be careful what i'm saying here but basically with in the for loop we do the same as we did here we have this pair as a list as an empty list and then it the for loop goes through the offset list and for each value in the offset list it adds the variable u which we this we call the pair value okay so we call it pair and then append m and we we get rid of this here and we can we can say print maybe it's easier off set and print the pair okay now it should work yeah one of my lights got bust that means soon we'll be in the dark here okay let's see if i still have a battery in the camera yeah still have okay so that's done and now and now i will add an offset here uh another output here which i call pair which is the same as here of course it doesn't work what now it works now if i combine this let's let's get rid of this one here let's move it somewhere now that could combine these values with merge is it merge merge by the way merge also works like this you can add stuff here if you want now we have all the values in one list and we could add this to the radius and it creates me this one and then of course i can still play with my values here the step size the power of python so now i'll show you something very simple uh but i used it a lot already in the past so i copy this no no i i just create a new one anyway i have my list here my new list i mean i can see yeah i have to the biggest value here 92 but for example for a list which is random and it's not sorted then and you want to get the maximum or the minimum then you could use python because if for example if i would use the maximum the maximum component provided by a grasshopper then i would go in here and i would need to provide another number which i never really got i just didn't understand it it's like comparing one number so this is something i don't understand i don't really completely understand this component anyway maybe somebody explained me that if i want to get the max the maximum number out of this list i can actually create a very simple python script i go in here and now we learn something one more thing very important in python and how to use this i could go in here again i can kick all this stuff here or i don't need that it was simple here here was easy because these aren't uh only one a value it's only one value it's not a list so that's something important to keep in mind because at the moment uh python doesn't understand that this that this uh incoming stuff is a list so what we need to do is we need to give them the understanding that this is a list and you can do this here it's either an item access or it's a list access or a tree or you give them a hint what it could be that's also possible so i would say list now python knows whatever is in x is a list of numbers yeah it's this list here and i can say max of x sorry it's not that easy well it is very easy but um of course made a mystic maximum is yeah then print maximum and it can also be minimum you can also write here minimum many more is minimum yes right because i need to sorry many more i can have both also i can also remove this and call it call this here minimum and it should give me the number 10. or whatever is to the smallest that doesn't work why minimum is the minimum of x okay oh yeah it worked you just need to update this sometimes so it runs properly i mean it doesn't get simpler really it really doesn't get simpler what it means is i define a name and there can be anything i just write a name what i want this to be what what is the result on how i want to call the result and call it minimum because i want to get the minimum out of the list x so minimum the minimum of x i haven't really got this minimum i did first item for comparison second item comparison i don't understand this tool maybe i haven't because i have never used it really but you can see how simple is how simple that could be and but at the same time very powerful i hope you liked this episode like and subscribe and if you have any questions please let me know see you you
Info
Channel: Philipp Galvan Design
Views: 264
Rating: 4.8095236 out of 5
Keywords: rhino3d, rhino7, python, ironpython, while loops, forloops, for loops, ghpython, scripting for beginners, how to
Id: quV5z2JYqzI
Channel Id: undefined
Length: 33min 24sec (2004 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.