Python 3D Graphics Tutorial 1: Installation and Demonstration of Vpython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McWhorter from  toptechboy.com and we're here today with   lesson number one in our incredible new tutorial  series where you're going to learn 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 black coffee no sugar no  sweetener none needed and what i'm going to   need you to do is get ready to learn some cool new  stuff now in this series of lessons i am going to   assume that you have basic familiarity with python  if you've not seen python yet you need to hop over   to my other tutorial series where i sort of teach  some of the basics of python but in this series   of lessons you don't need to be a python expert  but you do need to kind of be familiar with the   basic concepts of python the other thing is in  this series of tutorials i will be using the most   excellent visual studio code as my development  environment and it's going to make your life a lot   easier and my life a lot easier if we are using  the same development environment if you don't have   visual studio code it's a great program it's a  free program you can go back to my python lessons   in my python tutorials on the other playlist  lesson number five shows you how to install   visual studio code and how to get it and how to  get it configured the way i have mine configured   so it'll just be a lot easier for you to follow  along with me and so what i will be doing   is i will be switching over to my most excellent  visual studio code and if you remember in that   earlier series of lessons we were working in a  folder called python files well what i really   want to do is i want to make a new folder for this  series of lessons so all those old programs are   in that python files folder and we're going  to have a new folder that we are going to call   the python because we will be using visual python  or v python to do our 3d graphics and our 3d   animations so the way we would do that is we would  come over to file and we would come to open folder   now you say i haven't made the folder yet don't  worry just follow along trust the plan as they   say trust the plan you're going to click on open  folder and when you click on open folder it comes   over and you need to kind of navigate to where  you want this folder to be i've got mine this   was what i was doing before the python files and  this is in my top tech boy com folder you can put   this wherever you want but just sort of remember  where you put it but once you get this open then   i'm going to say right mouse click and i'm going  to say new i'm going to make a new folder called v   python little v uppercase p v python and then all  of my programs i'm going to put in this folder i   will click enter and now that we've created it  i want to select it and then say select folder   there and then boom in visual studio code i have  a new working folder called v python all right now   that is where we're going to be doing our work  but before we can really do any work we need to   install v python v python is a library that runs  under python and it allows us to do all the cool   3d simulations and graphics and animations so what  i need to do is i need to come over here to view   and then under view i'm going to come down to  terminal and that will open up a terminal for   me down here and so i just need to click down  here somewhere and then i'm going to click enter   and you can see i will move further out of your  way you can see that i have a command line here   this would be like opening up in windows the  cmd folder or the cmd terminal and we can do   some operations here what we need to do is we need  to install the python and we do that using pip   now if you're on one of the later versions of  python 3 you should already have pip installer   i should say that i am on i can type in python  here and i am on python 3.9.1 you need to at   least be on one of the later versions of python  3. you would not want to be on python 2. those   python 2 won't work with this series of  lessons okay so yes i am on python 3.9   and python 3.9 comes with pip and so you should  just be able to do a pip install with that so   i will come here and i will type not pup  that's a whole different thing pip install   the python like that pip install v-p-y-t-h-o-n  like that and then i click enter and it is   collecting things and it is over there doing  all of this good stuff this is what you want   to see you want to see it out downloading  packages and installing packages because   when you do the pip install v python what it also  installs are all the other packages that you will   need in order to do v python now this might take  three or four minutes to do and so i'm not going   to pause the video i'll just sit here and kind of  chat with you as this is doing this yours might be   going faster or slower i have a very fast internet  connection so it downloads the stuff very quickly   if you have a slower connection it might take  you a little bit longer but kind of telling you   what we're going to do in this series of lessons  we're going to start by learning how to just make   3d graphics like create 3d objects using python  and then we're going to animate those 3d objects   where we can make things move around and  do different things and then after that   what we're going to learn is we're going to learn  to connect v python python and v python to arduino   and then we can be out collecting data and then we  can create animations on v python that represents   what's happening in the real world so let's say  that we have arduino and arduino is measuring   temperature we can send that temperature data over  to v python and we could have like a graphic of a   3d thermometer and we could have that thermometer  showing graphically as the temperature goes up and   down like a little animation or simulation of a  thermometer that is actually responding to what   is happening in the real world okay it looks like  my v python has installed and so if yours hasn't   finished yet go ahead and pause the video until  your installation finishes and then rejoin me   okay so i'm going to assume that you have it all  installed now and i am going to kill this and then   i am going to kill this and so we are ready to  start our first program so i come over here and   i point at v python and then i click the little  plus and then that allows me to add a new python   program inside of v python and i am going to call  it v python intro dot p y the dot p y is kind of   important and also it is poor programming practice  to put spaces and file names so never put a space   in the file name and then i'm going to click enter  and boom i have a fresh new i have a fresh new uh   what is this linter pilot is not installed  do not show again okay so i got rid of that   all right so i have a fresh new python  program just waiting to be written   written and so the first thing i want to do  is i want to just see if the python program   was installed successfully so i'm the python  library so i'm going to say import the python okay   and then i'm just going to run that and see if i  get any errors and we will be looking down here   and boom what you see is it ran the program  and then it came back to the command prompt   and we didn't get any errors and so that would  indicate that we had installed v python correctly   now we are going to try to generate our very first  animation not our very first animation our very   first 3d graphic and so we are going to create a  ball and that ball is going to be equal to sphere   and then just open and close and since we are  doing with it this with the v python library we   need to say v python dot sphere so this will go  to our v python library it will find the method   sphere and it should create a sphere now what i  will say is is that after this line of code if you   just let the program end you create this animation  of the sphere and then you let the program in what   you're going to get is you're going to get kind  of an ugly error because the program stopped with   that with the uh with the animation or the graphic  just hanging out there that's not good so what i   need to do is create a while true and when is  true true true is always true and then what   do i want to do i want to just pass okay so it's  going to import the library it's going to create   this sphere which it's going to call ball and then  it's going to just sit and loop and do nothing and   that just keeps the program alive so that you are  able to keep the visualization visualization up   there now this will fire it up and it will open  it in a browser so whatever your default browser   is it's going to fire up a browser window and then  you're going to see this ball the sphere that you   created in that in that browser window now the  first time you do this you might get some sort   of permissions question from windows windows  might ask do you really want to let vpython   open a browser window or something like that  you just say yes let it do it and so we are   going to give this a try so i'm going to try  to run this i'm going to click this for run   and boom we can come over here to  this other window and what you can   see is is that we created a most excellent  sphere so in about the first five minutes   of this lesson of this tutorial you have already  created your own first visualization so we're   going to come back over here and let's see if  we can make things a little more interesting   all right as we do and do and do and go and  go and go i don't want to every time i call   a method have to type in v python so i'm going  to show you a little bit better way to import v   python where we won't have to keep saying v python  every time we want to use it so we're going to say   from the python we are going to m port star which  means from b v python import everything and by   doing things this way then we're not going to have  to say v python.sphere we can just say sphere and   it should do it so i'm going to come back over  here on my other window i'm going to kill that   browser that had the sphere in it and i'm going  to run it again and see if i can regenerate that   and boom look at that did you guys get that okay  so this is an easier way to create a sphere and   so we are really on a roll here okay so that  was kind of really easy that was really easy but let me show you something else that you can do   between the open and close parentheses you can  start putting parameters what is a parameter   well what if i wanted to set the color so  i could say color is equal to color dot red   now this can't do like if you said color dot  fuchsia or something like that it's not going to   go that to that but i bet you could do like red  green blue orange cyan magenta yellow i bet you   could probably do those and now i am going to come  in and run this thing and let's see what happens   and oh that reminds me the program was still  running from before and we tried to run it again   that's not good you've got to come over here and  with this little trash can over my shoulder here   that little trash can kill that trash can and that  will kill the program before you try to run it   again and i'm gonna probably forget that more than  one time and boom we've got ourselves a red sphere   guys look at this we're doing 3d graphics here  just lickety-split so this is kind of fun huh   so that is creating a red sphere now i want to  show you something else we could do we could   create that and then what we could do is let's  pause and so if i'm going to pause i need to i   i need to get over here where you can see me if  i am going to pause i'm going to need to from   time import star and so i'm going to import  the time library and then what i could do   is i could just do sleep and i'm going to sleep  for five seconds and now this is the thing that   i want to show you can do you can change the  characteristics of that ball after you created   it what is it called the object is created ball  and what i could do is i could say ball dot   color is equal to color dot blue like that so  ball dot color is equal to color dot blue and   that should change the color and so let's see if  that'll work i will click there and i will come   over here and let's see if that will create and  you know what we forgot to do again we forgot to   we forgot to kill the program and i am probably  going to do that more than once and so we've   always got to remember to kill that last version  of the program before we run the next version so   we'll come here and then we'll click over here  and then boom we have a red ball and that should   stay there for about five seconds and it turned  blue okay and now it's just going to stay blue   because it is down here in this loop but what we  could do is we could say let's sleep another five   and then let's call it ball dot color is  equal to color dot hmm let's do let's do   green like that and see if that will run show you  that okay so what we did was we did color ball   colors color dot blue we waited five and now we're  going to do ball dot color is color dot green and   then it should stay green because it'll just be  down in here let's remember to kill this program   this time and then let's come over here and run it  and then we will switch over to our browser window   and we get a red ball four three two one we get  a blue ball five four three two one and we get a   green ball all right guys so you see we're  already kind of starting to do a little bit of an   animation there alright so we have created a ball  and we can change its color so let's play around   a little bit more with it the world is made up of  things i'm going to go ahead and kill that program   okay with the little trash can we kill the program  the world is made up of things besides spheres   and what if we said a uh what what if we said  a box okay and what my box is going to be is   uh i don't want it box is what the command is and  so i will call it my box is going to be equal to   box and let's make it a let's make it a color  let's see if we can do a yellow okay and then   since we don't have ball right we don't have ball  anymore i need to get rid of all this stuff we   are just seeing if we can make a box and so i  will run this thing and then switch over here   and then boom we got a box okay now it looks  like just a square right it just looks like a   square but if you will point over here add it and  now i need you to right mouse click right mouse   click and then drag it around you can see that  you have a 3d cube and you can change the zoom   of it also if you use the mouse wheel your mouse  wheel you can make it smaller or bigger right i'm   just using the mouse wheel if you have a mouse  wheel you can make it smaller or bigger and then   right mouse click you can change the view of it  so that is pretty slick okay so we have learned to   make a sphere we have learned to make a box well  let's see if we can play around with that block   box a little bit what if we wanted to set the  size well i could say let's say i want to make   something that looked like a ruler okay i am going  to come back over here man i've got to make sure   that you guys are looking at what i am looking at  because you know i i'm having to switch between   two screens i got to make sure that i'm not  talking from a screen that you're not able to see so let's see if we can give it a size so i am  going to say length is going to be equal to   12 okay and then i am going to say width is let's  see make sure that you can see this width is going   to be equal to 1. a ruler is about 1 and then  height is going to be equal to let's say about   point two let's see how that looks now let's  see if we make something that looks like a ruler   do you think it knows brown brown would look a  little bit more like a ruler i don't know if it   knows brown or not but we'll see if we can do that  and always remember to kill the program again do   you see this little trash can over my shoulder  here make sure you kill the program in the trash   can and then let's see if we can run this thing  again and then we will come back over to this view   and it doesn't like something here oh look what  i did look what i did did you guys see that   i said height of 0.2 where what i should have  said was height equal 0.2 i don't know how i   did that and then make sure that you close your  box so i open the box and then i close the box   parentheses and we are going to run this thing now  we are going to make sure the other one is dead   and then we are going to run it and we are  going to get another ah we get another error   it doesn't know what brown is that is pretty lame  that it doesn't know what brown is i would think   brown would be a pretty common color but let's  try uh let's try magenta cyan magenta yellow it   should know that one i would think so we will come  here and then we come over here and boom there is   our ruler now what i want you to kind of see here  is we've got to start kind of orienting ourselves   to the three-dimensional world we've got to start  orienting ourselves to the three-dimensional world   and so what i want you to see is that height  parameter that we set this height parameter   is kind of like z it's kind of like y it is up  and down the height parameter is kind of y it is   up and down and the length parameter is kind of  like x so you sort of got x is length and then y   is y is height and then z is width so z is coming  out of the page and so you've got to kind of think   that you know how you get the parameters that  you want and remember x goes like this y goes   like this and then z is out of the page and  that's kind of the right hand rule if you go from   x to y your thumb points in z so you go from the  x axis to the y axis and your thumb is pointing   in z and so i know i'm kind of repeating myself  but as we get really into v python you've got to   understand kind of like which direction is which  and so let's come back over here and let's look at   our animation and remember y is going this way and  so y was the height that's very thin and then z is   coming out of the page and z is the width and then  the length is along x now i can come over here and   i can right mouse click on this and i can spin  it around and yeah look at that you see it really   does look like a ruler that we created it really  does look like a ruler and so that is pretty neat   i'm still a little miffed that i can't get brown  all right little myth that i can't get brown okay let's go back to our code view let's see  something else that we could create well i could   create my tube and my tube would be a what it  would be a cylinder and let's make color dot   let's see if we can make an orange let's see if  it knows orange and guys you can really make any   color you want and in a later video i'll show you  how to just do any color you want by mixing colors   but right now we're just doing the standard colors  that they're offering let's leave this length of   12 well you wouldn't have a width and a height but  you would have a radius and that radius let's make   the radius equal to 1. and so this should generate  a tube so we are going to kill the old program   and then we're going to run the new program and  then we're going to hop over here and boom we have   a tube and i am going to kind of spin this around  and you can see that part of the problem here is   is that we have run the tube off the edge of the  page and so we said that that tube was 12 and so   why was the ruler 12 and it fit and the tube  is 12 and it doesn't well because the ruler   kind of has the center of the rectangle at the  origin of the scene and so you sort of had six   inches and six inches and so it fit and this uh  in this tube it starts the end at the origin and   so it went off the end of the page now what i will  show you in later videos as to how to set the size   of that window and do things like that but we're  not going to worry about that today we are going   to fix that i think if i just go to six probably  the whole window is from minus six to plus six   and so if i just set this if i just set this over  here if i just set this over here to a length of   six then that should be pretty much on the page  but it's still going to kind of be the edge over   and so the edge is going to be on the center  and so what i will do here is i will kill that   old program with the little trash can and  then we are going to run this and then we   are going to come back over here and then boom  this should be kind of like right to the edge   and it is not what did i not do right i did  not do something right here let's come back   and see i set length is equal to six and it still  seemed to be an enormous one and so let's see my   tube is a cylinder let's make the length one  and that certainly should stay on the screen   and so we're going to learn kind of some of the  quirky things about this so we it looks like we   have already killed the program and now length is  one so we will come over here and we will run this   and we will come back over here and there it is  okay so for one and so this is a radius of one   which would be a diameter of two and then that is  one long and so that is pretty good okay so you   can see i can kind of create whatever i want but  when i'm creating it i can run it off the edge of   the screen and so it looks like maybe that this  is if this is a length of one maybe the whole   window goes from plus three to minus three here so  we're going to really need to learn how to kind of   control our view and so i will come back over  here and we will kill that and then let's say   that i make it i'm going to try something what if  i made it 12 again instead of a radius what if we   set a length and width will the program crash  will we get something interesting so i'll say   the let's say the width is equal to let's say 1  and let's say the height is equal to 0.5 now that   would be kind of odd because a cylinder is round  and you would really specify it with a radius   so what happens if we try to set a width and a  height of a cylinder does it just crash or do we   get an interesting result so we'll try to run that  come back over here and boom look at that it did   make it and the funny thing is this time it didn't  run it off the edge and so we'll spin it over here   and we'll try to back up where you can see it look  at that so you can actually set you can create an   object you can call it a cylinder but then if you  give it a width and a height you can kind of make   that would be considered an ellipse so it's  like an extruded ellipse that we can make   by setting the width and height of a cylinder  which i think is kind of neat so we'll come back   and we will kill that program so look at all the  stuff that we have learned to do already we've   learned to make a ball we've learned to make  a box and we have learned to make a cylinder and so uh and we've also learned that length is  in the x direction okay and then i believe that we   had height was in the y direction and then width  is coming out of the page with this kind of in   the z direction so let's come in and let's go back  and let's play around again with making a box only   i'm going to call it a wall all right and the wall  is going to be a box okay so the wall is going to   be a box and my color color is going to be equal  to color dot i wonder if it knows gray did i even let's see if it'll show me the color so i say  color dot i'll look at this i've got different   colors black blue cyan let's go it would know  yellow it would know orange it would know green as you can see you can kind of see and you see it  knows gray and so let's say if i can make the wall   gray color is equal to color dot gray and then i'm  going to kind of instead of making a box i'm going   to kind of make a wall and so what i want that  wall to be is i want that wall to be a length   is equal to 10 and so length is along x  and then what i want is i want the height   to be equal to and i believe height was in y right  so height is going to be like this i'm going to   want it to be 0.1 and then on the width which is  coming out of the page i want width to be equal   to 10. and so this is going to be kind of a wall  and it should be down flat but it should be a 10   by 10 wall that's kind of thin and so we are going  to come up here and we are going to run this thing and then we'll come over here and let's see if  we get anything and i don't seem to be getting   anything what does it not like oh okay i got  an error and so let's come back over here and   see what it didn't like i have length is ten  height this point one with this ten and uh color must be a vector okay it didn't like gray i  thought that it would like gray but gray probably   wants a parameter and so let's just say white  let's see if it knows white and now i will come   back and i will make sure this is dead and then  i will run it and then i will come over here   and then there we have it okay so this is going  to be so i'm going to back off and then i'm going   to tilt it and look at that i made a big plate  that is pretty cool i think i made a big plate   and it sort of turned out gray even though i  called it white it sort of turned out great but   that that worked out pretty well i would say that  that worked out pretty well all right so we are   going to learn something new now i'm going to kill  over my shoulder kill the program so i made a wall   okay now i want to show you that that wall that  i made you can move it around okay and you've   got to remember that x is in this direction y is  in this direction and then z is in this direction   and if i go from x to y my thumb points in z  right so you got to remember that right hand   rule go from x to y and your thumb points in z  and so what i want to do is i want to i want to   think about changing the position or setting the  position and i'll just put it here so you can   watch me type it in you can put these in any order  but i'm going to say i want to give it a position   and that position is going to be equal to the  position is going to be equal to and now what i   have to do is i have to set it as a vector because  position needs three numbers you know and you're   doing relative to the center of your box so it  would be the center of the wall so that would be   the center in x the center in y in the center and  z relative to the center of your scene so what i   need to do is i need to put a comma between  my vector and color and now where do i want   this well i want to take this wall and i want to  move it down five okay i want to move it down five   and so that would be in the y direction so where  do i want to move it in the x direction nowhere so   remember x y z so i don't want to move it anywhere  in the x direction in the y direction i want to go   minus 5 and then in the z direction i don't  want to move it anywhere so i am going to put a   0 there okay i am going to put a 0 there  and so what i'm going to do now is run this   and then we will come back over here and see what  happens and boom do you see that it took this and   it lowered it down to -5 okay so not only not only  can i control the size but i moved it down and the   way that i moved it down was i'm very carefully  think of thinking about where i am in 3d space   and that's really the key guys is you've got  to start getting yourself oriented in 3d space   the center of your view is the point 0 0 0 and  then you're going to move out in x or back in   x or you're going to be moving up and down in y in  this case we move down in y and then you've got to   remember z is out of the page and kind of towards  you out of the page is positive z right hand rule   and further into the page is negative z does that  make sense so let me show you something else let's   call that wall one okay we're going to call that  wall one well what if i make a wall two wall two   is going to be equal to box well where do i want  to put this well this is kind of like the floor in   fact that's what i'm going to do i'm going to call  this the i'm going to call this object the floor   okay and then i am going to call this object  i'm going to call this option object the ceiling   and so the ceiling should be where  it should be at position is equal to   vector now the ceiling should be up so where  would that be that's in the y direction but what   would it be it would be 0 comma plus 5 and then  0 because i don't want to move it left and right   and i don't want to move it in and out of the page  and so the floor is going to be down here and the   ceiling is going to be up here all right let's  go ahead and give it a color so i'm going to say   color is equal to color dot white again and then  what am i going to do i am going to give it a   length is equal to 10 and then a height h  height boy height is a hard word to spell   isn't it it's equal to 0.1 and then a width is  equal to 10 and i think you could see all that   all right so what do we expect this to do what  we expect this to do is create a floor down here   and create a ceiling up here and then you know we  should sort of have a pretty good start on a room we should have a pretty good start on a room  so let's go ahead and run this and i will run   it i will move back over to the view here and  you know what we forgot to do yes we forgot   to kill the other program and so we need to kill  that other program and then we will try it again   and here we go coming up boom look at that you  see that we're building ourselves a room uh-huh   okay and then again we can scoot out by scrolling  or scoot in right mouse click and we can move it   around and you can see we are creating a 3d room  and so that is pretty cool that is just pretty   darn cool all right guys this has kind of been a  pretty nice little introduction and i want you to   give you guys a chance to play around with this on  your own and so this is your homework assignment   your homework assignment is to create a 3d room  so what would you need you would need the floor   you would need the ceiling and now i need you  to make a left wall and then i need you to make   a right wall and so you're going to have floor  ceiling left wall right wall and then back here   you need to have a back wall now don't put  a front wall because if you put a front wall   you're not going to be able to look in the room  and so we're just going to imagine the front wall   but there's going to be a back wall a sidewall  sidewall ceiling and floor and then what i want   you to do is i want you to put a little marble  a red marble in the center of the room so you're   going to create this room it is going to be it's  going to be 10 wide it's going to be 10 high and   it's going to be 10 deep the walls are going to  be a of an inch thick and then in the center of   the room you're going to put a red marble okay  that is your homework assignment for next week   guys i hope you're having as much fun with this as  i am and i hope that this has been enough to show   you that this is pretty easy and i hope that it's  been enough to kind of tweak your interest and you   can just play around with what you can play around  with uh boxes and cylinders and spheres you can   play around with boxes cylinder and spheres  you can play around with their color you can   play around with their position and you can play  around with their size and so i think that that is i think that that is a pretty good set of  lessons that we've been able to do today   and then go out and build that room and then next  week what i will do is i will build the room and   you'll see kind of how i did it and if you got  hung then you'll sort of see what the solution is   and then we'll start learning some new stuff next  week okay guys i hope you've enjoyed this lesson   if you enjoyed the lesson make sure to give us  a thumbs up and also if you haven't subscribed   to the channel make sure that you subscribe  when you subscribe make sure you ring that bell   so that you'll get notifications when future  lessons come out and think about sharing this   on your social media we need more people out  there coding and less people watching silly   cat videos so let's see if we can get some more  people interested in this series of lessons again   this is this is paul mcquarter from  toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 108,253
Rating: undefined out of 5
Keywords:
Id: MJiVtz4Uj7M
Channel Id: undefined
Length: 39min 28sec (2368 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.