Advertisement
here2share

# load_3D_models.py

Feb 25th, 2021
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. # load_3D_models.py
  2.  
  3. from math import cos, radians, sin
  4. from Tkinter import *
  5. ww = 1200
  6. hh = 600
  7. root = Tk()
  8. root.title("Tk Load_3D_Models")
  9. root.geometry("%dx%d+-10+0"%(ww,hh))
  10. canvas = Canvas(root, width=ww, height=hh)
  11. canvas.pack()
  12.  
  13. rx = lambda v, angle : (v[0],(cos(radians(angle))*v[1]) + ((-sin(radians(angle)))*v[2]),
  14.                              (sin(radians(angle))*v[1]) + ((cos(radians(angle)))*v[2]))
  15.  
  16. ry = lambda v, angle : (((cos(radians(angle)))*v[0]) + ((-sin(radians(angle)))*v[1]), v[2],
  17.                         ((sin(radians(angle)))*v[0]) + ((cos(radians(angle)))*v[1]))
  18.  
  19. rz = lambda v, angle : (((cos(radians(angle)))*v[0]) + ((-sin(radians(angle)))*v[1]),
  20.                         ((sin(radians(angle)))*v[0]) + ((cos(radians(angle)))*v[1]), v[2])
  21. class shape:
  22.     def __init__(self, file, canvas):
  23.         self.location, self.scale = [0, 0, 0], 1
  24.         self.rotation = [0, 0, 0]
  25.         self.canvas = canvas
  26.         self.EdgeThickness = 1
  27.         self.color="#ffffff​"
  28.         self.width = int(self.canvas["width"])
  29.         self.height = int(self.canvas["height"])
  30.         def load_model(file):
  31.             from functools import reduce
  32.             l=open(file, 'r').readlines()
  33.             verts, lines =[], []
  34.             for i in l:
  35.                 if (i[:1]=='v') and (i[:2]!='vn'):
  36.                     xv, yv, zv = (i[2:].split(' '))
  37.                     verts.append((float(xv), float(yv), float(zv)))
  38.                 elif (i[:1]=='f'):
  39.                     face = reduce(lambda x, y:x+y, map(lambda x:x.split(' '), i.split('//')))[-6:]
  40.                     lines.extend(((verts[int(face[0])-1], verts[int(face[2])-1]),
  41.                                   (verts[int(face[2])-1], verts[int(face[4])-1]),
  42.                                   (verts[int(face[4])-1], verts[int(face[0])-1])))
  43.             return tuple(map(lambda line:
  44.                              (tuple(map(lambda n:n*self.scale, line[0])), tuple(map(lambda n:n*self.scale, line[1]))),
  45.                         lines))
  46.  
  47.         self.lines = load_model(file)
  48.     def render(self):
  49.         u=int(self.width/16)
  50.         fl=0.15
  51.         def xcor(x, y):
  52.             try:
  53.                 if (x!=0): return (self.width/2)-(x/(y*fl))*(-1*u)
  54.                 else: return (self.width/2)+(x/(y*fl))*u
  55.             except(ZeroDivisionError):return 0
  56.         def ycor(z, y):
  57.             try:
  58.                 if (z!=0): return (self.height/2)-(z/(y*fl))*u
  59.                 else: return (self.height/2)+(z/(y*fl))*(-1*u)
  60.             except(ZeroDivisionError):return 0
  61.  
  62.         add = lambda x, y:tuple(map(lambda a, b:a+b, x, y))
  63.         vr = list(map(lambda v:(add(self.location, rz(ry(rx(v[0], self.rotation[0]), self.rotation[1]), self.rotation[2])),
  64.                                 add(self.location, rz(ry(rx(v[1], self.rotation[0]), self.rotation[1]), self.rotation[2]))),
  65.                       self.lines))
  66.         for l in vr: self.canvas.create_line(xcor(l[0][0], l[0][1]), ycor(l[0][2], l[0][1]),
  67.                                              xcor(l[1][0], l[1][1]), ycor(l[1][2], l[1][1]), fill=self.color, width=self.EdgeThickness)
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement