Browse Source

merge animations action

rickard 8 months ago
parent
commit
b149e55e95

+ 208 - 0
jme3-core/src/com/jme3/gde/core/assets/actions/MergeAnimationsAction.java

@@ -0,0 +1,208 @@
+/*
+ *  Copyright (c) 2009-2024 jMonkeyEngine
+ *  All rights reserved.
+ * 
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ * 
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ *  * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.core.assets.actions;
+
+import com.jme3.anim.AnimClip;
+import com.jme3.anim.AnimComposer;
+import com.jme3.anim.AnimTrack;
+import com.jme3.anim.Armature;
+import com.jme3.anim.Joint;
+import com.jme3.anim.SkinningControl;
+import com.jme3.anim.TransformTrack;
+import com.jme3.anim.util.HasLocalTransform;
+import com.jme3.gde.core.assets.SpatialAssetDataObject;
+import com.jme3.scene.Node;
+import com.jme3.scene.Spatial;
+import com.jme3.scene.plugins.bvh.SkeletonMapping;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import jme3utilities.MyAnimation;
+import jme3utilities.wes.AnimationEdit;
+import org.bushe.swing.event.Logger;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.NotifyDescriptor.Confirmation;
+import org.openide.util.Exceptions;
+
+/**
+ * Action for merging one or more spatials' animation to another.
+ * Same rig required.
+ * @author rickard
+ */
+public class MergeAnimationsAction implements ActionListener {
+    
+    private static final String CANCEL = "Cancel";
+    private final List<SpatialAssetDataObject> spatials;
+    private final Logger logger;
+
+    public MergeAnimationsAction(List<SpatialAssetDataObject> context) {
+        this.spatials = context;
+        logger = Logger.getLogger(MergeAnimationsAction.class.getName());
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        if (spatials.size() == 1) {
+            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
+                            "Must select more than one spatial",
+                            NotifyDescriptor.ERROR_MESSAGE));
+            return;
+        }
+        
+        final Object selectedSpatial = createSelector().getValue();
+        
+        if (selectedSpatial == null || selectedSpatial.equals(CANCEL)) {
+            logger.log(Logger.Level.INFO, "Operation cancelled by user.");
+            return;
+        }
+        
+        final SpatialAssetDataObject targetAsset = findSpatial(selectedSpatial.toString());
+        
+        if (targetAsset == null) {
+            logger.log(Logger.Level.INFO, "Operation failed. No spatial.");
+            return;
+        }
+        
+        final Spatial targetSpatial = targetAsset.loadAsset();
+        final AnimComposer targetAnimComposer = findAnimComposer(targetSpatial, null);
+        
+        if (targetAnimComposer == null) {
+            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
+                            String.format("%s has no AnimComposer.", targetSpatial),
+                            NotifyDescriptor.ERROR_MESSAGE));
+            return;
+        }
+        final Spatial targetAnimComposerSpatial = targetAnimComposer.getSpatial();
+        for (final Iterator<SpatialAssetDataObject> it = spatials.iterator(); it.hasNext();) {
+            final SpatialAssetDataObject spatialAssetDataObject = it.next();
+            if(spatialAssetDataObject.getName().equals(selectedSpatial)) {
+                continue;
+            }
+            Spatial sourceSpatial = spatialAssetDataObject.loadAsset();
+            final AnimComposer sourceAnimComposer = findAnimComposer(sourceSpatial, null);
+            if (sourceAnimComposer == null) {
+                DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
+                            String.format("%s has no AnimComposer.", sourceSpatial),
+                            NotifyDescriptor.ERROR_MESSAGE));
+                return;
+            }
+            copyClips(sourceAnimComposer, targetAnimComposer, targetAnimComposerSpatial.getControl(SkinningControl.class).getArmature());
+        }
+        logger.log(Logger.Level.INFO, "Merging animations done. Saving.");
+        try {
+            targetAsset.saveAsset();
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+    }
+    
+    private static AnimClip retargetClip(
+            AnimClip clip, Armature armature, String clipName) {
+        AnimTrack[] animTracks = clip.getTracks();
+        AnimClip result = new AnimClip(clipName);
+        for (AnimTrack animTrack : animTracks) {
+            if (MyAnimation.isJointTrack(animTrack)) {
+                TransformTrack transformTrack = (TransformTrack) animTrack;
+                HasLocalTransform animTarget = transformTrack.getTarget();
+                Joint animJoint = (Joint) animTarget;
+                String jointName = animJoint.getName();
+                Joint charaJoint = armature.getJoint(jointName);
+                if (charaJoint != null) {
+                    transformTrack.setTarget(charaJoint);
+                    AnimationEdit.addTrack(result, transformTrack);
+                }
+            }
+        }
+        return result;
+    }
+
+    
+    private void copyClips(final AnimComposer from, final AnimComposer to, Armature toArmature) {
+        final Collection<AnimClip> animClips = from.getAnimClips();
+        for(AnimClip animClip: animClips) {
+            to.addAnimClip(retargetClip(animClip, toArmature, animClip.getName()));
+            logger.log(Logger.Level.DEBUG, String.format("Added anim clip %s", animClip.getName()));
+        }
+    }
+
+    private Confirmation createSelector() {
+        final Object[] spatials = new Object[this.spatials.size() + 1];
+        int index = 0;
+        for (Iterator<SpatialAssetDataObject> it = this.spatials.iterator(); it.hasNext();) {
+            final SpatialAssetDataObject spatialAssetDataObject = it.next();
+            spatials[index++] = spatialAssetDataObject.getName();
+        }
+        spatials[index++] = CANCEL;
+        final NotifyDescriptor.Confirmation message =
+                new NotifyDescriptor.Confirmation(
+                        "Select spatial to copy animations to.");
+        message.setOptions(spatials);
+        
+        DialogDisplayer.getDefault().notify(message);
+        
+        return message;
+    }
+    
+    private AnimComposer findAnimComposer(Spatial spatial, AnimComposer animComposer) {
+        if (spatial.getControl(AnimComposer.class) != null) {
+            animComposer = spatial.getControl(AnimComposer.class);
+        }
+        if (animComposer == null && spatial instanceof Node node) {
+            for (Spatial child: node.getChildren()) {
+                animComposer = findAnimComposer(child, animComposer);
+                if (animComposer != null) {
+                    return animComposer;
+                }
+            }
+        }
+        return animComposer;
+    }
+    
+    private SpatialAssetDataObject findSpatial(String selected) {
+        for (Iterator<SpatialAssetDataObject> it = spatials.iterator(); it.hasNext();) {
+            final SpatialAssetDataObject spatialAssetDataObject = it.next();
+            if(spatialAssetDataObject.getName().equals(selected)) {
+                return spatialAssetDataObject;
+            }
+        }
+        NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
+            "Main asset to copy to not found. This is likely an issue with the tool itself.",
+            NotifyDescriptor.ERROR_MESSAGE);
+        DialogDisplayer.getDefault().notify(msg);
+        return null;
+    }
+    
+}

