-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializableScene.cs
247 lines (218 loc) · 6.91 KB
/
SerializableScene.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Copyright 2012 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace MaxSharp
{
/// <summary>
/// This presents a snapshot representation of a scene that can be
/// easily serialized, or passed across process boundaries using
/// the Remoting interface.
/// </summary>
[Serializable]
public class SerializableScene
{
/// <summary>
/// Serializable representation of a reference to a target.
/// </summary>
[Serializable]
public class Reference
{
public ulong id;
public Reference()
{
}
public Reference(ReferenceTarget rt)
{
if (rt == null || !rt.Valid)
{
id = 0;
}
else
{
this.id = (ulong)rt.AnimHandle;
}
}
}
/// <summary>
/// Serializable representation of an animatable. In other words
/// something that can be targeted.
/// </summary>
public class Target
{
public ulong id;
public Target()
{
id = 0;
}
public Target(Animatable a)
{
id = (ulong)a.AnimHandle;
}
}
/// <summary>
/// Serializalbe represdentation of a plug-in.
/// </summary>
[Serializable]
public class Plugin : Target
{
public string className;
public ClassID cid;
public SuperClassID scid;
public Reference[] targets;
public ParameterBlock[] paramblocks;
public Plugin()
{
}
public Plugin(Animatable a)
: base(a)
{
className = a.ClassName;
cid = a.ClassID;
scid = a.SuperClassID;
ReferenceTarget rt = a as ReferenceTarget;
var pbs = new List<ParameterBlock>();
if (rt == null)
{
targets = new Reference[0];
}
else
{
targets = new Reference[rt.NumTargets];
for (int i = 0; i < rt.NumTargets; ++i)
{
var target = rt.GetTarget(i);
targets[i] = new Reference(target);
if (target is ParameterBlock1)
{
pbs.Add(new ParameterBlock(target as ParameterBlock1));
}
else if (target is ParameterBlock2)
{
pbs.Add(new ParameterBlock(target as ParameterBlock2));
}
}
}
paramblocks = pbs.ToArray();
}
}
/// <summary>
/// Serializable representation of a node.
/// </summary>
[Serializable]
public class Node : Target
{
public string name;
public Node[] nodes;
public Reference sceneobject;
public Reference[] modifiers;
public Matrix3 transform;
public Node()
{
}
public Node(MaxSharp.Node n)
: base(n)
{
name = n.Name;
if (n.Object != null)
sceneobject = new Reference(n.Object.Base);
else
sceneobject = new Reference();
transform = n.NodeTransform;
nodes = (from x in n.Nodes select new Node(x)).ToArray();
modifiers = (from x in n.Modifiers select new Reference(x)).ToArray();
}
public IEnumerable<Node> NodeTree
{
get
{
if (nodes != null)
foreach (Node n in nodes)
foreach (Node m in n.NodeTree)
yield return n;
yield return this;
}
}
}
/// <summary>
/// Serializable representation of a parameter.
/// </summary>
[Serializable]
public class Parameter
{
public string name;
public System.Object value;
public string type;
public int id;
public Parameter()
{
}
public Parameter(IParameter p)
{
name = p.Name;
value = p.Value;
type = p.Type;
id = p.Id;
}
}
/// <summary>
/// Serializable representation of a parameter block.
/// </summary>
[Serializable]
public class ParameterBlock : Target
{
public Parameter[] parameters;
public ParameterBlock()
{
}
public ParameterBlock(ParameterBlock1 pb) : base(pb)
{
parameters = (from p in pb.Params select new Parameter(p)).ToArray();
}
public ParameterBlock(ParameterBlock2 pb) : base(pb)
{
parameters = (from p in pb.Params select new Parameter(p)).ToArray();
}
}
#region fields
public Node root;
public Plugin[] plugins;
#endregion
#region constructors
public SerializableScene()
{
}
public SerializableScene(MaxSharp.Node x)
{
root = new Node(x);
Dictionary<UIntPtr, MaxSharp.Animatable> lookup = new Dictionary<UIntPtr, MaxSharp.Animatable>();
var pluginList = new List<Plugin>();
foreach (var rt in x.NodeReferenceTree)
{
// Make sure we don't repeat anything.
if (!lookup.ContainsKey(rt.AnimHandle))
{
if (rt is MaxSharp.Node || rt is MaxSharp.ParameterBlock1 || rt is MaxSharp.ParameterBlock2)
{
// do nothing these
}
else
{
pluginList.Add(new Plugin(rt));
}
lookup.Add(rt.AnimHandle, rt);
}
}
this.plugins = pluginList.ToArray();
}
#endregion
}
}