Python 3D Graphics Tutorial 7: Vpython Model of a Thermometer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with episode number seven in our incredible new tutorial series where you're learning how to do 3d graphics and 3d animations in python what i'm going to need you to do is pour yourself a nice strong cup of black coffee that is straight up hot black coffee no sugar no sweeteners none needed that's your goat juice go get you some what i'm also going to need you to do is fire up your most excellent visual studio code and as you're doing that as always i want to give a shout out to you guys who are helping me out over at patreon it is your encouragement and your support that keeps this great content coming you guys that are not helping out yet look down in the description below there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and talk about what we are going to do in today's lesson and what we're going to do is we're going to look at the solution to the homework assignment that i gave you in lesson number six and that was for you to use visual python to create an animated thermometer where you have a visual a 3d visual of a thermometer and that thermometer is going up and down like the temperature of the mercury is going up and down based on your program how many of you guys were successful uh what i'm hoping is i'm hoping a few of you guys would just like do a screen capture of your animation and then put a link to it in the comments down below so we can kind of see what uh what each other are coming up with as far as good cool thermometer graphics and animations but let me jump in and go ahead and let me kind of show you what my solution to this is going to be so i'm going to come over here to visual studio code and i am working in the v python folder i will come over here and create a new program i'm going to call it my therm for my thermometer dot p y the dot p y is kind of important and boom there we have a fresh new python program just waiting to be written and i think i will need to get out of your way here and um ready to get started all right we're going to do this simulation in v python so we need to import feed python and so we'll save from the python from from the python v python import star okay we've got that now what i am going to do is i'm going to impr uh import our old friend from last week who was that that was mr numpy import uh how did we do that uh oh import numpy as np and make sure that it doesn't autofill and change that to numpy like that you just want np that is most annoying okay now that should get it you got to kind of get past that without it changing it to numpy import numpy is mp that means when we want to interact with numpy we interact within p it's like we rename it np makes the topping a little bit faster now we are going to want to make a thermometer so i am going to start by building a glass bulb and my glass bulb is going to have a is going to be a safa ear and that sofa ear is going to have a radius of let's say 1.25 okay and its color is going to be color dot white and then its opacity is going to be equal to let's just start at 0.5 okay so this is going to be kind of a clear glass bulb all right a clear glass bulb anything else we need to do there i don't think so and then let's create my glass cylinder and that is going to be a silent cylinder and that will have a radius of let's have a radius of i'm going to have to think about that a little bit a radius of if this is 1.25 i would want this to be let's make it one now let's make it .75 like that we can come back and look at it later i'm just in my mind trying to picture it that i want the cylinder smaller than the glass sphere okay and again color equal color dot white and then opacity opacity is equal to let's make it .5 again okay then i'm just going to kind of be showing this as we go so we can kind of get the proportions looking good and remember that you need to do a while while true because if you do a graphic and then the program ends then the graphics gonna crash so we got to kind of keep it running so i'll just put a a while loop here that doesn't do anything but just loops and does nothing but it keeps the graphic alive so let's give this a look and see what this is oh and that cylinder we need to put a length on that right so we'll put a length uh is equal to let's make it six so it'd be like a six inch six inch thermometer let's see how this looks oh i need a comma there all right rookie mistake okay can you guys see that all right now all right let's try it okay that's kind of interesting definitely i think this is probably i think that's probably too thick right i think that is too thick compared to the bulb and so instead of i'll make it 0.5 let's see how that looks right the my cylinder was too big around compared to the bulb i thought now it's too small i'm kind of picky about these things i'm going to make it .65 like that i want it to look like a thermometer guys don't hate me because i want to make it look like a thermometer okay i kind of like that i think that looks like a pretty good thermometer right there okay now that's the glass bulb right that is the glass bulb but what are we also going to need we are going to need the mercury so i'm going to put like a mercury uh mercury glo right what would i call that mercury sphere my mercury sphere is going to be a sphere and i want this radius to be equal to 1 because like the mercury's inside the glass i want the mercury to be smaller than the glass i want it to look like a glass bulb with a blob of mercury in it so the radius is going to be 1 and then we are going to have color equal color dot red i'm old school i want red mercury in there and then i think opacity we don't have to set because it will be okay just like that so we're going to have now a red glob inside of our glass bulb so let's look at that ah i like that okay do you see that so you see it actually looks like a glob of mercury inside of the glass bulb right now i need to get that cylinder in there i need to get that mercury column that is going to move up and down so i'm going to need my merck column merc column is going to be a cylinder and its radius is going to be equal to okay let's see now this cylinder up here was 0.65 we want this radius to be a little bit smaller so let's make it 0.5 and then let's give it a length equal to five or equal to six so it'll kind of fill up that that glass cylinder and then co color is going to be color dot red like that okay and uh you guys seeing that okay color is colored.red i hope i didn't cover up anything with my noggin i know you guys hate it when i'm coding behind my head but let's try this now here we go boom look at that all right so let's see if we can spin this around okay i think that looks like a thermometer that looks really good i do think maybe this is a little bit big i would like the red column to be a little bit smaller i've just got this kind of thing in my head of how a thermometer should look and so let's put it at 0.4 and see how that looks on the radius of my mercury column let's kill the old one let's run the new one and have a sip of coffee while we're doing it [Music] it's like the three little bears that's a little bit too small okay i think i've made it a little bit too small i'm going to try to kind of split the difference here let's say 0.445 like that kill it and i promise i won't change it again because i could sit here and i could tweak those parameters all day until i get the perfect one but i actually think that one looks pretty good that to me looks like a thermometer if you would allow me to tweak one more thing i want to take the opacity of the glass tube down a little bit because i want that red to show up a little bit more red on the inside so the opacity of the glass i'm going to take it down to point lid let's see point three all right let's try that on those two kill the old one run the new one kill the old one run the new one okay i like that do you see with the opacity turned down you can see the mercury column a lot better okay now what was the assignment we want that mercury column to go up and down okay we want the mercury now we we've got a good thermometer now but let's animate it okay let's animate it so we'll come over here kill it how do we animate it well while true we're still going to do this because we want it to go up and down and up and down so we'll still do this inside of the while loop but now we're going to say 4 and i'm going to say let's say for my temp in who's your friend mrlinspace lin np.linspace and i i think i'll start right out it kind of like i don't want to start at 0 i want to start right sort of at the outside of the glass bulb and so i will start at 1 and i will go to 6 and i will have 100 okay i will have 100 steps and remember ah this darn thing okay there now remember anytime you're in a loop you need to put a rate so i'm going to put a rate of 100 for v python to know how fast to run this thing and then i'm going to say what are we changing well it's the mercury column that we're changing the length of and then what are we going to change the dot length right and that is this length so it's merc column dot what dot length and then i'm going to set that equal to my temp like that does that look good i think that looks good okay now let's come over here and get this because the next one should be very similar to that and this will just be going back the other way okay so now my temp in np dot lin space and we'll go from 6 to 1 6 2 1 and we'll do 100 steps so let's try and see what that does okay here we go boom look at that it's alive it's alive look at that okay what do i like about it i like that it's very very smooth so those hundred steps are perfectly smooth what i would like to see is i would like to see it go a little bit slower than that that is going a little bit fast for my taste and so come back over here kill it and let's go let's go a rate of 25 here and a rate of 25 and give that a try and see what it does all right let me see oh i like that i like that a lot look at that that really looks like a thermometer that looks a whole lot like a thermometer and it comes down to sort of where you would like it to stop and then it goes back up i really really really like that okay now guys did any of you guys do something this sophisticated where you actually put kind of like a glass bulb with the mercury inside of it or did you do something better than this give me a screen capture and put it on youtube and post the link over there so we can sort of see what each other had done but if you looked at this and really wanted to take it to the next step what would you do would be cool if you had some tick marks in here right it would be cool if you had some tick marks in here for like temperature labels and so let's say that we wanted 10 tick marks all right now what i want to show you is you could kind of come in up here now these would not be animated so they wouldn't need to be in the for loop but you could kind of come in and say like my tick is equal to and then you could make a box and then you could kind of go up and do say 10 or 20 of those tick marks right but that would be like a whole lot of coding so i want to show you a little trick of how you could do something repetitive like that i could say for tick in what do i want to do here i think i'll use i think i'll use linspace so i'll say np dot lens space and i want to start at 1 and i want to go to 6 and i want to have 25 tick marks okay so i'm going to go from 1 to 6 i'm going to do 25 of them okay now the problem is if i did a little more research i could probably figure out how to put a variable name on the the left of an equal sign but what i'm going to do is i'm not going to name my objects i'm just going to put the objects out there the objects are tick marks i'm just going to put them out there when you create an object and don't give it a name you lose the ability to interact with that object but we're just putting them there so we don't have to name them and then this lets me create my tick marks inside of this for loop so what am i going to create i'm going to create a box and that box is going to let's see it's going to have a size it's going to have a size and that size is going to be let's see in the x direction we want it pretty thin so i'm going to say 0.1 okay and then in the y direction let's just make it 0.5 and in the z direction 0.5 and so i'm just imagining a little thin tick mark that is kind of like a little square and then we're going to just put those things uh we're just going to put those things up there and uh now this is where the trick comes in that is going to be the position is also going to be a vector and that vector is going to be well where do we want it in x we want it where at tick right so i'm giving it each time through it is going to put it in a different position all right and then i also need to give it if this is x it's going to be putting them like this and then i'm going to kind of want them raised up in i think i want them coming out in z and so i'll say 0 and 0.5 okay so they're going to be stacked up this way but then they're going to kind of be out towards you a little bit and this i'm just kind of doing off the top of my head so then we're going to have to go in and kind of look at them and figure out how to adjust these things but hopefully we'll at least see them and then make them look good after this and so we have got a size uh that is very bad size is equal to vector that vector and position is equal to vector well what's changing in the for loop what's changing is the position but they should all be the right size so this could be crazy off but hopefully it'll be close enough that we can then decide what we want to do with it so here we go and we can't even see them what did we do wrong there cannot even see them oh and also we killed it so what happened is we got an error in our code and uh guys i apologize i'm rubbing my nose but it is three degrees outside right now and inside the heater is not working so i am freezing in here it is like 40 degrees in here but i'm not gonna put my coat on because this orange shirt it's my trademark i'm not going to make a video on a coat even if i freeze to death but i'm sure what happened here is we probably killed the program with that line of code and so let's see what it didn't like okay let's try running it again we have an error i am sure in the code and it says in line eight vector we misspelled vector i hope you saw that all right let's try it again let's see if we get some tick marks on there this time hey we got some tick marks now look at that those tick marks are not that bad okay those tick marks are not that bad okay i think we got probably two i think they're too thick and let's see if we made them black let's see if you could see them if we made them black okay so we're going to say color equal color dot black all right and then also i thought they were a little too thick so i'm going to make them half that thick 0.05 and then let's see what they look like boom okay now the the black tick mark doesn't really work because you got a black background all right it doesn't really work because you got a black background so let's see if we can make them white let's see color is equal to color dot white and then that .05 i do think on the vector size that's the dimension in x i think that looked good and then we were bringing them out in the z direction which means they should show when i turned it up okay let's try that and see what it looks like boom i like that okay so you see we've got tick marks there on our thermometer and now we could come in i think uh i think they're sticking out a little bit too far when i made it the 0.5 okay and that i think is in the z direction they're coming out too far so let me not bring them out in z at all what if i just what if i made the z smaller like .25 now if i'm not careful so this is remember it's x it's y and z is coming out now if i don't bring them out if i don't make them big enough they're going to be hidden behind and so i got to make sure that they're at least big enough for that okay i think that is perfect you see how we have tick marks they're not sticking out too far out of the globe i mean out of the cylinder and then also it was unfair for me to do this on this one because i haven't shown you but you can put text labels and using that same trick that i just showed you you could actually label them but i do think i have too many tick marks so one last minor adjustment here and then we will move on i think those were too many so we had 20 let's make uh we had the ticks we made 25 let's make 15. let's see how that looks all right boom i like that i think that is a dandy thermometer what do you think i think we've got the tick marks we've got it animated we've got it moving up and down and then what you could imagine is is that we could put the labels and in a future lesson i'll show you how to do labels on things and we could use that same for loop trick and as you make the tick mark put the number beside it and then that way we would have the full the full thermometer but i think that is pretty darn cool okay guys i think it is time for me to give you a homework assignment for next week and the homework assignment for next week is i want two thermometers side by side okay so i want like thermometer one and i want thermometer two okay and i want thermometer one animated and i want thermometer two animated but i want thermometer one let's see thermometer one going twice as fast as thermometer two okay they're both moving and and you cannot do this you can't do this right they've got to both be moving at the same time but thermometer two going twice as fast as thermometer one okay two thermometers both of my anime because this is kind of important because if we're going to animate things we might have like a pressure sensor and a temperature sensor and you've got to animate them independently and so what i want you to do for next time is two thermometers both of them animated side by side one of them moving twice as fast as the other one all at the same time okay so that is going to be your assignment for next week and then what i will do i will come back next week and i will show you my solution guys i hope you're having as much fun taking these lessons as i'm having making them and i know that we're all excited to get in there and connect arduino up to our visual python but we've got to build some foundation we've got to build some fundamentals in the 3d modeling and in the 3d animations before we jump in and start sending live data in from the arduino and that's kind of what we're trying to do here so next week you're going to have two thermometers they're going to be running independently and then i will come back next week and i will show you my solution okay guys i i hope if you enjoy these videos you'll give me a thumbs up don't forget to subscribe to the channel also when you subscribe make sure you ring that bell so that you will get notifications when my future lessons come out and think about sharing this on social media because we need more people coding and fewer people sitting around watching silly cat videos this is paul mccorder from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 3,680
Rating: undefined out of 5
Keywords: Visual Python, Vpython, Modelling
Id: 1FByIXbXjnM
Channel Id: undefined
Length: 26min 59sec (1619 seconds)
Published: Wed Aug 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.