Merge pull request #22 from severin-lemaignan/master

Polishing of pyassimp code
pull/23/head
Alexander Gessler 2013-03-30 09:42:29 -07:00
commit e39915348b
8 changed files with 46 additions and 128 deletions

View File

@ -25,7 +25,7 @@ substituted by assertions ...):
```python ```python
from pyassimp.core import * from pyassimp import *
scene = load('hello.3ds') scene = load('hello.3ds')
assert len(scene.meshes) assert len(scene.meshes)
@ -44,7 +44,7 @@ scene:
```python ```python
from pyassimp.core import * from pyassimp import *
scene = load('hello.3ds') scene = load('hello.3ds')
for c in scene.rootnode.children: for c in scene.rootnode.children:

View File

@ -0,0 +1 @@
from .core import *

View File

@ -1,90 +0,0 @@
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
"""
This module demonstrates the functionality of PyAssimp.
"""
import pyassimp.core as pyassimp
import os, sys
#get a model out of assimp's test-data if none is provided on the command line
DEFAULT_MODEL = os.path.join(os.path.dirname(__file__),
"..", "..",
"test", "models", "MDL", "MDL3 (3DGS A4)", "minigun.MDL")
def recur_node(node,level = 0):
print(" " + "\t" * level + "- " + str(node))
for child in node.children:
recur_node(child, level + 1)
def main(filename=None):
filename = filename or DEFAULT_MODEL
print "Reading model", filename
scene = pyassimp.load(filename)
#the model we load
print "MODEL:", filename
print
#write some statistics
print "SCENE:"
print " meshes:", len(scene.meshes)
print " materials:", len(scene.materials)
print " textures:", len(scene.textures)
print
print "NODES:"
recur_node(scene.rootnode)
print
print "MESHES:"
for index, mesh in enumerate(scene.meshes):
print " MESH", index+1
print " material id:", mesh.materialindex+1
print " vertices:", len(mesh.vertices)
print " first 3 verts:", mesh.vertices[:3]
if len(mesh.normals) > 0:
print " first 3 normals:", mesh.normals[:3]
else:
print " no normals"
print " colors:", len(mesh.colors)
tc = mesh.texturecoords
if len(tc) >= 4:
print " texture-coords 1:", len(tc[0]), "first3:", tc[0][:3]
print " texture-coords 2:", len(tc[1]), "first3:", tc[1][:3]
print " texture-coords 3:", len(tc[2]), "first3:", tc[2][:3]
print " texture-coords 4:", len(tc[3]), "first3:", tc[3][:3]
elif len(tc) == 0:
print " no texture coordinates"
else:
print " tc is an unexpected number of elements (expect 4, got", len(tc), ")"
print " uv-component-count:", len(mesh.numuvcomponents)
print " faces:", len(mesh.faces), "first:", [f for f in mesh.faces[:3]]
print " bones:", len(mesh.bones), "first:", [str(b) for b in mesh.bones[:3]]
print
print "MATERIALS:"
for index, material in enumerate(scene.materials):
print(" MATERIAL (id:" + str(index+1) + ")")
for key, value in material.properties.items():
print " %s: %s" % (key, value)
print
print "TEXTURES:"
for index, texture in enumerate(scene.textures):
print " TEXTURE", index+1
print " width:", texture.width
print " height:", texture.height
print " hint:", texture.achformathint
print " data (size):", len(texture.data)
# Finally release the model
pyassimp.release(scene)
print "Finished parsing the model."
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv)>1 else None)

View File

@ -11,18 +11,20 @@ Based on:
- http://www.songho.ca/opengl/gl_transform.html - http://www.songho.ca/opengl/gl_transform.html
- http://code.activestate.com/recipes/325391/ - http://code.activestate.com/recipes/325391/
- ASSIMP's C++ SimpleOpenGL viewer - ASSIMP's C++ SimpleOpenGL viewer
Authors: SĂ©verin Lemaignan, 2012-2013
""" """
import sys import sys
import logging import logging
logger = logging.getLogger("underworlds.3d_viewer") logger = logging.getLogger("pyassimp")
gllogger = logging.getLogger("OpenGL") gllogger = logging.getLogger("OpenGL")
gllogger.setLevel(logging.WARNING) gllogger.setLevel(logging.WARNING)
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
import OpenGL import OpenGL
#OpenGL.ERROR_CHECKING=False OpenGL.ERROR_CHECKING=False
#OpenGL.ERROR_LOGGING = False OpenGL.ERROR_LOGGING = False
#OpenGL.ERROR_ON_COPY = True #OpenGL.ERROR_ON_COPY = True
#OpenGL.FULL_LOGGING = True #OpenGL.FULL_LOGGING = True
from OpenGL.GL import * from OpenGL.GL import *
@ -38,7 +40,7 @@ import math, random
import numpy import numpy
from numpy import linalg from numpy import linalg
from pyassimp import core as pyassimp import pyassimp
from pyassimp.postprocess import * from pyassimp.postprocess import *
from pyassimp.helper import * from pyassimp.helper import *
@ -403,8 +405,6 @@ if __name__ == '__main__':
app.render() app.render()
app.controls_3d(0) app.controls_3d(0)
if pygame.K_f in app.keys: pygame.display.toggle_fullscreen() if pygame.K_f in app.keys: pygame.display.toggle_fullscreen()
if pygame.K_s in app.keys: app.screenshot()
if pygame.K_v in app.keys: app.check_visibility()
if pygame.K_TAB in app.keys: app.cycle_cameras() if pygame.K_TAB in app.keys: app.cycle_cameras()
if pygame.K_ESCAPE in app.keys: if pygame.K_ESCAPE in app.keys:
break break

