[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4677] trunk/py/scripts/addons/ io_scene_fbx/parse_fbx.py: add a utility function to return the fbx version number only ( dont parse the entire file)

Campbell Barton ideasman42 at gmail.com
Wed Aug 14 09:29:11 CEST 2013


Revision: 4677
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4677
Author:   campbellbarton
Date:     2013-08-14 07:29:11 +0000 (Wed, 14 Aug 2013)
Log Message:
-----------
add a utility function to return the fbx version number only (dont parse the entire file)

Modified Paths:
--------------
    trunk/py/scripts/addons/io_scene_fbx/parse_fbx.py

Modified: trunk/py/scripts/addons/io_scene_fbx/parse_fbx.py
===================================================================
--- trunk/py/scripts/addons/io_scene_fbx/parse_fbx.py	2013-08-13 07:52:54 UTC (rev 4676)
+++ trunk/py/scripts/addons/io_scene_fbx/parse_fbx.py	2013-08-14 07:29:11 UTC (rev 4677)
@@ -24,6 +24,7 @@
 __all__ = (
     "parse",
     "data_types",
+    "parse_version",
     "FBXElem",
     )
 
@@ -37,6 +38,7 @@
 _BLOCK_SENTINEL_LENGTH = 13
 _BLOCK_SENTINEL_DATA = (b'\0' * _BLOCK_SENTINEL_LENGTH)
 _IS_BIG_ENDIAN = (__import__("sys").byteorder != 'little')
+_HEAD_MAGIC = b'Kaydara FBX Binary\x20\x20\x00\x1a\x00'
 from collections import namedtuple
 FBXElem = namedtuple("FBXElem", ("id", "props", "props_type", "elems"))
 del namedtuple
@@ -129,18 +131,28 @@
     return FBXElem(*args) if use_namedtuple else args
 
 
+def parse_version(fn):
+    """
+    Return the FBX version,
+    if the file isn't a binary FBX return zero.
+    """
+    with open(fn, 'rb') as f:
+        read = f.read
+
+        if read(len(_HEAD_MAGIC)) != _HEAD_MAGIC:
+            return 0
+
+        return read_uint(read)
+
+
 def parse(fn, use_namedtuple=True):
-    # import time
-    # t = time.time()
-
     root_elems = []
 
     with open(fn, 'rb') as f:
         read = f.read
         tell = f.tell
 
-        HEAD_MAGIC = b'Kaydara FBX Binary\x20\x20\x00\x1a\x00'
-        if read(len(HEAD_MAGIC)) != HEAD_MAGIC:
+        if read(len(_HEAD_MAGIC)) != _HEAD_MAGIC:
             raise IOError("Invalid header")
 
         fbx_version = read_uint(read)
@@ -151,8 +163,6 @@
                 break
             root_elems.append(elem)
 
-    # print("done in %.4f sec" % (time.time() - t))
-
     args = (b'', [], bytearray(0), root_elems)
     return FBXElem(*args) if use_namedtuple else args, fbx_version
 



More information about the Bf-extensions-cvs mailing list