Python 3D Graphics Tutorial 6: Animating 3D Objects by Changing Dimensions in Visual Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with episode number six in our incredible new tutorial series where you're going to learn how to do 3d animations and 3d graphics 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 black coffee no sugar no sweeteners none needed that's your goat juice go get you some i am also going to need you to fire up your most excellent visual studio code and as you are bringing up visual studio code as always i want to give a shout out to you guys who are helping me out over at patreon it is your help and your encouragement and your support that keeps this great content coming you guys that are not helping out yet look down in the description 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 learn today and what i'll do is i'll kind of give you a little bit of a hint of the direction that we're going with this series of lessons what i'd really like to do is get to the point that we are connecting an arduino up to our visual python and the arduino might be hooked up to sensors you know pressure sensor temperature sensor distance sensor different things like that but rather than just taking data i want it to pass that data to visual python and then i want us to have animated 3d graphics that will change in response to what's happening with the arduino so if we're measuring temperature imagine that in v python we built a visual graphic of a thermometer and then that thermometer changed as the temperature on the arduino was changing pretty cool yeah so that's going to be a lot of fun but to do that we've got to learn a little bit about a little bit more about how to animate a 3d graphic and we learned a little bit about that in the last few lessons one way we can bring animation to a graphic is to change position right we had that little box and then we had the marble in it the marble was bouncing in the box that came alive that was animated and the animation was based on moving the object another way that we can animate it there's several other ways that we can animate it but one of the ways that it's very effective if we're using it in conjunction with arduino is to animate something by changing its size and so that's what we're going to play around with today one of the things we'll play around with today is showing you how to bring animation to your visual python model by changing uh dimensions so enough of this talking let me get out of your way and let's come over here and let's uh go ahead and we are working in the v python folder and let's create a new file and i'm going to call it my piston dot p y the dot p y is kind of important we click enter and then boom we have a fresh new python program just waiting to be written now we are using v python so we are going to want to say from the the python be python import everything which is the star and today i'm going to be introducing you to a really cool additional library which is called numpy and numpy just does all kinds of incredible things and it does them real easily someday i should have a whole tutorial series on numpy but today i'm just going to be showing you one of the things because it's what we need for making the animations run better so we're going to say fro or we're going to say import numpy as np now you notice this import has a little different syntax than the one we did for v python well the thing is we're importing the entirety of numpy but i want to import it as np that way if i want to interact with the library i can say np.this or np dot that and if i just said import numpy then i would have to say numpy.this and numpy.that so if you said import numpy as cat then you would do cat dot this or cat dot that i hope that makes sense so now we have our two libraries and then if we're going to animate something we're going to have to have an object to animate so i think i will make an object called uh my piston although it's really bad to create things the same as your file name so let's see i will call it uh i will call it pissdone1 okay piston1 and that is equal to cylinder and then we're going to put some parameters in here let's say i'm going to have a radius equal to 1. let's say i'm going to have a length equal to 3 and then i'm gonna have a color equal color dot red i think and then uh let's see i think also what i would like to do is i'd like to introduce you to another parameter that i haven't talked about before but we're just kind of learning as we're going here another parameter you can do is opacity the opacity is how solid or how transparent it is so an opacity of one means it's solid an opacity of zero means it's invisible an opacity of 0.5 would be it's kind of halfway there and halfway transparent so let's try that that opacity is equal to let's just set it to 0.5 and with that we should sort of be able to see through this piston a little bit and then remember that we need to say while true because when we create an object if we don't keep the python program running the whole thing will die so we'll say while true and then do nothing but pass and so if i am right here and i hope i haven't already made a mistake this early in the video if i am thinking correctly what i think is i think that this should just create a nice cylinder so let's try this let's run it boom okay we have a nice cylinder and you see how you can kind of see that bottom face of the cylinder and so you actually are sort of looking through it a little bit let's play with that parameter a little bit just because it's new i don't want to get too sidetracked here but let's put like an opacity of 0.25 kill the old one start the new one and boom you can really see that that one is more transparent now unfortunately here our background is black and so if you're making something transparent against a black background it sort of starts getting really dark this would look better if we had a white background but i do not want to get sidetracked at this point with paint playing with the background but let's just set it back 2.5 but you can see that we can set opacity we've set color we've set length and we have set radius all right so let's see if we could animate this thing so what we're going to do here is we are going to put a for loop so we're going to say 4 and then we are going to say like my length link is equal to for my length in range range and let's just go from one to six in steps of point of one okay now remember with v python anytime you're changing a parameter inside of a loop you have to you have to put a rate in there or you can get very uh very unusual response okay you can get an unusual response you always have to put a rate in there so i'm just going to put a rate of let's say five that's pretty slow and so it's going to start at one it's going to go to six in steps of one and now i'm going to bring it back down in size so for my length in range and what do we want to do this time we want to go from 6 down to 1 and we want to go in steps of minus 1 like that okay also remember we have this crazy nonsense it's not going to go actually to 6 it's going to stop at 5 because that's what the range function does but that should be okay rate of 5. now we've got to do something in these four loops well what do i want to change i want to change the piston length so i'm going to say piston 1 dot what i want to change its length and then what do i want to change it to well it's my length is the one that is stepping through the range and so i want to change it to my length good the and then i think we will just snag this here and then come down and put it here and it should go from one two three four five four three two one like that it should do that so let's give this a try and see if this thing actually works okay boom look at that look at that okay do you see how we are animating this thing boom okay so it goes one to five to one to five all right so that is what our goal is is to animate this but now if you were just looking at this and you were going to offer critique or suggestion what would you really like okay you would like it to go smoother it's choppy okay so you would like it to go smoother so let's come back over here and see what we can do we'll kill that what your inclination would be is say okay you don't want to go in steps of one let's say you make steps at point one but you're going to see that there's a problem in doing that and the problem is we are going to get an error down here and let's see if i can make that a little bigger you can see that the error is saying that uh this range 1 to 6 in steps of 0.1 float object cannot be interpreted as an integer and so you can see that this range function wants integers it doesn't want floats and right here what we have is we put in a float so if we're going to do this we have to go back and just make it 1 and 1 and then just live with the choppiness in our animation but are we the kind of guys that are gonna sit here and live with choppy animations no not on my watch we're gonna make it smooth and we're gonna make it smooth by using numpy because numpy has a feature sort of like range but the feature in numpy does two things for you number one it doesn't have this nonsense of you know on range if you want to if you tell it to go to six it stops at five right uh with with the the numpy equivalent of this you tell it where to start it starts there you tell it where to stop it stops there not one before there and then you tell it how many steps you want between those two and then it figures out the step size and so i really love that and that is actually that is actually will come here and instead of range let me just go all the way across here we are going to do np so that's calling the numpy library and then np dot what what it is lin space that's like linear space lin space and you tell it where you want to start you want to start at one where you want to stop you want to stop at six it will go all the way to six exactly it doesn't stop before there now the other neat thing is how many steps do you want it to make well let's say make 10 steps so start at 1 go to 6 make 10 steps okay and then this is still my length so i think the rest of this stuff should still be good let me get that out of the way and then here we're going to do the same thing so we'll just do this again okay so we'll go np dot what's the function lin space and we want to start at 6 and we want to go to 1 and we want to go in steps of 10. and we'll go at a rate of 5. all right are we ready to try this here we go okay and it seems to be dead why is it dead why is that thing dead what did we do wrong here let's go back and find our error oh it says that you know what i think i might have not killed the last one so let's try it by killing it and then coming back sometimes if you don't kill it you get unexpected results and in fact it is still dead it is dead what did i do wrong what did i do wrong my length lin space linspace guys i hope you saw this it auto-corrected sometimes this auto correction really kills you i said import numpy as np and you see it changes it to numpy np and then get off of there and make sure it is not numpy so now i interact with that as np okay np now this should work i hope boom look at that look at that okay do you see how that's a little smoother you see how that's a little smoother okay what is it that you would suggest though you would suggest that you want it even more smooth right you want it even smoother because i can still see those discrete steps so we're going to come back over here and we are going to come over here all right and let me make my code view a little bit bigger for you and so what we're going to do is instead of 10 we're going to go 100 so we're going to take 100 steps and so now it should be much smoother okay so let's kill that come back over here yeah that is really getting much smoother can you see that much smoother okay but you see how it's still a little choppy all right so what would you do tell me what you would do let's change it to a thousand so let's go in a thousand steps and now that should be perfectly smooth it should be smooth all right let's look here let's run it with a thousand steps oh man look how smooth that is okay look how smooth that is all right do you see how smooth that is but now you guys that are always complaining what are you going to complain well i put so many steps and now it is too slow well the answer my friend is not to go in and make it back to a hundred the answer is to make it go what faster so let's see if we could go a hundred instead of five and then it should step through those smooth transitions much quicker so let's see what that does we're gonna kill this and then we're going to come here run it okay that is starting to look good right that is starting to look good do you see how smooth that is that would be a beautiful animation that would be a beautiful animation now i do think for me i'd like it to go a little bit faster still and so we're going to come back over here we're going to kill it and then we are going to go to 50 and then we are going to go 250 and then let's see what happens okay let's get it where we can see it okay i like that i like it a lot i like that a lot so you see we have a nice animation there really smooth everything about that i like i think that's like a nice speed to be going and it's a nice smoothness but kind of the point here is is that you can animate something by changing a parameter and the parameter that we're changing here is we are changing the parameter of length in these for loops we've learned how to use numpy line space to make things nice and let me just show you with this line space how easy it is all right so let's say that instead of changing length we want to change opacity okay we're going to change opacity so for that we would say uh let's say for my opacity for my opacity in linspace and for opacity we would want to go from one to zero okay and then we would change piston one we wouldn't change length we would change opacity right and this dot opacity that is the parameter up here right so piston one is the object what parameter are we changing opacity what are we changing it to we are going to change it to my opacity which is kind of like our for loop index so we're going to change it to that so you've got to understand the difference between dot opacity is the parameter we're changing and what we're changing it to is my opacity which is this variable that we're stepping through uh stepping through our numbers with so i think that should work let's go ahead and snag this line and then let's come down here and put it here okay and then uh i think i'm gonna snag this line here too because it's very close to what we want and i'm gonna put it here and then we need to go if we go from one to zero then we're gonna need to go from zero back to one and then we're going to start out at a length of three this should look pretty cool if i'm not if i'm thinking about it right make sure we kill that old program here we go okay there it goes look at that goes away comes back boom look at that full solid and then it fades away and then it comes back so you start are you starting to see all the things that you can change you can change where something is you can change how big something is and then you can change its opacity okay let's just do maybe one more example here just to kind of get comfortable with things and so what if i say instead of a piston one what if i say my spheres so the ear and then that would need to be instead of a cylinder that would need to be a sofa ear and then we're gonna have like a radius of one and a sphere doesn't have a length okay should be radius one color is color dot red opacity is still 0.5 that looks pretty good and now what we're going to do is we're going to go back and we are going to make my radius radius equal lens space let's go from a radius of 0.1 to a radius of one in its steps of a thousand and then from a radius of one back to a radius of 0.1 okay and guys when you're changing size again i can't remember if i told you this before don't set the size to zero because if you set if you take a 3d object and you set the size to zero it's like you disappear it and then sometimes it won't come back so don't ever size something to zero because it might be that you just evaporate it when you do that okay and then this is going to again be my radius and then we're going to say piston one not opacity but ray d us why is that because it's it's this radius right it is the parameter radius that we're changing piston one dot what dot radius and then what are we going to set it to we are going to set it to my radius which is the index that we're stepping through hope that makes sense and then here we will do the same thing radius and then we will make it my radius like that so let's try that kill the old one bring in the new one here we go and it is dead it is dead it is a dead graphic it's doing nothing what did i do wrong here oh piston yeah yeah yeah yeah okay remember when i made my sphere right that is now my object so i have to make my object my sphere my object my sphere like that now let's try it and it is still a dead graphic oh no there it goes okay i just didn't wait long enough look at that boom look at that okay i want you to know though that's not coming towards you and moving away it's just getting bigger and smaller and so you've got to not visually interpret that as the ball's coming towards you it's just the ball is getting bigger and the ball is getting smaller and i think if i back off a little bit you can see it a little better as just changing size all right guys that is pretty cool so what did we learn today we learned that if you really want to be stepping through parameters in a way that it's easy for you to control who's your friend mr numpy dot linspace is your friend okay mr np dot linspace and guys you know almost any time that i do a for loop i go ahead and use lens space because i don't even have to think this is where i want to start this is where i want to stop and this is how many points i want and so i don't have to think through kind of some of the nonsense that goes on when you use that range command and if you want to go from one to ten in steps of point one you would say start at one stop at ten and give me ten steps you see so it's this lens space will work for almost anything then what we saw is anything that you set a parameter for when you create the object you can change it later on by doing whatever your object is whatever your my object dot then what parameter you want to change my object.radius myobject.color my.object.opacity whatever parameter you can change in an animated way so i've kind of shown you three different examples you can move position showed you that a few weeks ago in the last couple of weeks i mean then i've showed you today how you can change the length you can change the radius we didn't do color but you could probably do the color and then you could step through the opacity as well and so this is just a way to bring your animations to live now imagine in the future when we hook up to the arduino we might hook up to a temperature sensor and if we hooked up to a temperature sensor you would want a nice graphic a nice animated graphic that looked like a thermometer so that is what your homework assignment is for next week is i want you to make an animated uh an animated visual that looks like a thermometer okay so you're going to have a thermometer and then you want the mercury going up and coming down and going up and coming down and then what you can do is try to do like a screen capture and then upload your video to youtube and then put a comment down below linking over to your animation and let's see who comes up with the best thermometer graphic now some of you ocd people are just probably going to just go crazy with the level of detail that you're going to put in okay but what i want to see is i want to see how you guys are going to do your animations and see if anybody comes up with just like a really cool thermometer but that's your homework for next week and then next week i'll do a quick solution and then talk about some new stuff okay hope you guys are having as much fun with visual python as i am i really enjoy this i have a lot of fun with it i can't wait till we get that arduino hooked up with it but we got to kind of build a foundation of being able to build good graphics before we bring in the arduino okay guys uh if you enjoyed this video be sure to give it a thumbs up if you haven't subscribed to the channel already make sure that you subscribe when you do ring that bell so you'll get notifications when my new videos come out and then think about sharing this with other people we need more people in the world that are coding and fewer people that are sitting around watching silly cat videos this is paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 3,935
Rating: undefined out of 5
Keywords:
Id: qXmyVcCMhr0
Channel Id: undefined
Length: 26min 3sec (1563 seconds)
Published: Wed Aug 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.