Skip to content

Commit

Permalink
merge severin-lemaignan:for-upstream from github. This is a full rewr…
Browse files Browse the repository at this point in the history
…ite of pyassimp, rendering it much easier to use.

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@1332 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
  • Loading branch information
aramis_acg committed Nov 9, 2012
1 parent 81aef90 commit 4286c72
Show file tree
Hide file tree
Showing 17 changed files with 975 additions and 1,885 deletions.
66 changes: 0 additions & 66 deletions port/PyAssimp/README

This file was deleted.

23 changes: 16 additions & 7 deletions port/PyAssimp/gen/structsgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
# ---------------------------------------------------------------------------

"""Update PyAssimp's data structures to keep up with the
C/C++ headers."""
C/C++ headers.
This script is meant to be executed in the source tree, directly from
port/PyAssimp/gen
"""

import os
import re
Expand Down Expand Up @@ -96,7 +100,7 @@ def GetType(type, prefix='c_'):
return None

t = t.strip()
types = {'unsigned int':'uint', 'unsigned char':'ubyte', 'size_t':'uint',}
types = {'unsigned int':'uint', 'unsigned char':'ubyte',}
if t in types:
t = types[t]
t = prefix + t
Expand Down Expand Up @@ -210,9 +214,11 @@ def Structify(fileName):

# Restructure
text = RErestruc.sub(restructure, text)

# Replace comments
text = RErpcom.sub('#\g<line>', text)
text = RErpcom.sub('# \g<line>', text)
text = text.replace("),#", "),\n#")
text = text.replace("#", "\n#")
text = "".join([l for l in text.splitlines(True) if not l.strip().endswith("#")]) # remove empty comment lines

# Whether it's selfreferencing: ex. struct Node { Node* parent; };
selfreferencing = text.find('POINTER('+name+')') != -1
Expand Down Expand Up @@ -245,20 +251,21 @@ def Structify(fileName):
text = text.replace('$DEFINES$', defines)
else:
text = text.replace('$DEFINES$', '')


result.append((primitive, selfreferencing, complex, text))

return result

text = "#-*- coding: UTF-8 -*-\n\n"
text += "from ctypes import POINTER, c_int, c_uint, c_char, c_float, Structure, c_char_p, c_double, c_ubyte\n\n"
text += "from ctypes import POINTER, c_int, c_uint, c_size_t, c_char, c_float, Structure, c_char_p, c_double, c_ubyte\n\n"

structs1 = ""
structs2 = ""
structs3 = ""
structs4 = ""

path = '../include/'
path = '../../../include/assimp'
files = os.listdir (path)
#files = ["aiScene.h", "aiTypes.h"]
for fileName in files:
Expand All @@ -276,6 +283,8 @@ def Structify(fileName):

text += structs1 + structs2 + structs3 + structs4

file = open('structs.txt', 'w')
file = open('structs.py', 'w')
file.write(text)
file.close()

print("Generation done. You can now review the file 'structs.py' and merge it.")
1 change: 0 additions & 1 deletion port/PyAssimp/pyassimp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#-*- coding: UTF-8 -*-
55 changes: 46 additions & 9 deletions port/PyAssimp/pyassimp/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@

import os
import ctypes
import structs
from ctypes import POINTER
import operator
import numpy

from errors import AssimpError
from ctypes import POINTER
import logging;logger = logging.getLogger("pyassimp")

from .errors import AssimpError

additional_dirs, ext_whitelist = [],[]

# populate search directories and lists of allowed file extensions
# depending on the platform we're running on.
if os.name=='posix':
additional_dirs.append('/usr/lib/')
additional_dirs.append('/usr/local/lib/')

# note - this won't catch libassimp.so.N.n, but
Expand All @@ -27,11 +30,43 @@
elif os.name=='nt':
ext_whitelist.append('.dll')

print(additional_dirs)
#print(additional_dirs)
def vec2tuple(x):
""" Converts a VECTOR3D to a Tuple """
return (x.x, x.y, x.z)

def transform(vector3, matrix4x4):
""" Apply a transformation matrix on a 3D vector.
:param vector3: a numpy array with 3 elements
:param matrix4x4: a numpy 4x4 matrix
"""
return numpy.dot(matrix4x4, numpy.append(vector3, 1.))


def get_bounding_box(scene):
bb_min = [1e10, 1e10, 1e10] # x,y,z
bb_max = [-1e10, -1e10, -1e10] # x,y,z
return get_bounding_box_for_node(scene.rootnode, bb_min, bb_max)

def get_bounding_box_for_node(node, bb_min, bb_max):
for mesh in node.meshes:
for v in mesh.vertices:
v = transform(v, node.transformation)
bb_min[0] = min(bb_min[0], v[0])
bb_min[1] = min(bb_min[1], v[1])
bb_min[2] = min(bb_min[2], v[2])
bb_max[0] = max(bb_max[0], v[0])
bb_max[1] = max(bb_max[1], v[1])
bb_max[2] = max(bb_max[2], v[2])


for child in node.children:
bb_min, bb_max = get_bounding_box_for_node(child, bb_min, bb_max)

return bb_min, bb_max



def try_load_functions(library,dll,candidates):
"""try to functbind to aiImportFile and aiReleaseImport
Expand All @@ -54,7 +89,8 @@ def try_load_functions(library,dll,candidates):
pass
else:
#Library found!
load.restype = POINTER(structs.Scene)
from .structs import Scene
load.restype = POINTER(Scene)

candidates.append((library, load, release, dll))

Expand Down Expand Up @@ -88,10 +124,11 @@ def search_library():
continue

library = os.path.join(curfolder, filename)
print 'Try ',library
logger.debug('Try ' + library)
try:
dll = ctypes.cdll.LoadLibrary(library)
except:
except Exception as e:
logger.warning(str(e))
# OK, this except is evil. But different OSs will throw different
# errors. So just ignore any errors.
continue
Expand All @@ -100,12 +137,12 @@ def search_library():

if not candidates:
# no library found
raise AssimpError, "assimp library not found"
raise AssimpError("assimp library not found")
else:
# get the newest library
candidates = map(lambda x: (os.lstat(x[0])[-2], x), candidates)
res = max(candidates, key=operator.itemgetter(0))[1]
print 'Taking ',res[0]
logger.debug('Using assimp library located at ' + res[0])

# XXX: if there are 1000 dll/so files containing 'assimp'
# in their name, do we have all of them in our address
Expand Down
Loading

0 comments on commit 4286c72

Please sign in to comment.