I’ve found this closed thread from @haavard:
I have exactly the same problem: I want to serialize/deserialize a class, all works fine except for that the dll must be in the 3dsMax root folder. I’ve tried others as bin, plugins, stdplugs… and nothing work.
Any idea how to make it work?
Here are the sample codes:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Max;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace P3D
{
[Serializable]
public class Test_Serialize : ISerializable
{
private IINode node;
private List<int> vertsID;
public int[] VertsID
{
get
{
return vertsID.ToArray();
}
}
public string nodeName
{
get
{
return node.Name;
}
}
//////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////
public Test_Serialize(uint nodeHandle)
{
IGlobal global = GlobalInterface.Instance;
IInterface14 ip = global.COREInterface14;
node = ip.GetINodeByHandle(nodeHandle);
vertsID = Enumerable.Range(0, node.NumSubs).ToList();
}
public Test_Serialize(SerializationInfo info, StreamingContext context)
{
vertsID = (List<int>)info.GetValue("vertsID", typeof(List<int>));
string nodeName = info.GetString("nodeName");
IGlobal global = GlobalInterface.Instance;
IInterface14 ip = global.COREInterface14;
node = ip.GetINodeByName(nodeName);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("vertsID", vertsID);
info.AddValue("nodeName", node.Name);
}
//////////////////////////////////////
// SERIALIZATION METHODS
//////////////////////////////////////
public void SerializeTestObject(string filename)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(filename,
FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, this);
stream.Close();
}
public Test_Serialize Deserialize(string filename)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(filename,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
Test_Serialize test = (Test_Serialize)formatter.Deserialize(stream);
stream.Close();
return test;
}
}
}
And in MaxScript:
dotNet.loadAssembly @"C:\Program Files\Autodesk\3ds Max 2016\Test_Serialize.dll"
the_Object = $
handleObj = the_Object.inode.handle
TestObj = dotnetObject "P3D.Test_Serialize" handleObj -- OK, creates object
TestObj.VertsID -- OK, shows List<int>
TestObj.nodeName -- OK, gets the node's name
TestObj.SerializeTestObject @"D:\_000-ANDRES\TEST\TestObj.bin" -- OK, saves serialized object
TestObj2 = TestObj.Deserialize @"D:\_000-ANDRES\TEST\TestObj.bin" -- OK, only if dll is in the root folder
TestObj2.VertsID -- OK, good deserialization
TestObj2.nodeName -- OK, good deserialization