+ 1 - 0
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/Bundle.properties

@@ -4,6 +4,7 @@ CTL_OpenSceneComposer=Edit in SceneComposer
 CTL_SceneComposerAction=SceneComposer
 CTL_SceneComposerAction=SceneComposer
 CTL_SceneComposerTopComponent=SceneComposer Window
 CTL_SceneComposerTopComponent=SceneComposer Window
 CTL_SomeAction=SomeAction
 CTL_SomeAction=SomeAction
+CTL_MergeAnimationsAction=Merge Animations
 HINT_SceneComposerTopComponent=This is a SceneComposer window
 HINT_SceneComposerTopComponent=This is a SceneComposer window
 OpenIDE-Module-Display-Category=jMonkeyEngine
 OpenIDE-Module-Display-Category=jMonkeyEngine
 OpenIDE-Module-Long-Description=\
 OpenIDE-Module-Long-Description=\

+ 14 - 1
jme3-scenecomposer/src/com/jme3/gde/scenecomposer/layer.xml

@@ -33,6 +33,15 @@
                 <attr name="selectionType" stringvalue="ANY"/>
                 <attr name="selectionType" stringvalue="ANY"/>
                 <attr name="type" stringvalue="com.jme3.gde.core.assets.SpatialAssetDataObject"/>
                 <attr name="type" stringvalue="com.jme3.gde.core.assets.SpatialAssetDataObject"/>
             </file>
             </file>
+            <file name="com-jme3-gde-core-assets-actions-MergeAnimationsAction.instance">
+                <attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>
+                <attr name="displayName" bundlevalue="com.jme3.gde.scenecomposer.Bundle#CTL_MergeAnimationsAction"/>
+                <attr name="injectable" stringvalue="com.jme3.gde.core.assets.actions.MergeAnimationsAction"/>
+                <attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>
+                <attr name="noIconInMenu" boolvalue="false"/>
+                <attr name="selectionType" stringvalue="ANY"/>
+                <attr name="type" stringvalue="com.jme3.gde.core.assets.SpatialAssetDataObject"/>
+            </file>
         </folder>
         </folder>
         <folder name="Window">
         <folder name="Window">
             <file name="com-jme3-gde-scenecomposer-SceneComposerAction.instance">
             <file name="com-jme3-gde-scenecomposer-SceneComposerAction.instance">
@@ -59,9 +68,13 @@
                         <attr name="originalFile" stringvalue="Actions/SceneComposer/com-jme3-gde-scenecomposer-LinkSceneComposer.instance"/>
                         <attr name="originalFile" stringvalue="Actions/SceneComposer/com-jme3-gde-scenecomposer-LinkSceneComposer.instance"/>
                         <attr name="position" intvalue="30"/>
                         <attr name="position" intvalue="30"/>
                     </file>
                     </file>
+                    <file name="com-jme3-gde-core-assets-actions-animation-MergeAnimationsAction.shadow">
+                        <attr name="originalFile" stringvalue="Actions/SceneComposer/com-jme3-gde-core-assets-actions-MergeAnimationsAction.instance"/>
+                        <attr name="position" intvalue="40"/>
+                    </file>
                     <file name="scenecomposer_sep-2.instance">
                     <file name="scenecomposer_sep-2.instance">
                         <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
                         <attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
-                        <attr name="position" intvalue="40"/>
+                        <attr name="position" intvalue="45"/>
                     </file>
                     </file>
                 </folder>
                 </folder>
             </folder>
             </folder>