Skip to content

Commit beced4a

Browse files
authored
Merge pull request #556 from elpie89/calculate_animgroup_range
calculate "end frame" of animation group, animation group methods exposed to maxscript
2 parents 5cefe86 + 24d6104 commit beced4a

File tree

5 files changed

+233
-3
lines changed

5 files changed

+233
-3
lines changed

3ds Max/Max2Babylon/Exporter/AnimationGroup.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ public class AnimationGroupList : List<AnimationGroup>
256256
{
257257
const string s_AnimationListPropertyName = "babylonjs_AnimationList";
258258

259+
public AnimationGroup GetAnimationGroupByName(string name)
260+
{
261+
return this.First(animationGroup => animationGroup.Name == name);
262+
}
263+
259264
public void LoadFromData()
260265
{
261266
string[] animationPropertyNames = Loader.Core.RootNode.GetStringArrayProperty(s_AnimationListPropertyName);

3ds Max/Max2Babylon/Forms/AnimationGroupControl.Designer.cs

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

3ds Max/Max2Babylon/Forms/AnimationGroupControl.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ private void confirmButton_Click(object sender, EventArgs e)
157157
confirmedInfo.FrameStart = newFrameStart;
158158
confirmedInfo.FrameEnd = newFrameEnd;
159159

160-
if(nodesChanged)
160+
if (nodesChanged)
161+
{
161162
confirmedInfo.NodeHandles = newHandles;
163+
}
162164

163165
ResetChangedTextBoxColors();
164166
MaxNodeTree.SelectedNode = null;
@@ -203,5 +205,13 @@ private void removeNodeButton_Click(object sender, EventArgs e)
203205
}
204206
MaxNodeTree.EndUpdate();
205207
}
208+
209+
private void calculateTimeRangeBtn_Click(object sender, EventArgs e)
210+
{
211+
if (currentInfo == null)
212+
return;
213+
214+
endTextBox.Text = Tools.CalculateEndFrameFromAnimationGroupNodes(currentInfo).ToString();
215+
}
206216
}
207217
}

3ds Max/Max2Babylon/MaxScriptManager.cs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
5+
using System.Text.RegularExpressions;
46
using System.Windows.Forms;
7+
using Autodesk.Max;
58

69
namespace Max2Babylon
710
{
@@ -112,5 +115,164 @@ public static void MergeAnimationGroups(string jsonPath)
112115
}
113116
}
114117

