Search in sources :

Example 36 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class IndexesLoop method findPath.

/**
     * The method finds the path between the given indexes.
     * @param start
     *            the start index
     * @param end
     *            the end index
     * @param result
     *            a list containing indexes on the path from start to end (inclusive)
     * @throws IllegalStateException
     *             an exception is thrown when the loop is not normalized (at least one
     *             index has more than 2 neighbours)
     * @throws BlenderFileException
     *             an exception is thrown if the vertices of a face create more than one loop; this is thrown
     *             to prevent lack of memory errors during triangulation
     */
public void findPath(Integer start, Integer end, List<Integer> result) throws BlenderFileException {
    result.clear();
    Integer node = start;
    while (!node.equals(end)) {
        if (result.contains(node)) {
            throw new BlenderFileException("Indexes of face have infinite loops!");
        }
        result.add(node);
        List<Integer> nextSteps = edges.get(node);
        if (nextSteps == null || nextSteps.size() == 0) {
            // no directed path from start to end
            result.clear();
            return;
        } else if (nextSteps.size() == 1) {
            node = nextSteps.get(0);
        } else {
            throw new BlenderFileException("Triangulation failed. Face has ambiguous indexes loop. Please triangulate your model in Blender as a workaround.");
        }
    }
    result.add(end);
}
Also used : BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException)

Example 37 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class MeshHelper method loadVerticesGroups.

/**
     * Loads all vertices groups.
     * @param meshStructure
     *            the mesh structure
     * @return a list of vertex groups for every vertex in the mesh
     * @throws BlenderFileException
     *             an exception is thrown when problems with blend file occur
     */
