Search in sources :

Example 26 with Structure

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

the class ConstraintHelper method loadConstraints.

/**
     * This method reads constraints for for the given structure. The
     * constraints are loaded only once for object/bone.
     * 
     * @param objectStructure
     *            the structure we read constraint's for
     * @param blenderContext
     *            the blender context
     * @throws BlenderFileException
     */
public void loadConstraints(Structure objectStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.fine("Loading constraints.");
    // reading influence ipos for the constraints
    AnimationHelper animationHelper = blenderContext.getHelper(AnimationHelper.class);
    Map<String, Map<String, Ipo>> constraintsIpos = new HashMap<String, Map<String, Ipo>>();
    Pointer pActions = (Pointer) objectStructure.getFieldValue("action");
    if (pActions.isNotNull()) {
        List<Structure> actions = pActions.fetchData();
        for (Structure action : actions) {
            Structure chanbase = (Structure) action.getFieldValue("chanbase");
            List<Structure> actionChannels = chanbase.evaluateListBase();
            for (Structure actionChannel : actionChannels) {
                Map<String, Ipo> ipos = new HashMap<String, Ipo>();
                Structure constChannels = (Structure) actionChannel.getFieldValue("constraintChannels");
                List<Structure> constraintChannels = constChannels.evaluateListBase();
                for (Structure constraintChannel : constraintChannels) {
                    Pointer pIpo = (Pointer) constraintChannel.getFieldValue("ipo");
                    if (pIpo.isNotNull()) {
                        String constraintName = constraintChannel.getFieldValue("name").toString();
                        Ipo ipo = animationHelper.fromIpoStructure(pIpo.fetchData().get(0), blenderContext);
                        ipos.put(constraintName, ipo);
                    }
                }
                String actionName = actionChannel.getFieldValue("name").toString();
                constraintsIpos.put(actionName, ipos);
            }
        }
    }
    // loading constraints connected with the object's bones
    Pointer pPose = (Pointer) objectStructure.getFieldValue("pose");
    if (pPose.isNotNull()) {
        List<Structure> poseChannels = ((Structure) pPose.fetchData().get(0).getFieldValue("chanbase")).evaluateListBase();
        for (Structure poseChannel : poseChannels) {
            List<Constraint> constraintsList = new ArrayList<Constraint>();
            Long boneOMA = Long.valueOf(((Pointer) poseChannel.getFieldValue("bone")).getOldMemoryAddress());
            // the name is read directly from structure because bone might
            // not yet be loaded
            String name = blenderContext.getFileBlock(boneOMA).getStructure(blenderContext).getFieldValue("name").toString();
            List<Structure> constraints = ((Structure) poseChannel.getFieldValue("constraints")).evaluateListBase();
            for (Structure constraint : constraints) {
                String constraintName = constraint.getFieldValue("name").toString();
                Map<String, Ipo> ipoMap = constraintsIpos.get(name);
                Ipo ipo = ipoMap == null ? null : ipoMap.get(constraintName);
                if (ipo == null) {
                    float enforce = ((Number) constraint.getFieldValue("enforce")).floatValue();
                    ipo = animationHelper.fromValue(enforce);
                }
                constraintsList.add(new BoneConstraint(constraint, boneOMA, ipo, blenderContext));
            }
            blenderContext.addConstraints(boneOMA, constraintsList);
        }
    }
    // loading constraints connected with the object itself
    List<Structure> constraints = ((Structure) objectStructure.getFieldValue("constraints")).evaluateListBase();
    if (constraints != null && constraints.size() > 0) {
        Pointer pData = (Pointer) objectStructure.getFieldValue("data");
        String dataType = pData.isNotNull() ? pData.fetchData().get(0).getType() : null;
        List<Constraint> constraintsList = new ArrayList<Constraint>(constraints.size());
        for (Structure constraint : constraints) {
            String constraintName = constraint.getFieldValue("name").toString();
            String objectName = objectStructure.getName();
            Map<String, Ipo> objectConstraintsIpos = constraintsIpos.get(objectName);
            Ipo ipo = objectConstraintsIpos != null ? objectConstraintsIpos.get(constraintName) : null;
            if (ipo == null) {
                float enforce = ((Number) constraint.getFieldValue("enforce")).floatValue();
                ipo = animationHelper.fromValue(enforce);
            }
            constraintsList.add(this.createConstraint(dataType, constraint, objectStructure.getOldMemoryAddress(), ipo, blenderContext));
        }
        blenderContext.addConstraints(objectStructure.getOldMemoryAddress(), constraintsList);
    }
}
Also used : AnimationHelper(com.jme3.scene.plugins.blender.animations.AnimationHelper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Ipo(com.jme3.scene.plugins.blender.animations.Ipo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with Structure

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

the class ConstraintDefinitionFactory method createConstraintDefinition.

/**
     * This method creates the constraint instance.
     * 
     * @param constraintStructure
     *            the constraint's structure (bConstraint clss in blender 2.49).
     *            If the value is null the NullConstraint is created.
     * @param blenderContext
     *            the blender context
     * @throws BlenderFileException
     *             this exception is thrown when the blender file is somehow
     *             corrupted
     */
public static ConstraintDefinition createConstraintDefinition(Structure constraintStructure, String constraintName, Long ownerOMA, BlenderContext blenderContext) throws BlenderFileException {
    if (constraintStructure == null) {
        return new ConstraintDefinitionNull(null, ownerOMA, blenderContext);
    }
    String constraintClassName = constraintStructure.getType();
    Class<? extends ConstraintDefinition> constraintDefinitionClass = CONSTRAINT_CLASSES.get(constraintClassName);
    if (constraintDefinitionClass != null) {
        try {
            ConstraintDefinition def = (ConstraintDefinition) constraintDefinitionClass.getDeclaredConstructors()[0].newInstance(constraintStructure, ownerOMA, blenderContext);
            def.setConstraintName(constraintName);
            return def;
        } catch (IllegalArgumentException e) {
            throw new BlenderFileException(e.getLocalizedMessage(), e);
        } catch (SecurityException e) {
            throw new BlenderFileException(e.getLocalizedMessage(), e);
        } catch (InstantiationException e) {
            throw new BlenderFileException(e.getLocalizedMessage(), e);
        } catch (IllegalAccessException e) {
            throw new BlenderFileException(e.getLocalizedMessage(), e);
        } catch (InvocationTargetException e) {
            throw new BlenderFileException(e.getLocalizedMessage(), e);
        }
    } else {
        String unsupportedConstraintClassName = UNSUPPORTED_CONSTRAINTS.get(constraintClassName);
        if (unsupportedConstraintClassName != null) {
            return new UnsupportedConstraintDefinition(unsupportedConstraintClassName);
        } else {
            throw new BlenderFileException("Unknown constraint type: " + constraintClassName);
        }
    }
}
Also used : BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 28 with Structure

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

the class AbstractBlenderHelper method loadProperties.

/**
     * This method loads the properties if they are available and defined for the structure.
     * @param structure
     *            the structure we read the properties from
     * @param blenderContext
     *            the blender context
     * @return loaded properties or null if they are not available
     * @throws BlenderFileException
     *             an exception is thrown when the blend file is somehow corrupted
     */
protected Properties loadProperties(Structure structure, BlenderContext blenderContext) throws BlenderFileException {
    Properties properties = null;
    Structure id = (Structure) structure.getFieldValue("ID");
    if (id != null) {
        Pointer pProperties = (Pointer) id.getFieldValue("properties");
        if (pProperties.isNotNull()) {
            Structure propertiesStructure = pProperties.fetchData().get(0);
            properties = new Properties();
            properties.load(propertiesStructure, blenderContext);
        }
    }
    return properties;
}
Also used : Pointer(com.jme3.scene.plugins.blender.file.Pointer) Properties(com.jme3.scene.plugins.blender.objects.Properties) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 29 with Structure

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

the class AbstractBlenderHelper method loadLibrary.

/**
     * The method loads library of a given ID from linked blender file.
     * @param id
     *            the ID of the linked feature (it contains its name and blender path)
     * @return loaded feature or null if none was found
     * @throws BlenderFileException
     *             and exception is throw when problems with reading a blend file occur
     */
protected Object loadLibrary(Structure id) throws BlenderFileException {
    Pointer pLib = (Pointer) id.getFieldValue("lib");
    if (pLib.isNotNull()) {
        // we need full name with the prefix
        String fullName = id.getFieldValue("name").toString();
        String nameOfFeatureToLoad = id.getName();
        Structure library = pLib.fetchData().get(0);
        String path = library.getFieldValue("filepath").toString();
        if (!blenderContext.getLinkedFeatures().keySet().contains(path)) {
            Spatial loadedAsset = null;
            BlenderKey blenderKey = new BlenderKey(path);
            blenderKey.setLoadUnlinkedAssets(true);
            try {
                loadedAsset = blenderContext.getAssetManager().loadAsset(blenderKey);
            } catch (AssetNotFoundException e) {
                LOGGER.log(Level.FINEST, "Cannot locate linked resource at path: {0}.", path);
            }
            if (loadedAsset != null) {
                Map<String, Map<String, Object>> linkedData = loadedAsset.getUserData("linkedData");
                for (Entry<String, Map<String, Object>> entry : linkedData.entrySet()) {
                    String linkedDataFilePath = "this".equals(entry.getKey()) ? path : entry.getKey();
                    blenderContext.getLinkedFeatures().put(linkedDataFilePath, entry.getValue());
                }
            } else {
                LOGGER.log(Level.WARNING, "No features loaded from path: {0}.", path);
            }
        }
        Object result = blenderContext.getLinkedFeature(path, fullName);
        if (result == null) {
            LOGGER.log(Level.WARNING, "Could NOT find asset named {0} in the library of path: {1}.", new Object[] { nameOfFeatureToLoad, path });
        } else {
            blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.STRUCTURE, id);
            blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.FEATURE, result);
        }
        return result;
    } else {
        LOGGER.warning("Library link points to nothing!");
    }
    return null;
}
Also used : Spatial(com.jme3.scene.Spatial) Pointer(com.jme3.scene.plugins.blender.file.Pointer) BlenderKey(com.jme3.asset.BlenderKey) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) Structure(com.jme3.scene.plugins.blender.file.Structure) Map(java.util.Map)

Example 30 with Structure

use of com.jme3.scene.plugins.blender.file.Structure 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)34 Pointer (com.jme3.scene.plugins.blender.file.Pointer)30 ArrayList (java.util.ArrayList)16 Vector3f (com.jme3.math.Vector3f)8 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)7 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)7 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)7 List (java.util.List)6 Map (java.util.Map)6 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)5 HashMap (java.util.HashMap)5 ColorRGBA (com.jme3.math.ColorRGBA)4 Node (com.jme3.scene.Node)4 Texture (com.jme3.texture.Texture)4 CameraNode (com.jme3.scene.CameraNode)3 LightNode (com.jme3.scene.LightNode)3 Spatial (com.jme3.scene.Spatial)3 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)3 ConstIpo (com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo)3