118+
public static void MergeAnimationGroups(string jsonPath, string old_root, string new_root)
119+
{
120+
AnimationGroupList animationGroups = new AnimationGroupList();
121+
var fileStream = File.Open(jsonPath, FileMode.Open);
122+
123+
using (StreamReader reader = new StreamReader(fileStream))
124+
{
125+
string jsonContent = reader.ReadToEnd();
126+
string textToFind = string.Format(@"\b{0}\b", old_root);
127+
string overridedJsonContent = Regex.Replace(jsonContent, textToFind, new_root);
128+
animationGroups.LoadFromJson(overridedJsonContent, true);
129+
}
130+
}
131+
132+
public AnimationGroup GetAnimationGroupByName(string name)
133+
{
134+
AnimationGroupList animationGroupList = new AnimationGroupList();
135+
animationGroupList.LoadFromData();
136+
137+
foreach (AnimationGroup animationGroup in animationGroupList)
138+
{
139+
if (animationGroup.Name == name)
140+
{
141+
return animationGroup;
142+
}
143+
}
144+
145+
return null;
146+
}
147+
148+
public AnimationGroup CreateAnimationGroup()
149+
{
150+
AnimationGroupList animationGroupList = new AnimationGroupList();
151+
animationGroupList.LoadFromData();
152+
153+
AnimationGroup info = new AnimationGroup();
154+
155+
// get a unique name and guid
156+
string baseName = info.Name;
157+
int i = 0;
158+
bool hasConflict = true;
159+
while (hasConflict)
160+
{
161+
hasConflict = false;
162+
foreach (AnimationGroup animationGroup in animationGroupList)
163+
{
164+
if (info.Name.Equals(animationGroup.Name))
165+
{
166+
info.Name = baseName + i.ToString();
167+
++i;
168+
hasConflict = true;
169+
break;
170+
}
171+
if (info.SerializedId.Equals(animationGroup.SerializedId))
172+
{
173+
info.SerializedId = Guid.NewGuid();
174+
hasConflict = true;
175+
break;
176+
}
177+
}
178+
}
179+
180+
// save info and animation list entry
181+
animationGroupList.Add(info);
182+
animationGroupList.SaveToData();
183+
Loader.Global.SetSaveRequiredFlag(true, false);
184+
return info;
185+
}
186+
187+
public string RenameAnimationGroup(AnimationGroup info,string name)
188+
{
189+
AnimationGroupList animationGroupList = new AnimationGroupList();
190+
animationGroupList.LoadFromData();
191+
192+
AnimationGroup animGroupToRename = animationGroupList.GetAnimationGroupByName(info.Name);
193+
194+
string baseName = name;
195+
int i = 0;
196+
bool hasConflict = true;
197+
while (hasConflict)
198+
{
199+
hasConflict = false;
200+
foreach (AnimationGroup animationGroup in animationGroupList)
201+
{
202+
if (baseName.Equals(animationGroup.Name))
203+
{
204+
baseName = name + i.ToString();
205+
++i;
206+
hasConflict = true;
207+
break;
208+
}
209+
}
210+
}
211+
212+
animGroupToRename.Name = baseName;
213+
214+
// save info and animation list entry
215+
animationGroupList.SaveToData();
216+
Loader.Global.SetSaveRequiredFlag(true, false);
217+
return baseName;
218+
}
219+
220+
public void AddNodeInAnimationGroup(AnimationGroup info, uint nodeHandle)
221+
{
222+
if (info == null)
223+
return;
224+
225+
IINode node = Loader.Core.GetINodeByHandle(nodeHandle);
226+
if (node == null)
227+
{
228+
return;
229+
}
230+
231+
List<uint> newHandles = info.NodeHandles.ToList();
232+
newHandles.Add(nodeHandle);
233+
info.NodeHandles = newHandles;
234+
info.SaveToData();
235+
}
236+
237+
public void SetAnimationGroupTimeRange(AnimationGroup info, int start,int end)
238+
{
239+
if (info == null)
240+
return;
241+
242+
info.FrameStart = start;
243+
info.FrameEnd = end;
244+
info.SaveToData();
245+
}
246+
247+
public void RemoveAllNodeFromAnimationGroup(AnimationGroup info)
248+
{
249+
if (info == null)
250+
return;
251+
252+
info.NodeHandles = new List<uint>();
253+
info.SaveToData();
254+
}
255+
256+
public void RemoveNodeFromAnimationGroup(AnimationGroup info, uint nodeHandle)
257+
{
258+
if (info == null)
259+
return;
260+
261+
IINode node = Loader.Core.GetINodeByHandle(nodeHandle);
262+
if (node == null)
263+
{
264+
return;
265+
}
266+
267+
List<uint> newHandles = info.NodeHandles.ToList();
268+
newHandles.Remove(nodeHandle);
269+
info.NodeHandles = newHandles;
270+
info.SaveToData();
271+
}
272+
273+
274+
275+
276+
115277
}
116278
}

3ds Max/Max2Babylon/Tools/Tools.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,43 @@ public static bool DeleteProperty(this IINode node, string propertyName)
933933

934934
#endregion
935935

936+
#region AnimationGroup Helpers
937+
public static int CalculateEndFrameFromAnimationGroupNodes(AnimationGroup animationGroup)
938+
{
939+
int endFrame = 0;
940+
foreach (uint nodeHandle in animationGroup.NodeHandles)
941+
{
942+
IINode node = Loader.Core.GetINodeByHandle(nodeHandle);
943+
if (node.IsAnimated && node.TMController != null)
944+
{
945+
int lastKey = 0;
946+
if (node.TMController.PositionController != null)
947+
{
948+
int posKeys = node.TMController.PositionController.NumKeys;
949+
lastKey = Math.Max(lastKey, node.TMController.PositionController.GetKeyTime(posKeys - 1));
950+
}
951+
952+
if (node.TMController.RotationController != null)
953+
{
954+
int rotKeys = node.TMController.RotationController.NumKeys;
955+
lastKey = Math.Max(lastKey, node.TMController.RotationController.GetKeyTime(rotKeys - 1));
956+
}
957+
958+
if (node.TMController.ScaleController != null)
959+
{
960+
int scaleKeys = node.TMController.ScaleController.NumKeys;
961+
lastKey = Math.Max(lastKey, node.TMController.ScaleController.GetKeyTime(scaleKeys - 1));
962+
}
963+
decimal keyTime = Decimal.Ceiling(lastKey / 160);
964+
endFrame = Math.Max(endFrame, Decimal.ToInt32(keyTime));
965+
}
966+
}
967+
968+
return (endFrame!=0)? endFrame : animationGroup.FrameEnd;
969+
}
970+
#endregion
971+
972+
936973
#region Windows.Forms.Control Serialization
937974

938975
public static bool PrepareCheckBox(CheckBox checkBox, IINode node, string propertyName, int defaultState = 0)

0 commit comments

Comments
 (0)