public List<Map<String, Float>> loadVerticesGroups(Structure meshStructure) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading vertices groups from mesh: {0}.", meshStructure.getName());
    List<Map<String, Float>> result = new ArrayList<Map<String, Float>>();
    Structure parent = blenderContext.peekParent();
    if (parent != null) {
        // the mesh might be saved without its parent (it is then unused)
        Structure defbase = (Structure) parent.getFieldValue("defbase");
        List<String> groupNames = new ArrayList<String>();
        List<Structure> defs = defbase.evaluateListBase();
        for (Structure def : defs) {
            groupNames.add(def.getFieldValue("name").toString());
        }
        // dvert = DeformVERTices
        Pointer pDvert = (Pointer) meshStructure.getFieldValue("dvert");
        if (pDvert.isNotNull()) {
            // assigning weights and bone indices
            List<Structure> dverts = pDvert.fetchData();
            for (Structure dvert : dverts) {
                Map<String, Float> weightsForVertex = new HashMap<String, Float>();
                Pointer pDW = (Pointer) dvert.getFieldValue("dw");
                if (pDW.isNotNull()) {
                    List<Structure> dw = pDW.fetchData();
                    for (Structure deformWeight : dw) {
                        int groupIndex = ((Number) deformWeight.getFieldValue("def_nr")).intValue();
                        float weight = ((Number) deformWeight.getFieldValue("weight")).floatValue();
                        String groupName = groupNames.get(groupIndex);
                        weightsForVertex.put(groupName, weight);
                    }
                }
                result.add(weightsForVertex);
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 38 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class Edge method loadAll.

/**
     * The method loads all edges from the given mesh structure that does not belong to any face.
     * @param meshStructure
     *            the mesh structure
     * @param temporalMesh
     *            the owner of the edges
     * @return all edges without faces
     * @throws BlenderFileException
     *             an exception is thrown when problems with file reading occur
     */
public static List<Edge> loadAll(Structure meshStructure, TemporalMesh temporalMesh) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading all edges that do not belong to any face from mesh: {0}", meshStructure.getName());
    List<Edge> result = new ArrayList<Edge>();
    Pointer pMEdge = (Pointer) meshStructure.getFieldValue("medge");
    if (pMEdge.isNotNull()) {
        List<Structure> edges = pMEdge.fetchData();
        for (Structure edge : edges) {
            int flag = ((Number) edge.getFieldValue("flag")).intValue();
            int v1 = ((Number) edge.getFieldValue("v1")).intValue();
            int v2 = ((Number) edge.getFieldValue("v2")).intValue();
            // I do not know why, but blender stores (possibly only sometimes) crease as negative values and shows positive in the editor
            float crease = Math.abs(((Number) edge.getFieldValue("crease")).floatValue());
            boolean edgeInFace = (flag & Edge.FLAG_EDGE_NOT_IN_FACE) == 0;
            result.add(new Edge(v1, v2, crease, edgeInFace, temporalMesh));
        }
    }
    LOGGER.log(Level.FINE, "Loaded {0} edges.", result.size());
    return result;
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 39 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class BlenderLoader method setup.

/**
     * This method sets up the loader.
     * @param assetInfo
     *            the asset info
     * @throws BlenderFileException
     *             an exception is throw when something wrong happens with blender file
     */
protected BlenderContext setup(AssetInfo assetInfo) throws BlenderFileException {
    // registering loaders
    ModelKey modelKey = (ModelKey) assetInfo.getKey();
    BlenderKey blenderKey;
    if (modelKey instanceof BlenderKey) {
        blenderKey = (BlenderKey) modelKey;
    } else {
        blenderKey = new BlenderKey(modelKey.getName());
    }
    // opening stream
    BlenderInputStream inputStream = new BlenderInputStream(assetInfo.openStream());
    // reading blocks
    List<FileBlockHeader> blocks = new ArrayList<FileBlockHeader>();
    FileBlockHeader fileBlock;
    BlenderContext blenderContext = new BlenderContext();
    blenderContext.setBlenderVersion(inputStream.getVersionNumber());
    blenderContext.setAssetManager(assetInfo.getManager());
    blenderContext.setInputStream(inputStream);
    blenderContext.setBlenderKey(blenderKey);
    // creating helpers
    blenderContext.putHelper(AnimationHelper.class, new AnimationHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(TextureHelper.class, new TextureHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MeshHelper.class, new MeshHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ObjectHelper.class, new ObjectHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CurvesHelper.class, new CurvesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LightHelper.class, new LightHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(CameraHelper.class, new CameraHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ModifierHelper.class, new ModifierHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(MaterialHelper.class, new MaterialHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ConstraintHelper.class, new ConstraintHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(ParticlesHelper.class, new ParticlesHelper(inputStream.getVersionNumber(), blenderContext));
    blenderContext.putHelper(LandscapeHelper.class, new LandscapeHelper(inputStream.getVersionNumber(), blenderContext));
    // reading the blocks (dna block is automatically saved in the blender context when found)
    FileBlockHeader sceneFileBlock = null;
    do {
        fileBlock = new FileBlockHeader(inputStream, blenderContext);
        if (!fileBlock.isDnaBlock()) {
            blocks.add(fileBlock);
            // save the scene's file block
            if (fileBlock.getCode() == BlockCode.BLOCK_SC00) {
                sceneFileBlock = fileBlock;
            }
        }
    } while (!fileBlock.isLastBlock());
    if (sceneFileBlock != null) {
        blenderContext.setSceneStructure(sceneFileBlock.getStructure(blenderContext));
    }
    // adding locator for linked content
    assetInfo.getManager().registerLocator(assetInfo.getKey().getName(), LinkedContentLocator.class);
    return blenderContext;
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) ModelKey(com.jme3.asset.ModelKey) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) ArrayList(java.util.ArrayList) ConstraintHelper(com.jme3.scene.plugins.blender.constraints.ConstraintHelper) ModifierHelper(com.jme3.scene.plugins.blender.modifiers.ModifierHelper) LandscapeHelper(com.jme3.scene.plugins.blender.landscape.LandscapeHelper) CurvesHelper(com.jme3.scene.plugins.blender.curves.CurvesHelper) TextureHelper(com.jme3.scene.plugins.blender.textures.TextureHelper) MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper) BlenderKey(com.jme3.asset.BlenderKey) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) ParticlesHelper(com.jme3.scene.plugins.blender.particles.ParticlesHelper) LightHelper(com.jme3.scene.plugins.blender.lights.LightHelper) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper) CameraHelper(com.jme3.scene.plugins.blender.cameras.CameraHelper)

Example 40 with BlenderFileException

use of com.jme3.scene.plugins.blender.file.BlenderFileException in project jmonkeyengine by jMonkeyEngine.

the class AnimationHelper method getTracks249.

/**
     * This method retuns the bone tracks for animation for blender version 2.49
     * (and probably several lower versions too).
     * 
     * @param actionStructure
     *            the structure containing the tracks
     * @param blenderContext
     *            the blender context
     * @return a list of tracks for the specified animation
     * @throws BlenderFileException
     *             an exception is thrown when there are problems with the blend
     *             file
     */
private BlenderAction getTracks249(Structure actionStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Getting tracks!");
    Structure chanbase = (Structure) actionStructure.getFieldValue("chanbase");
    // bActionChannel
    List<Structure> actionChannels = chanbase.evaluateListBase();
    BlenderAction blenderAction = new BlenderAction(actionStructure.getName(), blenderContext.getBlenderKey().getFps());
    int lastFrame = 1;
    for (Structure bActionChannel : actionChannels) {
        String animatedFeatureName = bActionChannel.getFieldValue("name").toString();
        Pointer p = (Pointer) bActionChannel.getFieldValue("ipo");
        if (!p.isNull()) {
            Structure ipoStructure = p.fetchData().get(0);
            Ipo ipo = this.fromIpoStructure(ipoStructure, blenderContext);
            if (ipo != null) {
                // this can happen when ipo with no curves appear in blender file
                lastFrame = Math.max(lastFrame, ipo.getLastFrame());
                blenderAction.featuresTracks.put(animatedFeatureName, ipo);
            }
        }
    }
    blenderAction.stopFrame = lastFrame;
    return blenderAction;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Aggregations

Structure (com.jme3.scene.plugins.blender.file.Structure)31 Pointer (com.jme3.scene.plugins.blender.file.Pointer)26 ArrayList (java.util.ArrayList)19 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)10 Vector3f (com.jme3.math.Vector3f)7 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)7 List (java.util.List)7 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)6 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 Map (java.util.Map)5 Node (com.jme3.scene.Node)4 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)4 MeshHelper (com.jme3.scene.plugins.blender.meshes.MeshHelper)4 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)4 Texture (com.jme3.texture.Texture)4 HashMap (java.util.HashMap)4 BlenderKey (com.jme3.asset.BlenderKey)3 Light (com.jme3.light.Light)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Spline (com.jme3.math.Spline)3