View File

@ -0,0 +1,13 @@
pyassimp examples
=================
- `sample.py`: shows how to load a model with pyassimp, and display some statistics.
- `3d_viewer.py`: an OpenGL 3D viewer that requires shaders
- `fixed_pipeline_3d_viewer`: an OpenGL 3D viewer using the old fixed-pipeline.
Only for illustration example. Base new projects on `3d_viewer.py`.
Requirements for the 3D viewers:
- `pyopengl` (on Ubuntu/Debian, `sudo apt-get install python-opengl`)
- `pygame` (on Ubuntu/Debian, `sudo apt-get install python-pygame`)

View File

@ -35,7 +35,7 @@ logging.basicConfig(level=logging.INFO)
import math import math
import numpy import numpy
from pyassimp import core as pyassimp import pyassimp
from pyassimp.postprocess import * from pyassimp.postprocess import *
from pyassimp.helper import * from pyassimp.helper import *
@ -204,7 +204,6 @@ class GLRenderer():
""" """
if not hasattr(mat, "gl_mat"): # evaluate once the mat properties, and cache the values in a glDisplayList. if not hasattr(mat, "gl_mat"): # evaluate once the mat properties, and cache the values in a glDisplayList.
diffuse = numpy.array(mat.properties.get("diffuse", [0.8, 0.8, 0.8, 1.0])) diffuse = numpy.array(mat.properties.get("diffuse", [0.8, 0.8, 0.8, 1.0]))
specular = numpy.array(mat.properties.get("specular", [0., 0., 0., 1.0])) specular = numpy.array(mat.properties.get("specular", [0., 0., 0., 1.0]))
ambient = numpy.array(mat.properties.get("ambient", [0.2, 0.2, 0.2, 1.0])) ambient = numpy.array(mat.properties.get("ambient", [0.2, 0.2, 0.2, 1.0]))
@ -234,14 +233,6 @@ class GLRenderer():
gl_time = glutGet(GLUT_ELAPSED_TIME) gl_time = glutGet(GLUT_ELAPSED_TIME)
# Compute the new position of the camera and set it
self.x += self.dp * self.lx * 0.01 * (gl_time-self.prev_time)
self.z += self.dp * self.lz * 0.01 * (gl_time-self.prev_time)
self.angle += self.drot * 0.1 * (gl_time-self.prev_time)
self.lx = math.sin(self.angle)
self.lz = -math.cos(self.angle)
self.set_default_camera()
self.angle = (gl_time - self.prev_time) * 0.1 self.angle = (gl_time - self.prev_time) * 0.1
self.prev_time = gl_time self.prev_time = gl_time

View File

@ -12,10 +12,10 @@ loading and querying of 3d models using pyassimp works.
import sys,os import sys,os
import sample import sample
from pyassimp import pyassimp,errors from pyassimp import errors
# paths to be walkd recursively # paths to be walkd recursively
basepaths = [os.path.join('..','..','test','models'), os.path.join('..','..','test','models-nonbsd')] basepaths = [os.path.join('..','..','..','test','models'), os.path.join('..','..','..','test','models-nonbsd')]
# file extensions to be considered # file extensions to be considered
extensions = ['.3ds','.x','.lwo','.obj','.md5mesh','.dxf','.ply','.stl','.dae','.md5anim','.lws','.irrmesh','.nff','.off','.blend'] extensions = ['.3ds','.x','.lwo','.obj','.md5mesh','.dxf','.ply','.stl','.dae','.md5anim','.lws','.irrmesh','.nff','.off','.blend']
@ -23,6 +23,7 @@ extensions = ['.3ds','.x','.lwo','.obj','.md5mesh','.dxf','.ply','.stl','.dae','
def run_tests(): def run_tests():
ok,err = 0,0 ok,err = 0,0
for path in basepaths: for path in basepaths:
print("Looking for models in %s..." % path)
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
for afile in files: for afile in files:
base,ext = os.path.splitext(afile) base,ext = os.path.splitext(afile)
@ -34,7 +35,9 @@ def run_tests():
# assimp error is fine, this is a controlled case # assimp error is fine, this is a controlled case
print error print error
err += 1 err += 1
print '** Loaded %s models, got controlled errors for %s files' % (ok,err) except Exception:
print("Error encountered while loading <%s>"%os.path.join(root,afile))
print('** Loaded %s models, got controlled errors for %s files' % (ok,err))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -7,9 +7,9 @@ This module demonstrates the functionality of PyAssimp.
import os, sys import os, sys
import logging import logging
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.INFO)
from pyassimp import core as pyassimp import pyassimp
def recur_node(node,level = 0): def recur_node(node,level = 0):
print(" " + "\t" * level + "- " + str(node)) print(" " + "\t" * level + "- " + str(node))
@ -48,7 +48,7 @@ def main(filename=None):
print(" no normals") print(" no normals")
print(" colors:" + str(len(mesh.colors))) print(" colors:" + str(len(mesh.colors)))
tcs = mesh.texturecoords tcs = mesh.texturecoords
if tcs: if tcs.any():
for index, tc in enumerate(tcs): for index, tc in enumerate(tcs):
print(" texture-coords "+ str(index) + ":" + str(len(tcs[index])) + "first3:" + str(tcs[index][:3])) print(" texture-coords "+ str(index) + ":" + str(len(tcs[index])) + "first3:" + str(tcs[index][:3]))