diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxDump.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxDump.java index 718a18b0a..00619dce7 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxDump.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxDump.java @@ -37,6 +37,8 @@ import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Quick n' dirty dumper of FBX binary files. @@ -45,12 +47,14 @@ import java.util.Map; * * @author Kirill Vainer */ -public final class FBXDump { +public final class FbxDump { + + private static final Logger logger = Logger.getLogger(FbxDump.class.getName()); private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.0000000000"); - private FBXDump() { } + private FbxDump() { } /** * Creates a map between object UIDs and the objects themselves. @@ -58,16 +62,17 @@ public final class FBXDump { * @param file The file to create the mappings for. * @return The UID to object map. */ - private static Map createUidToObjectMap(FBXFile file) { - Map uidToObjectMap = new HashMap(); - for (FBXElement rootElement : file.rootElements) { + private static Map createUidToObjectMap(FbxFile file) { + Map uidToObjectMap = new HashMap(); + for (FbxElement rootElement : file.rootElements) { if (rootElement.id.equals("Objects")) { - for (FBXElement fbxObj : rootElement.children) { - if (fbxObj.propertiesTypes[0] != 'L') { - continue; // error + for (FbxElement fbxObj : rootElement.children) { + FbxId uid = FbxId.getObjectId(fbxObj); + if (uid != null) { + uidToObjectMap.put(uid, fbxObj); + } else { + logger.log(Level.WARNING, "Cannot determine ID for object: {0}", fbxObj); } - Long uid = (Long) fbxObj.properties.get(0); - uidToObjectMap.put(uid, fbxObj); } } } @@ -79,8 +84,8 @@ public final class FBXDump { * * @param file the file to dump. */ - public static void dumpFBX(FBXFile file) { - dumpFBX(file, System.out); + public static void dumpFile(FbxFile file) { + dumpFile(file, System.out); } /** @@ -89,11 +94,11 @@ public final class FBXDump { * @param file the file to dump. * @param out the output stream where to output. */ - public static void dumpFBX(FBXFile file, OutputStream out) { - Map uidToObjectMap = createUidToObjectMap(file); + public static void dumpFile(FbxFile file, OutputStream out) { + Map uidToObjectMap = createUidToObjectMap(file); PrintStream ps = new PrintStream(out); - for (FBXElement rootElement : file.rootElements) { - dumpFBXElement(rootElement, ps, 0, uidToObjectMap); + for (FbxElement rootElement : file.rootElements) { + dumpElement(rootElement, ps, 0, uidToObjectMap); } } @@ -112,9 +117,9 @@ public final class FBXDump { return string.replaceAll("\u0000\u0001", "::"); } - protected static void dumpFBXProperty(String id, char propertyType, + protected static void dumpProperty(String id, char propertyType, Object property, PrintStream ps, - Map uidToObjectMap) { + Map uidToObjectMap) { switch (propertyType) { case 'S': // String @@ -124,13 +129,19 @@ public final class FBXDump { case 'R': // RAW data. byte[] bytes = (byte[]) property; - ps.print("["); - for (int j = 0; j < bytes.length; j++) { + int numToPrint = Math.min(10 * 1024, bytes.length); + ps.print("(size = "); + ps.print(bytes.length); + ps.print(") ["); + for (int j = 0; j < numToPrint; j++) { ps.print(String.format("%02X", bytes[j] & 0xff)); if (j != bytes.length - 1) { ps.print(" "); } } + if (numToPrint < bytes.length) { + ps.print(" ..."); + } ps.print("]"); break; case 'D': @@ -158,7 +169,7 @@ public final class FBXDump { // If this is a connection, decode UID into object name. if (id.equals("C")) { Long uid = (Long) property; - FBXElement element = uidToObjectMap.get(uid); + FbxElement element = uidToObjectMap.get(FbxId.create(uid)); if (element != null) { String name = (String) element.properties.get(1); ps.print("\"" + convertFBXString(name) + "\""); @@ -177,7 +188,7 @@ public final class FBXDump { int length = Array.getLength(property); for (int j = 0; j < length; j++) { Object arrayEntry = Array.get(property, j); - dumpFBXProperty(id, Character.toUpperCase(propertyType), arrayEntry, ps, uidToObjectMap); + dumpProperty(id, Character.toUpperCase(propertyType), arrayEntry, ps, uidToObjectMap); if (j != length - 1) { ps.print(","); } @@ -188,24 +199,24 @@ public final class FBXDump { } } - protected static void dumpFBXElement(FBXElement el, PrintStream ps, - int indent, Map uidToObjectMap) { + protected static void dumpElement(FbxElement el, PrintStream ps, + int indent, Map uidToObjectMap) { // 4 spaces per tab should be OK. String indentStr = indent(indent * 4); String textId = el.id; // Properties are called 'P' and connections are called 'C'. - if (el.id.equals("P")) { - textId = "Property"; - } else if (el.id.equals("C")) { - textId = "Connect"; - } +// if (el.id.equals("P")) { +// textId = "Property"; +// } else if (el.id.equals("C")) { +// textId = "Connect"; +// } ps.print(indentStr + textId + ": "); for (int i = 0; i < el.properties.size(); i++) { Object property = el.properties.get(i); char propertyType = el.propertiesTypes[i]; - dumpFBXProperty(el.id, propertyType, property, ps, uidToObjectMap); + dumpProperty(el.id, propertyType, property, ps, uidToObjectMap); if (i != el.properties.size() - 1) { ps.print(", "); } @@ -214,8 +225,8 @@ public final class FBXDump { ps.println(); } else { ps.println(" {"); - for (FBXElement childElement : el.children) { - dumpFBXElement(childElement, ps, indent + 1, uidToObjectMap); + for (FbxElement childElement : el.children) { + dumpElement(childElement, ps, indent + 1, uidToObjectMap); } ps.println(indentStr + "}"); } diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxElement.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxElement.java index eeeee872c..1a4d09d53 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxElement.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxElement.java @@ -34,7 +34,7 @@ package com.jme3.scene.plugins.fbx.file; import java.util.ArrayList; import java.util.List; -public class FBXElement { +public class FbxElement { public String id; public List properties; @@ -55,10 +55,70 @@ public class FBXElement { * c - array of unsigned bytes (represented as array of ints) */ public char[] propertiesTypes; - public List children = new ArrayList(); + public List children = new ArrayList(); - public FBXElement(int propsCount) { - properties = new ArrayList(propsCount); - propertiesTypes = new char[propsCount]; + public FbxElement(int propsCount) { + this.properties = new ArrayList(propsCount); + this.propertiesTypes = new char[propsCount]; } + + public FbxElement getChildById(String name) { + for (FbxElement element : children) { + if (element.id.equals(name)) { + return element; + } + } + return null; + } + + public List getFbxProperties() { + List props = new ArrayList(); + FbxElement propsElement = null; + boolean legacy = false; + + for (FbxElement element : children) { + if (element.id.equals("Properties70")) { + propsElement = element; + break; + } else if (element.id.equals("Properties60")) { + legacy = true; + propsElement = element; + break; + } + } + + if (propsElement == null) { + return props; + } + + for (FbxElement prop : propsElement.children) { + if (prop.id.equals("P") || prop.id.equals("Property")) { + if (legacy) { + char[] types = new char[prop.propertiesTypes.length + 1]; + types[0] = prop.propertiesTypes[0]; + types[1] = prop.propertiesTypes[0]; + System.arraycopy(prop.propertiesTypes, 1, types, 2, types.length - 2); + + List values = new ArrayList(prop.properties); + values.add(1, values.get(0)); + + FbxElement dummyProp = new FbxElement(types.length); + dummyProp.children = prop.children; + dummyProp.id = prop.id; + dummyProp.propertiesTypes = types; + dummyProp.properties = values; + props.add(dummyProp); + } else { + props.add(prop); + } + } + } + + return props; + } + + @Override + public String toString() { + return "FBXElement[id=" + id + ", numProps=" + properties.size() + ", numChildren=" + children.size() + "]"; + } } diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxFile.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxFile.java index ba81f1a74..9435b4c4e 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxFile.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxFile.java @@ -34,9 +34,13 @@ package com.jme3.scene.plugins.fbx.file; import java.util.ArrayList; import java.util.List; -public class FBXFile { +public class FbxFile { - public List rootElements = new ArrayList(); + public List rootElements = new ArrayList(); public long version; + @Override + public String toString() { + return "FBXFile[version=" + version + ",numElements=" + rootElements.size() + "]"; + } } diff --git a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxReader.java b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxReader.java index b39dfef1c..22e93d8db 100644 --- a/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxReader.java +++ b/jme3-plugins/src/fbx/java/com/jme3/scene/plugins/fbx/file/FbxReader.java @@ -40,8 +40,8 @@ import java.nio.ByteOrder; import java.util.Arrays; import java.util.zip.InflaterInputStream; -public class FBXReader { - +public class FbxReader { + public static final int BLOCK_SENTINEL_LENGTH = 13; public static final byte[] BLOCK_SENTINEL_DATA = new byte[BLOCK_SENTINEL_LENGTH]; /** @@ -49,9 +49,9 @@ public class FBXReader { * "Kaydara FBX Binary\x20\x20\x00\x1a\x00" */ public static final byte[] HEAD_MAGIC = new byte[]{0x4b, 0x61, 0x79, 0x64, 0x61, 0x72, 0x61, 0x20, 0x46, 0x42, 0x58, 0x20, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x20, 0x00, 0x1a, 0x00}; - - public static FBXFile readFBX(InputStream stream) throws IOException { - FBXFile fbxFile = new FBXFile(); + + public static FbxFile readFBX(InputStream stream) throws IOException { + FbxFile fbxFile = new FbxFile(); // Read file to byte buffer so we can know current position in file ByteBuffer byteBuffer = readToByteBuffer(stream); try { @@ -61,27 +61,29 @@ public class FBXReader { // Check majic header byte[] majic = getBytes(byteBuffer, HEAD_MAGIC.length); if(!Arrays.equals(HEAD_MAGIC, majic)) - throw new IOException("Not FBX file"); + throw new IOException("Either ASCII FBX or corrupt file. " + + "Only binary FBX files are supported"); + // Read version fbxFile.version = getUInt(byteBuffer); // Read root elements while(true) { - FBXElement e = readFBXElement(byteBuffer); + FbxElement e = readFBXElement(byteBuffer); if(e == null) break; fbxFile.rootElements.add(e); } return fbxFile; } - - private static FBXElement readFBXElement(ByteBuffer byteBuffer) throws IOException { + + private static FbxElement readFBXElement(ByteBuffer byteBuffer) throws IOException { long endOffset = getUInt(byteBuffer); if(endOffset == 0) return null; long propCount = getUInt(byteBuffer); getUInt(byteBuffer); // Properties length unused - FBXElement element = new FBXElement((int) propCount); + FbxElement element = new FbxElement((int) propCount); element.id = new String(getBytes(byteBuffer, getUByte(byteBuffer))); for(int i = 0; i < propCount; ++i) {