Skip to content

Commit 1572302

Browse files
authored
Merge pull request #626 from neph1/merge_animations_actions
Merge animations actions
2 parents 900c5dd + b41846e commit 1572302

File tree

3 files changed

+248
-1
lines changed

3 files changed

+248
-1
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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.util.SafeArrayList;
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 java.util.logging.Level;
53+
import java.util.logging.Logger;
54+
import org.openide.DialogDisplayer;
55+
import org.openide.NotifyDescriptor;
56+
import org.openide.NotifyDescriptor.Confirmation;
57+
import org.openide.util.Exceptions;
58+
59+
/**
60+
* Action for merging one or more spatials' animation to another. Same rig
61+
* required.
62+
*
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(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(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(Level.INFO, "Merging animations done. Saving.");
125+
try {
126+
targetAsset.saveAsset();
127+
} catch (IOException ex) {
128+
Exceptions.printStackTrace(ex);
129+
}
130+
}
131+
132+
private void copyClips(final AnimComposer from, final AnimComposer to, Armature toArmature) {
133+
final Collection<AnimClip> animClips = from.getAnimClips();
134+
for (AnimClip animClip : animClips) {
135+
to.addAnimClip(retargetClip(animClip, toArmature, animClip.getName()));
136+
logger.log(Level.FINE, String.format("Added anim clip %s", animClip.getName()));
137+
}
138+
}
139+
140+
private Confirmation createSelector() {
141+
final Object[] spatials = new Object[this.spatials.size() + 1];
142+
int index = 0;
143+
for (Iterator<SpatialAssetDataObject> it = this.spatials.iterator(); it.hasNext();) {
144+
final SpatialAssetDataObject spatialAssetDataObject = it.next();
145+
spatials[index++] = spatialAssetDataObject.getName();
146+
}
147+
spatials[index++] = CANCEL;
148+
final NotifyDescriptor.Confirmation message
149+
= new NotifyDescriptor.Confirmation(
150+
"Select spatial to copy animations to.");
151+
message.setOptions(spatials);
152+
153+
DialogDisplayer.getDefault().notify(message);
154+
155+
return message;
156+
}
157+
158+
private AnimComposer findAnimComposer(Spatial spatial, AnimComposer animComposer) {
159+
if (spatial.getControl(AnimComposer.class) != null) {
160+
return spatial.getControl(AnimComposer.class);
161+
}
162+
if (animComposer == null && spatial instanceof Node node) {
163+
for (Spatial child : node.getChildren()) {
164+
animComposer = findAnimComposer(child, animComposer);
165+
if (animComposer != null) {
166+
return animComposer;
167+
}
168+
}
169+
}
170+
return animComposer;
171+
}
172+
173+
private SpatialAssetDataObject findSpatial(String selected) {
174+
for (Iterator<SpatialAssetDataObject> it = spatials.iterator(); it.hasNext();) {
175+
final SpatialAssetDataObject spatialAssetDataObject = it.next();
176+
if (spatialAssetDataObject.getName().equals(selected)) {
177+
return spatialAssetDataObject;
178+
}
179+
}
180+
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
181+
"Main asset to copy to not found. This is likely an issue with the tool itself.",
182+
NotifyDescriptor.ERROR_MESSAGE);
183+
DialogDisplayer.getDefault().notify(msg);
184+
return null;
185+
}
186+
187+
private AnimClip retargetClip(AnimClip sourceClip, Armature targetArmature, String clipName) {
188+
189+
// Create a list to hold the new tracks
190+
SafeArrayList<AnimTrack> tracks = new SafeArrayList<>(AnimTrack.class);
191+
192+
// Iterate through each track in the source clip
193+
for (AnimTrack animTrack : sourceClip.getTracks()) {
194+
195+
TransformTrack sourceTrack = (TransformTrack) animTrack;
196+
String targetName = getTargetName(sourceTrack.getTarget());
197+
if (targetName == null) {
198+
logger.log(Level.SEVERE, String.format("Unsupported target for: %s. Skipping.", animTrack));
199+
continue;
200+
}
201+
Joint target = targetArmature.getJoint(targetName);
202+
203+
if (target != null) {
204+
// Clone the source track and set the new target joint
205+
TransformTrack newTrack = sourceTrack.jmeClone();
206+
newTrack.setTarget(target);
207+
tracks.add(newTrack);
208+
209+
} else {
210+
logger.log(Level.WARNING, "Joint not found in the target Armature: {0}", targetName);
211+
}
212+
}
213+
214+
// Create a new animation clip with the specified name and set its tracks
215+
AnimClip newClip = new AnimClip(clipName);
216+
newClip.setTracks(tracks.getArray());
217+
218+
logger.log(Level.INFO, "Created new AnimClip {0} with {1} tracks out of {2} from the source clip",
219+
new Object[]{clipName, tracks.size(), sourceClip.getTracks().length});
220+
return newClip;
221+
}
222+
223+
private String getTargetName(final HasLocalTransform target) {
224+
if (target instanceof Node node) {
225+
return node.getName();
226+
}
227+
if (target instanceof Joint joint) {
228+
return joint.getName();
229+
}
230+
return null;
231+
}
232+
233+
}

jme3-scenecomposer/src/com/jme3/gde/scenecomposer/Bundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CTL_OpenSceneComposer=Edit in SceneComposer
44
CTL_SceneComposerAction=SceneComposer
55
CTL_SceneComposerTopComponent=SceneComposer Window
66
CTL_SomeAction=SomeAction
7+
CTL_MergeAnimationsAction=Merge Animations
78
HINT_SceneComposerTopComponent=This is a SceneComposer window
89
OpenIDE-Module-Display-Category=jMonkeyEngine
910
OpenIDE-Module-Long-Description=\

jme3-scenecomposer/src/com/jme3/gde/scenecomposer/layer.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
<attr name="selectionType" stringvalue="ANY"/>
3434
<attr name="type" stringvalue="com.jme3.gde.core.assets.SpatialAssetDataObject"/>
3535
</file>
36+
<file name="com-jme3-gde-core-assets-actions-MergeAnimationsAction.instance">
37+
<attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>
38+
<attr name="displayName" bundlevalue="com.jme3.gde.scenecomposer.Bundle#CTL_MergeAnimationsAction"/>
39+
<attr name="injectable" stringvalue="com.jme3.gde.core.assets.actions.MergeAnimationsAction"/>
40+
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>
41+
<attr name="noIconInMenu" boolvalue="false"/>
42+
<attr name="selectionType" stringvalue="ANY"/>
43+
<attr name="type" stringvalue="com.jme3.gde.core.assets.SpatialAssetDataObject"/>
44+
</file>
3645
</folder>
3746
<folder name="Window">
3847
<file name="com-jme3-gde-scenecomposer-SceneComposerAction.instance">
@@ -59,9 +68,13 @@
5968
<attr name="originalFile" stringvalue="Actions/SceneComposer/com-jme3-gde-scenecomposer-LinkSceneComposer.instance"/>
6069
<attr name="position" intvalue="30"/>
6170
</file>
71+
<file name="com-jme3-gde-core-assets-actions-animation-MergeAnimationsAction.shadow">
72+
<attr name="originalFile" stringvalue="Actions/SceneComposer/com-jme3-gde-core-assets-actions-MergeAnimationsAction.instance"/>
73+
<attr name="position" intvalue="40"/>
74+
</file>
6275
<file name="scenecomposer_sep-2.instance">
6376
<attr name="instanceClass" stringvalue="javax.swing.JSeparator"/>
64-
<attr name="position" intvalue="40"/>
77+
<attr name="position" intvalue="45"/>
6578
</file>
6679
</folder>
6780
</folder>

0 commit comments

Comments
 (0)