Skip to content

Commit

Permalink
syncing mopp python wrapper with the PyFFI version
Browse files Browse the repository at this point in the history
  • Loading branch information
amorilia committed Jul 14, 2008
1 parent 9e0e397 commit a142624
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions NifMopp.py → Mopp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# ***** BEGIN LICENSE BLOCK *****
#
# Copyright (c) 2007-2008, NIF File Format Library and Tools.
# Copyright (c) 2007-2008, Python File Format Interface
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand All @@ -17,7 +17,7 @@
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * Neither the name of the NIF File Format Library and Tools
# * Neither the name of the Python File Format Interface
# project nor the names of its contributors may be used to endorse
# or promote products derived from this software without specific
# prior written permission.
Expand All @@ -39,18 +39,18 @@

from ctypes import *
from itertools import chain
import os.path

_NifMopp = WinDLL("NifMopp.dll")
try:
_NifMopp = WinDLL(os.path.join(os.path.dirname(__file__), "NifMopp.dll"))
except NameError:
# on linux WinDLL will raise a NameError
_NifMopp = None

def getMoppScaleOriginCode(vertices, triangles):
"""Generate mopp code for given geometry.
@param vertices: List of vertices.
@type vertices: list of tuples of floats
@param triangles: List of triangles (indices referring back to vertex list).
@type triangles: list of tuples of ints
@return: The mopp scale as a float, the origin as a tuple of floats, and
the mopp code as a list of ints.
"""Generate mopp code for given geometry. Raises RuntimeError if something
goes wrong (e.g. if mopp generator fails, or if NifMopp.dll cannot be
loaded on the current platform).
For example, creating a mopp for the standard cube:
Expand All @@ -61,35 +61,44 @@ def getMoppScaleOriginCode(vertices, triangles):
... (0, 2, 4), (4, 1, 7), (6, 4, 7), (3, 0, 6),
... (0, 3, 5), (3, 2, 5), (2, 0, 5), (1, 3, 6)])
>>> scale
6.2187392503428082e-315
>>> orig
(-5.3776448224770004e-019, 1.5595011253197302e-314, 0.0)
16319749.0
>>> ["%6.3f" % value for value in orig]
['-0.010', '-0.010', '-0.010']
>>> moppcode
[40, 0, 255, 39, 0, 255, 38, 0, 255, 22, 129, 126, 9, 38, 0, 3, 16, 3, 0, 1, 58, 56, 39, 0, 3, 16, 255, 0, 1, 49, 53]
[40, 0, 255, 39, 0, 255, 38, 0, 255, 19, 129, 125, 41, 22, 130, 125, 12, 24, 130, 125, 4, 38, 0, 5, 51, 39, 0, 5, 50, 24, 130, 125, 4, 40, 0, 5, 59, 16, 255, 249, 12, 20, 130, 125, 4, 39, 0, 5, 53, 40, 0, 5, 49, 54, 22, 130, 125, 25, 24, 130, 125, 17, 17, 255, 249, 12, 21, 129, 125, 4, 38, 0, 5, 57, 40, 249, 255, 58, 56, 40, 249, 255, 52, 24, 130, 125, 4, 39, 249, 255, 55, 38, 249, 255, 48]
@param vertices: List of vertices.
@type vertices: list of tuples of floats
@param triangles: List of triangles (indices referring back to vertex list).
@type triangles: list of tuples of ints
@return: The mopp scale as a float, the origin as a tuple of floats, and
the mopp code as a list of ints.
"""
if _NifMopp is None:
raise RuntimeError("havok mopp generator cannot run on this platform")
nverts = c_int(len(vertices))
ntris = c_int(len(triangles))
pverts = (c_double * (3 * len(vertices)))(*chain(*vertices))
pverts = (c_float * (3 * len(vertices)))(*chain(*vertices))
ptris = (c_short * (3 * len(triangles)))(*chain(*triangles))
moppcodelen = _NifMopp.GenerateMoppCode(nverts, pverts, ntris, ptris)
if moppcodelen == -1:
raise RuntimeError("mopp generator failed")
raise RuntimeError("havok mopp generator failed")
pmoppcode = create_string_buffer(moppcodelen)
result = _NifMopp.RetrieveMoppCode(c_int(moppcodelen), pmoppcode)
if result != moppcodelen:
raise RuntimeError("retrieving mopp code failed")
raise RuntimeError("retrieving havok mopp code failed")
# convert c byte array to list of ints
moppcode = list(ord(char) for char in pmoppcode)

scale = c_double()
scale = c_float()
if not _NifMopp.RetrieveMoppScale(byref(scale)):
raise RuntimeError("retrieving mopp scale failed")
raise RuntimeError("retrieving havok mopp scale failed")
# convert c_double to Python float
scale = scale.value

porigin = (c_double * 3)()
porigin = (c_float * 3)()
if not _NifMopp.RetrieveMoppOrigin(porigin):
raise RuntimeError("retreiving mopp origin failed")
raise RuntimeError("retreiving havok mopp origin failed")
origin = tuple(value for value in porigin)
#_NifMopp.RetrieveOrigin
return scale, origin, moppcode
Expand All @@ -98,3 +107,4 @@ def getMoppScaleOriginCode(vertices, triangles):
if __name__ == "__main__":
import doctest
doctest.testmod()

0 comments on commit a142624

Please sign in to comment.