From bf7d39280e19096445d53cc06f62a5d401cc200b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Lemaignan?= Date: Sat, 10 Nov 2012 23:30:58 +0100 Subject: [PATCH 1/6] [pyassimp] Simplified and beautify simple_opengl_viewer.py Since the new SDL based viewer is much better suited to interactive exploration, simplify the basic OpenGL viewer to make it an easy introductive read. --- port/PyAssimp/scripts/simple_opengl_viewer.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/port/PyAssimp/scripts/simple_opengl_viewer.py b/port/PyAssimp/scripts/simple_opengl_viewer.py index 8373f7f03..036e19787 100755 --- a/port/PyAssimp/scripts/simple_opengl_viewer.py +++ b/port/PyAssimp/scripts/simple_opengl_viewer.py @@ -204,7 +204,6 @@ class GLRenderer(): """ 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])) 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])) @@ -212,8 +211,9 @@ class GLRenderer(): shininess = min(mat.properties.get("shininess", 1.0), 128) wireframe = mat.properties.get("wireframe", 0) twosided = mat.properties.get("twosided", 1) - - setattr(mat, "gl_mat", glGenLists(1)) + from OpenGL.raw import GL + setattr(mat, "gl_mat", GL.GLuint(0)) + mat.gl_mat = glGenLists(1) glNewList(mat.gl_mat, GL_COMPILE) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse) @@ -234,15 +234,7 @@ class GLRenderer(): 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.01 self.prev_time = gl_time From b00ee8b24ab733efb431580b2df5bb000375c1eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Lemaignan?= Date: Sat, 10 Nov 2012 23:47:59 +0100 Subject: [PATCH 2/6] [pyassimp] Minor fixes to the simplified open_gl viwer --- port/PyAssimp/scripts/simple_opengl_viewer.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/port/PyAssimp/scripts/simple_opengl_viewer.py b/port/PyAssimp/scripts/simple_opengl_viewer.py index 036e19787..5365e99f3 100755 --- a/port/PyAssimp/scripts/simple_opengl_viewer.py +++ b/port/PyAssimp/scripts/simple_opengl_viewer.py @@ -211,9 +211,8 @@ class GLRenderer(): shininess = min(mat.properties.get("shininess", 1.0), 128) wireframe = mat.properties.get("wireframe", 0) twosided = mat.properties.get("twosided", 1) - from OpenGL.raw import GL - setattr(mat, "gl_mat", GL.GLuint(0)) - mat.gl_mat = glGenLists(1) + + setattr(mat, "gl_mat", glGenLists(1)) glNewList(mat.gl_mat, GL_COMPILE) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse) @@ -234,7 +233,7 @@ class GLRenderer(): gl_time = glutGet(GLUT_ELAPSED_TIME) - self.angle += (gl_time - self.prev_time) * 0.01 + self.angle = (gl_time - self.prev_time) * 0.1 self.prev_time = gl_time From fb280d7cf510676c9bd047852843e8523b19565d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Lemaignan?= Date: Wed, 7 Nov 2012 22:56:54 +0100 Subject: [PATCH 3/6] [pyassimp] Make it easier to import pyassimp 'import pyassimp' (instead of 'from pyassimp import core') is now sufficient --- port/PyAssimp/README.md | 4 ++-- port/PyAssimp/pyassimp/__init__.py | 1 + port/PyAssimp/scripts/advanced_3d_viewer.py | 4 ++-- port/PyAssimp/scripts/sample.py | 4 ++-- port/PyAssimp/scripts/simple_opengl_viewer.py | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/port/PyAssimp/README.md b/port/PyAssimp/README.md index db014a5b3..77db57fae 100644 --- a/port/PyAssimp/README.md +++ b/port/PyAssimp/README.md @@ -25,7 +25,7 @@ substituted by assertions ...): ```python -from pyassimp.core import * +from pyassimp import * scene = load('hello.3ds') assert len(scene.meshes) @@ -44,7 +44,7 @@ scene: ```python -from pyassimp.core import * +from pyassimp import * scene = load('hello.3ds') for c in scene.rootnode.children: diff --git a/port/PyAssimp/pyassimp/__init__.py b/port/PyAssimp/pyassimp/__init__.py index e69de29bb..bb67a43fa 100644 --- a/port/PyAssimp/pyassimp/__init__.py +++ b/port/PyAssimp/pyassimp/__init__.py @@ -0,0 +1 @@ +from .core import * diff --git a/port/PyAssimp/scripts/advanced_3d_viewer.py b/port/PyAssimp/scripts/advanced_3d_viewer.py index 33fa84524..87dbaa01c 100755 --- a/port/PyAssimp/scripts/advanced_3d_viewer.py +++ b/port/PyAssimp/scripts/advanced_3d_viewer.py @@ -15,7 +15,7 @@ Based on: import sys import logging -logger = logging.getLogger("underworlds.3d_viewer") +logger = logging.getLogger("pyassimp") gllogger = logging.getLogger("OpenGL") gllogger.setLevel(logging.WARNING) logging.basicConfig(level=logging.INFO) @@ -38,7 +38,7 @@ import math, random import numpy from numpy import linalg -from pyassimp import core as pyassimp +import pyassimp from pyassimp.postprocess import * from pyassimp.helper import * diff --git a/port/PyAssimp/scripts/sample.py b/port/PyAssimp/scripts/sample.py index b843b82ba..adc80f81b 100755 --- a/port/PyAssimp/scripts/sample.py +++ b/port/PyAssimp/scripts/sample.py @@ -7,9 +7,9 @@ This module demonstrates the functionality of PyAssimp. import os, sys 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): print(" " + "\t" * level + "- " + str(node)) diff --git a/port/PyAssimp/scripts/simple_opengl_viewer.py b/port/PyAssimp/scripts/simple_opengl_viewer.py index 5365e99f3..48c157917 100755 --- a/port/PyAssimp/scripts/simple_opengl_viewer.py +++ b/port/PyAssimp/scripts/simple_opengl_viewer.py @@ -35,7 +35,7 @@ logging.basicConfig(level=logging.INFO) import math import numpy -from pyassimp import core as pyassimp +import pyassimp from pyassimp.postprocess import * from pyassimp.helper import * From 3574b6973cf3945987d7460a75533147c7afda9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Lemaignan?= Date: Sat, 23 Mar 2013 23:40:01 +0100 Subject: [PATCH 4/6] [pyassimp] Make sure sample.py and quicktest.py work --- port/PyAssimp/sample.py | 90 ------------------------------ port/PyAssimp/scripts/quicktest.py | 35 ++++++------ port/PyAssimp/scripts/sample.py | 2 +- 3 files changed, 20 insertions(+), 107 deletions(-) delete mode 100755 port/PyAssimp/sample.py diff --git a/port/PyAssimp/sample.py b/port/PyAssimp/sample.py deleted file mode 100755 index 20cebcd36..000000000 --- a/port/PyAssimp/sample.py +++ /dev/null @@ -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) diff --git a/port/PyAssimp/scripts/quicktest.py b/port/PyAssimp/scripts/quicktest.py index e01a49fa5..7e8caa3b2 100755 --- a/port/PyAssimp/scripts/quicktest.py +++ b/port/PyAssimp/scripts/quicktest.py @@ -12,29 +12,32 @@ loading and querying of 3d models using pyassimp works. import sys,os import sample -from pyassimp import pyassimp,errors +from pyassimp import errors # 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 extensions = ['.3ds','.x','.lwo','.obj','.md5mesh','.dxf','.ply','.stl','.dae','.md5anim','.lws','.irrmesh','.nff','.off','.blend'] def run_tests(): - ok,err = 0,0 - for path in basepaths: - for root, dirs, files in os.walk(path): - for afile in files: - base,ext = os.path.splitext(afile) - if ext in extensions: - try: - sample.main(os.path.join(root,afile)) - ok += 1 - except errors.AssimpError as error: - # assimp error is fine, this is a controlled case - print error - err += 1 - print '** Loaded %s models, got controlled errors for %s files' % (ok,err) + ok,err = 0,0 + for path in basepaths: + print("Looking for models in %s..." % path) + for root, dirs, files in os.walk(path): + for afile in files: + base,ext = os.path.splitext(afile) + if ext in extensions: + try: + sample.main(os.path.join(root,afile)) + ok += 1 + except errors.AssimpError as error: + # assimp error is fine, this is a controlled case + print error + err += 1 + 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__': diff --git a/port/PyAssimp/scripts/sample.py b/port/PyAssimp/scripts/sample.py index adc80f81b..e93e1d7b0 100755 --- a/port/PyAssimp/scripts/sample.py +++ b/port/PyAssimp/scripts/sample.py @@ -48,7 +48,7 @@ def main(filename=None): print(" no normals") print(" colors:" + str(len(mesh.colors))) tcs = mesh.texturecoords - if tcs: + if tcs.any(): for index, tc in enumerate(tcs): print(" texture-coords "+ str(index) + ":" + str(len(tcs[index])) + "first3:" + str(tcs[index][:3])) From fa7ff915c1d8d894fff09e0fbd334fb5586b525f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Lemaignan?= Date: Sat, 23 Mar 2013 23:47:45 +0100 Subject: [PATCH 5/6] [pyassimp] Renamed the 3d for better clarity + added a short README --- .../scripts/{advanced_3d_viewer.py => 3d_viewer.py} | 0 port/PyAssimp/scripts/README.md | 13 +++++++++++++ ...opengl_viewer.py => fixed_pipeline_3d_viewer.py} | 0 3 files changed, 13 insertions(+) rename port/PyAssimp/scripts/{advanced_3d_viewer.py => 3d_viewer.py} (100%) create mode 100644 port/PyAssimp/scripts/README.md rename port/PyAssimp/scripts/{simple_opengl_viewer.py => fixed_pipeline_3d_viewer.py} (100%) diff --git a/port/PyAssimp/scripts/advanced_3d_viewer.py b/port/PyAssimp/scripts/3d_viewer.py similarity index 100% rename from port/PyAssimp/scripts/advanced_3d_viewer.py rename to port/PyAssimp/scripts/3d_viewer.py diff --git a/port/PyAssimp/scripts/README.md b/port/PyAssimp/scripts/README.md new file mode 100644 index 000000000..42caa2791 --- /dev/null +++ b/port/PyAssimp/scripts/README.md @@ -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`) diff --git a/port/PyAssimp/scripts/simple_opengl_viewer.py b/port/PyAssimp/scripts/fixed_pipeline_3d_viewer.py similarity index 100% rename from port/PyAssimp/scripts/simple_opengl_viewer.py rename to port/PyAssimp/scripts/fixed_pipeline_3d_viewer.py From da140f2a05dca5e106766480572fdfef47d606d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Lemaignan?= Date: Sun, 24 Mar 2013 00:12:02 +0100 Subject: [PATCH 6/6] [pyassimp] Minor tuning in 3d_viewer.py --- port/PyAssimp/scripts/3d_viewer.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/port/PyAssimp/scripts/3d_viewer.py b/port/PyAssimp/scripts/3d_viewer.py index 87dbaa01c..60170af4b 100755 --- a/port/PyAssimp/scripts/3d_viewer.py +++ b/port/PyAssimp/scripts/3d_viewer.py @@ -6,11 +6,13 @@ It make a large use of shaders to illustrate a 'modern' OpenGL pipeline. Based on: -- pygame + mouselook code from http://3dengine.org/Spectator_%28PyOpenGL%29 + - pygame + mouselook code from http://3dengine.org/Spectator_%28PyOpenGL%29 - http://www.lighthouse3d.com/tutorials - http://www.songho.ca/opengl/gl_transform.html - http://code.activestate.com/recipes/325391/ - ASSIMP's C++ SimpleOpenGL viewer + +Authors: Séverin Lemaignan, 2012-2013 """ import sys @@ -21,8 +23,8 @@ gllogger.setLevel(logging.WARNING) logging.basicConfig(level=logging.INFO) import OpenGL -#OpenGL.ERROR_CHECKING=False -#OpenGL.ERROR_LOGGING = False +OpenGL.ERROR_CHECKING=False +OpenGL.ERROR_LOGGING = False #OpenGL.ERROR_ON_COPY = True #OpenGL.FULL_LOGGING = True from OpenGL.GL import * @@ -403,8 +405,6 @@ if __name__ == '__main__': app.render() app.controls_3d(0) 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_ESCAPE in app.keys: break