python中有什么好用的工具用来处理mesh模型?
如何把一个平面(立方体的一个面)转换为mesh格式,工具是否能实现
python中有什么好用的工具用来处理mesh模型?
如何把一个平面(立方体的一个面)转换为mesh格式,工具是否能实现
首先Mesh文件为unity平台独有的文件类型,用以描述模型
unity本身对外开放性并不那么友好,所以一些中间平台会比较好
https://gist.github.com/MattRix/0522c27ee44c0fbbdf76d65de123eeff
这里是把Mesh从unity3D导出到OBJ文件的指南
然后python有大量的库可以处理OBJ文件
比如OpenGL
https://stackoverflow.com/questions/59923419/pyopengl-how-do-i-import-an-obj-file
这里有个教程
以防打不开stack overflow,这里贴下代码
import pygame
import OpenGL
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import pywavefront
scene = pywavefront.Wavefront('bunny.obj', collect_faces=True)
scene_box = (scene.vertices[0], scene.vertices[0])
for vertex in scene.vertices:
min_v = [min(scene_box[0][i], vertex[i]) for i in range(3)]
max_v = [max(scene_box[1][i], vertex[i]) for i in range(3)]
scene_box = (min_v, max_v)
scene_size = [scene_box[1][i]-scene_box[0][i] for i in range(3)]
max_scene_size = max(scene_size)
scaled_size = 5
scene_scale = [scaled_size/max_scene_size for i in range(3)]
scene_trans = [-(scene_box[1][i]+scene_box[0][i])/2 for i in range(3)]
def Model():
glPushMatrix()
glScalef(*scene_scale)
glTranslatef(*scene_trans)
for mesh in scene.mesh_list:
glBegin(GL_TRIANGLES)
for face in mesh.faces:
for vertex_i in face:
glVertex3f(*scene.vertices[vertex_i])
glEnd()
glPopMatrix()
def main():
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0] / display[1]), 1, 500.0)
glTranslatef(0.0, 0.0, -10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
glTranslatef(-0.5,0,0)
if event.key == pygame.K_RIGHT:
glTranslatef(0.5,0,0)
if event.key == pygame.K_UP:
glTranslatef(0,1,0)
if event.key == pygame.K_DOWN:
glTranslatef(0,-1,0)
glRotatef(1, 5, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
Model()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
pygame.display.flip()
pygame.time.wait(10)
main()