|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2024 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.gde.core.assets.actions; |
| 33 | + |
| 34 | +import com.jme3.anim.AnimClip; |
| 35 | +import com.jme3.anim.AnimComposer; |
| 36 | +import com.jme3.anim.AnimTrack; |
| 37 | +import com.jme3.anim.Armature; |
| 38 | +import com.jme3.anim.Joint; |
| 39 | +import com.jme3.anim.SkinningControl; |
| 40 | +import com.jme3.anim.TransformTrack; |
| 41 | +import com.jme3.anim.util.HasLocalTransform; |
| 42 | +import com.jme3.gde.core.assets.SpatialAssetDataObject; |
| 43 | +import com.jme3.scene.Node; |
| 44 | +import com.jme3.scene.Spatial; |
| 45 | +import com.jme3.scene.plugins.bvh.SkeletonMapping; |
| 46 | +import java.awt.event.ActionEvent; |
| 47 | +import java.awt.event.ActionListener; |
| 48 | +import java.io.IOException; |
| 49 | +import java.util.Collection; |
| 50 | +import java.util.Iterator; |
| 51 | +import java.util.List; |
| 52 | +import jme3utilities.MyAnimation; |
| 53 | +import jme3utilities.wes.AnimationEdit; |
| 54 | +import org.bushe.swing.event.Logger; |
| 55 | +import org.openide.DialogDisplayer; |
| 56 | +import org.openide.NotifyDescriptor; |
| 57 | +import org.openide.NotifyDescriptor.Confirmation; |
| 58 | +import org.openide.util.Exceptions; |
| 59 | + |
| 60 | +/** |
| 61 | + * Action for merging one or more spatials' animation to another. |
| 62 | + * Same rig required. |
| 63 | + * @author rickard |
| 64 | + */ |
| 65 | +public class MergeAnimationsAction implements ActionListener { |
| 66 | + |
| 67 | + private static final String CANCEL = "Cancel"; |
| 68 | + private final List<SpatialAssetDataObject> spatials; |
| 69 | + private final Logger logger; |
| 70 | + |
| 71 | + public MergeAnimationsAction(List<SpatialAssetDataObject> context) { |
| 72 | + this.spatials = context; |
| 73 | + logger = Logger.getLogger(MergeAnimationsAction.class.getName()); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void actionPerformed(ActionEvent e) { |
| 78 | + if (spatials.size() == 1) { |
| 79 | + DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( |
| 80 | + "Must select more than one spatial", |
| 81 | + NotifyDescriptor.ERROR_MESSAGE)); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + final Object selectedSpatial = createSelector().getValue(); |
| 86 | + |
| 87 | + if (selectedSpatial == null || selectedSpatial.equals(CANCEL)) { |
| 88 | + logger.log(Logger.Level.INFO, "Operation cancelled by user."); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + final SpatialAssetDataObject targetAsset = findSpatial(selectedSpatial.toString()); |
| 93 | + |
| 94 | + if (targetAsset == null) { |
| 95 | + logger.log(Logger.Level.INFO, "Operation failed. No spatial."); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + final Spatial targetSpatial = targetAsset.loadAsset(); |
| 100 | + final AnimComposer targetAnimComposer = findAnimComposer(targetSpatial, null); |
| 101 | + |
| 102 | + if (targetAnimComposer == null) { |
| 103 | + DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( |
| 104 | + String.format("%s has no AnimComposer.", targetSpatial), |
| 105 | + NotifyDescriptor.ERROR_MESSAGE)); |
| 106 | + return; |
| 107 | + } |
| 108 | + final Spatial targetAnimComposerSpatial = targetAnimComposer.getSpatial(); |
| 109 | + for (final Iterator<SpatialAssetDataObject> it = spatials.iterator(); it.hasNext();) { |
| 110 | + final SpatialAssetDataObject spatialAssetDataObject = it.next(); |
| 111 | + if(spatialAssetDataObject.getName().equals(selectedSpatial)) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + Spatial sourceSpatial = spatialAssetDataObject.loadAsset(); |
| 115 | + final AnimComposer sourceAnimComposer = findAnimComposer(sourceSpatial, null); |
| 116 | + if (sourceAnimComposer == null) { |
| 117 | + DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( |
| 118 | + String.format("%s has no AnimComposer.", sourceSpatial), |
| 119 | + NotifyDescriptor.ERROR_MESSAGE)); |
| 120 | + return; |
| 121 | + } |
| 122 | + copyClips(sourceAnimComposer, targetAnimComposer, targetAnimComposerSpatial.getControl(SkinningControl.class).getArmature()); |
| 123 | + } |
| 124 | + logger.log(Logger.Level.INFO, "Merging animations done. Saving."); |
| 125 | + try { |
| 126 | + targetAsset.saveAsset(); |
| 127 | + } catch (IOException ex) { |
| 128 | + Exceptions.printStackTrace(ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static AnimClip retargetClip( |
| 133 | + AnimClip clip, Armature armature, String clipName) { |
| 134 | + AnimTrack[] animTracks = clip.getTracks(); |
| 135 | + AnimClip result = new AnimClip(clipName); |
| 136 | + for (AnimTrack animTrack : animTracks) { |
| 137 | + if (MyAnimation.isJointTrack(animTrack)) { |
| 138 | + TransformTrack transformTrack = (TransformTrack) animTrack; |
| 139 | + HasLocalTransform animTarget = transformTrack.getTarget(); |
| 140 | + Joint animJoint = (Joint) animTarget; |
| 141 | + String jointName = animJoint.getName(); |
| 142 | + Joint charaJoint = armature.getJoint(jointName); |
| 143 | + if (charaJoint != null) { |
| 144 | + transformTrack.setTarget(charaJoint); |
| 145 | + AnimationEdit.addTrack(result, transformTrack); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return result; |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + private void copyClips(final AnimComposer from, final AnimComposer to, Armature toArmature) { |
| 154 | + final Collection<AnimClip> animClips = from.getAnimClips(); |
| 155 | + for(AnimClip animClip: animClips) { |
| 156 | + to.addAnimClip(retargetClip(animClip, toArmature, animClip.getName())); |
| 157 | + logger.log(Logger.Level.DEBUG, String.format("Added anim clip %s", animClip.getName())); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private Confirmation createSelector() { |
| 162 | + final Object[] spatials = new Object[this.spatials.size() + 1]; |
| 163 | + int index = 0; |
| 164 | + for (Iterator<SpatialAssetDataObject> it = this.spatials.iterator(); it.hasNext();) { |
| 165 | + final SpatialAssetDataObject spatialAssetDataObject = it.next(); |
| 166 | + spatials[index++] = spatialAssetDataObject.getName(); |
| 167 | + } |
| 168 | + spatials[index++] = CANCEL; |
| 169 | + final NotifyDescriptor.Confirmation message = |
| 170 | + new NotifyDescriptor.Confirmation( |
| 171 | + "Select spatial to copy animations to."); |
| 172 | + message.setOptions(spatials); |
| 173 | + |
| 174 | + DialogDisplayer.getDefault().notify(message); |
| 175 | + |
| 176 | + return message; |
| 177 | + } |
| 178 | + |
| 179 | + private AnimComposer findAnimComposer(Spatial spatial, AnimComposer animComposer) { |
| 180 | + if (spatial.getControl(AnimComposer.class) != null) { |
| 181 | + animComposer = spatial.getControl(AnimComposer.class); |
| 182 | + } |
| 183 | + if (animComposer == null && spatial instanceof Node node) { |
| 184 | + for (Spatial child: node.getChildren()) { |
| 185 | + animComposer = findAnimComposer(child, animComposer); |
| 186 | + if (animComposer != null) { |
| 187 | + return animComposer; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + return animComposer; |
| 192 | + } |
| 193 | + |
| 194 | + private SpatialAssetDataObject findSpatial(String selected) { |
| 195 | + for (Iterator<SpatialAssetDataObject> it = spatials.iterator(); it.hasNext();) { |
| 196 | + final SpatialAssetDataObject spatialAssetDataObject = it.next(); |
| 197 | + if(spatialAssetDataObject.getName().equals(selected)) { |
| 198 | + return spatialAssetDataObject; |
| 199 | + } |
| 200 | + } |
| 201 | + NotifyDescriptor.Message msg = new NotifyDescriptor.Message( |
| 202 | + "Main asset to copy to not found. This is likely an issue with the tool itself.", |
| 203 | + NotifyDescriptor.ERROR_MESSAGE); |
| 204 | + DialogDisplayer.getDefault().notify(msg); |
| 205 | + return null; |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments