카테고리 없음

Python 3D graphics tutorial in Vpython | TopTechBoy

토마토. 2021. 9. 19. 14:38

https://www.youtube.com/watch?v=03ysGfNO-G8&list=PLGs0VKk2DiYzGCOzBrMNSWEdd2CIGC0kJ&index=2 

 

-> 토마토에 필요한 요소 :

비율 조정한 구(타원형 구) 몇 개 / 색깔 조정 / 표정 / 막대기 등 + 여러 개 같이 움직이게 만들기

 

0. vpython

 

# visual python
#what is v python?
#v python is a library
# that runs under python and
# it allows us to do all the cool 3d simulations and graphics

 

1. 설치

pip install vpython

 

2. 3d simulations and graphics

hello world

import vpython

ball = vpython.sphere()

while True:
    pass

from vpython import *

ball = sphere(color=color.red)

while True:
    pass

색 지정 가능함

from vpython import *
from time import *

ball = sphere(color=color.red)
sleep(5)

ball.color=color.green

while True:
    pass
from vpython import *
from time import *

ball = sphere(color=color.red)
sleep(5)

ball.color=color.green
sleep(5)

ball.color=color.blue

while True:
    pass

 

from vpython import *
from time import *

mybox = box(color=color.yellow)


while True:
    pass

Screencast from 2021년 09월 19일 12시 51분 18초.webm
0.94MB

 

마우스 우클릭 + 스크롤을 이용해서 조절할 수 있다.

 

파라미터 조정을 통해 모양 바꾼다.

from vpython import *
from time import *

#length = x, width = y, height = z
mybox = box(color=color.white, length=12, width=1, height=.2)


while True:
    pass


# 화면 레코드 : Ctrl+Alt+Shift+R

 

이번에는 실린더

from vpython import *
from time import *

#length = x, width = y, height = z
mytube = cylinder(color=color.orange, length=20, radius=6)

while True:
    pass


# 화면 레코드 : Ctrl+Alt+Shift+R

 

-> 실린더를 화면 가운데 두려면?

 

 

여기까지가 hello world

 

-> 원기둥을 타원형으로 만들고 싶다면? length, width, height로 파라미터 지정

 

mytube = cylinder(color=color.orange, length=.4, width=.2,height=.1)
wall = box(color=color.yellow, length=10, height=.1, width=10)
while True:
    pass

#length = x, width = y, height = z
#vector 첫번째 칼럼 : 행은 정해져 있고, 열은 어디서 시작할 것인가? 물체의 좌우
#vector 두번째 칼럼 : 열은 정해져 있고, 행을 어디서 시작할 것인가? 물체의 높이
#vector 세번째 칼럼 : 물체를 정면(화면)에서 얼마나 멀리 둘 것인가? 
wall = box(pos=vector(0,0,-10), color=color.yellow, length=10, height=.1, width=10)
while True:
    pass

...

from vpython import *
from time import *

#length = x, width = y, height = z
#vector 첫번째 칼럼 : 행은 정해져 있고, 열은 어디서 시작할 것인가? 물체의 좌우
#vector 두번째 칼럼 : 열은 정해져 있고, 행을 어디서 시작할 것인가? 물체의 높이
#vector 세번째 칼럼 : 물체를 정면(화면)에서 얼마나 멀리 둘 것인가? 

canvas(width=700, height=500, background=color.white)

floor = box(pos=vector(0,-4.15,0), color=color.green, length=10, height=.1, width=10)
#ceiling = box(pos = vector(0,5,0), color = color.white, length=10, height=.1, width=10)

ball = sphere(pos = vector(0,-2.5,0), color=color.red, length = 2, height = 2.5, width = 2)

hair1 = sphere(pos = vector(-.3,-1.15,0), color=color.green, radius =.2)
hair2 = sphere(pos = vector(0,-1.1,0), color=color.green, radius =.2)
hair3 = sphere(pos = vector(+.3,-1.15,0), color=color.green, radius =.2)

leg1 = cylinder(pos = vector(-0.3,-3.7,0), color=color.black, length=.17, width=.2, height=0.44)
leg2 = cylinder(pos = vector(+0.3,-3.7,0), color=color.black, length=.17, width=.2, height=0.44)
foot1 = sphere(pos = vector(-.24,-4,0), color=color.green, radius =.1)
foot2 = sphere(pos = vector(+.36,-4,0), color=color.green, radius =.1)

x = -0.8
y = +1
x2 = +0.7
y2 = +1

arm1 = cylinder(pos = vector(-0.3+x,-3.7+y,0), color=color.black, length=.17, width=.2, height=0.44)
arm2 = cylinder(pos = vector(+0.3+x2,-3.7+y2,0), color=color.black, length=.17, width=.2, height=0.44)
hand1 = sphere(pos = vector(-.24+x,-4+y,0), color=color.green, radius =.1)
hand2 = sphere(pos = vector(+.36+x2,-4+y2,0), color=color.green, radius =.1)

comp = [ball, hair1, hair2, hair3, leg1, leg2, foot1, foot2, arm1, arm2, hand1, hand2]

deltaX = 0.001
xPos = 0
# 어렵다. 
while True:
    # file loop에서 할 것이다. 
    rate(100)
    xPos = 0.1
    for i in comp:
        i.pos = i.pos + vector(xPos,0,0)

    if xPos > 0.1 or xPos < -0.1:
        xPos = 0

# 화면 레코드 : Ctrl+Alt+Shift+R
# box, ball, cylinder