Skip to content

Commit

Permalink
created a python3 version of the 3dviewer and fixed the / = float in py3
Browse files Browse the repository at this point in the history
  • Loading branch information
melMass committed Aug 23, 2017
1 parent 91f6a9a commit 37f5619
Show file tree
Hide file tree
Showing 2 changed files with 1,353 additions and 21 deletions.
54 changes: 33 additions & 21 deletions port/PyAssimp/pyassimp/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from ctypes import POINTER
import operator

from distutils.sysconfig import get_python_lib
import re
import sys

try: import numpy
except: numpy = None

Expand All @@ -26,7 +30,15 @@
additional_dirs.append('/usr/lib/x86_64-linux-gnu')
additional_dirs.append('/usr/local/lib/')

# note - this won't catch libassimp.so.N.n, but
# check if running from anaconda.
if "conda" or "continuum" in sys.version.lower():
cur_path = get_python_lib()
pattern = re.compile('.*\/lib\/')
conda_lib = pattern.match(cur_path).group()
logger.info("Adding Anaconda lib path:"+ conda_lib)
additional_dirs.append(conda_lib)

# note - this won't catch libassimp.so.N.n, but
# currently there's always a symlink called
# libassimp.so in /usr/local/lib.
ext_whitelist.append('.so')
Expand All @@ -39,7 +51,7 @@
for dir_candidate in path_dirs:
if 'assimp' in dir_candidate.lower():
additional_dirs.append(dir_candidate)

#print(additional_dirs)
def vec2tuple(x):
""" Converts a VECTOR3D to a Tuple """
Expand All @@ -61,10 +73,10 @@ def transform(vector3, matrix4x4):
m2[0]*x + m2[1]*y + m2[2]*z + m2[3],
m3[0]*x + m3[1]*y + m3[2]*z + m3[3]
]

def _inv(matrix4x4):
m0,m1,m2,m3 = matrix4x4

det = m0[3]*m1[2]*m2[1]*m3[0] - m0[2]*m1[3]*m2[1]*m3[0] - \
m0[3]*m1[1]*m2[2]*m3[0] + m0[1]*m1[3]*m2[2]*m3[0] + \
m0[2]*m1[1]*m2[3]*m3[0] - m0[1]*m1[2]*m2[3]*m3[0] - \
Expand All @@ -77,7 +89,7 @@ def _inv(matrix4x4):
m0[2]*m1[1]*m2[0]*m3[3] + m0[1]*m1[2]*m2[0]*m3[3] + \
m0[2]*m1[0]*m2[1]*m3[3] - m0[0]*m1[2]*m2[1]*m3[3] - \
m0[1]*m1[0]*m2[2]*m3[3] + m0[0]*m1[1]*m2[2]*m3[3]

return[[( m1[2]*m2[3]*m3[1] - m1[3]*m2[2]*m3[1] + m1[3]*m2[1]*m3[2] - m1[1]*m2[3]*m3[2] - m1[2]*m2[1]*m3[3] + m1[1]*m2[2]*m3[3]) /det,
( m0[3]*m2[2]*m3[1] - m0[2]*m2[3]*m3[1] - m0[3]*m2[1]*m3[2] + m0[1]*m2[3]*m3[2] + m0[2]*m2[1]*m3[3] - m0[1]*m2[2]*m3[3]) /det,
( m0[2]*m1[3]*m3[1] - m0[3]*m1[2]*m3[1] + m0[3]*m1[1]*m3[2] - m0[1]*m1[3]*m3[2] - m0[2]*m1[1]*m3[3] + m0[1]*m1[2]*m3[3]) /det,
Expand All @@ -94,7 +106,7 @@ def _inv(matrix4x4):
( m0[1]*m2[2]*m3[0] - m0[2]*m2[1]*m3[0] + m0[2]*m2[0]*m3[1] - m0[0]*m2[2]*m3[1] - m0[1]*m2[0]*m3[2] + m0[0]*m2[1]*m3[2]) /det,
( m0[2]*m1[1]*m3[0] - m0[1]*m1[2]*m3[0] - m0[2]*m1[0]*m3[1] + m0[0]*m1[2]*m3[1] + m0[1]*m1[0]*m3[2] - m0[0]*m1[1]*m3[2]) /det,
( m0[1]*m1[2]*m2[0] - m0[2]*m1[1]*m2[0] + m0[2]*m1[0]*m2[1] - m0[0]*m1[2]*m2[1] - m0[1]*m1[0]*m2[2] + m0[0]*m1[1]*m2[2]) /det]]

def get_bounding_box(scene):
bb_min = [1e10, 1e10, 1e10] # x,y,z
bb_max = [-1e10, -1e10, -1e10] # x,y,z
Expand Down Expand Up @@ -129,7 +141,7 @@ def get_bounding_box_for_node(node, bb_min, bb_max, transformation):
t3[0]*T0[2] + t3[1]*T1[2] + t3[2]*T2[2] + t3[3]*T3[2],
t3[0]*T0[3] + t3[1]*T1[3] + t3[2]*T2[3] + t3[3]*T3[3]
] ]

for mesh in node.meshes:
for v in mesh.vertices:
v = transform(v, transformation)
Expand All @@ -149,25 +161,25 @@ def get_bounding_box_for_node(node, bb_min, bb_max, transformation):
def try_load_functions(library_path, dll):
'''
Try to bind to aiImportFile and aiReleaseImport
Arguments
---------
library_path: path to current lib
dll: ctypes handle to library
Returns
---------
If unsuccessful:
None
If successful:
Tuple containing (library_path,
Tuple containing (library_path,
load from filename function,
load from memory function,
export to filename function,
release function,
release function,
ctypes handle to assimp library)
'''

try:
load = dll.aiImportFile
release = dll.aiReleaseImport
Expand All @@ -176,7 +188,7 @@ def try_load_functions(library_path, dll):
except AttributeError:
#OK, this is a library, but it doesn't have the functions we need
return None

# library found!
from .structs import Scene
load.restype = POINTER(Scene)
Expand All @@ -185,13 +197,13 @@ def try_load_functions(library_path, dll):

def search_library():
'''
Loads the assimp library.
Loads the assimp library.
Throws exception AssimpError if no library_path is found
Returns: tuple, (load from filename function,
Returns: tuple, (load from filename function,
load from memory function,
export to filename function,
release function,
release function,
dll)
'''
#this path
Expand All @@ -201,15 +213,15 @@ def search_library():
try:
ctypes.windll.kernel32.SetErrorMode(0x8007)
except AttributeError:
pass
pass

candidates = []
# test every file
for curfolder in [folder]+additional_dirs:
if os.path.isdir(curfolder):
for filename in os.listdir(curfolder):
# our minimum requirement for candidates is that
# they should contain 'assimp' somewhere in
# they should contain 'assimp' somewhere in
# their name
if filename.lower().find('assimp')==-1 or\
os.path.splitext(filename)[-1].lower() not in ext_whitelist:
Expand Down Expand Up @@ -248,10 +260,10 @@ def hasattr_silent(object, name):
"""
Calls hasttr() with the given parameters and preserves the legacy (pre-Python 3.2)
functionality of silently catching exceptions.
Returns the result of hasatter() or False if an exception was raised.
"""

try:
return hasattr(object, name)
except:
Expand Down
Loading

0 comments on commit 37f5619

Please sign in to comment.