目标:导出struct类型对象为一个json字符串,并保存到文件中,然后在Unity中读取。
1.MXSJson(参考:http://www.scriptspot.com/3ds-max/scripts/mxsjson)
这个需要再改一下 根据一个struct对象,导出json
获取struct成员变量信息
测试可以使用
pros=getPropNames per
-- print (pros as string)
for prop in pros do(
-- print prop
v=getProperty per prop
-- print v
)
假如结合这个和MXSJson.ms的话,应该可以得到我想要的,预计要2-3小时开发调试时间吧。继续找找其他人的,能拿来用的千万不用自己编写,原型写出来很快,维护调试扩展成本很高
2.用C#的Newtonsoft.Json.dll
参考1:https://blog.csdn.net/pengyancai/article/details/50110677#comments
参考2:http://forums.cgsociety.org/t/json-and-maxscript/1552038/11
参考3:http://home.avvanta.com/~loganb/code.htm
其他只是讨论一下,这个提供了一个json.ms。可惜没有例子,要自己摸索使用。
看了一下代码,是对Newtonsoft.Json.Linq.JObject进行了一下封装,挺好的,但是要达到我需要的struct2json的效果,还是要二次开发。
3.使用python,(参考:https://forums.autodesk.com/t5/3ds-max-programming/read-json-file/m-p/6769862#M15237)
这里说无法讲struct对象传给c#(参考:https://forums.cgsociety.org/t/struct-as-a-parameter/2047447)
4.不用json格式,直接使用struct as string的内容,到c#里面解析该格式的字符串
看打印出来的内容,有规律,需要的信息也都在
struct TypeInfo(id,type)
struct PersonInfo(name,age,type)
struct BoxInfo(name,count,type)
struct AllInfo(personList,person,box,type)
p=PersonInfo name:"Abc" age:12
print (p as string)
pList=#()
for i= 1 to 2 do
(
p=PersonInfo name:"Abc" age:i
p.type=TypeInfo id:i type:("type"+(i as string))
append pList p
)
print (pList as string)
i=AllInfo()
i.personList=pList
i.person=p
i.box=BoxInfo name:"box" count:20
print (i as string)
打印:
"(PersonInfo name:"Abc" age:12 type:undefined)"
"#((PersonInfo name:"Abc" age:1 type:(TypeInfo id:1 type:"type1")), (PersonInfo name:"Abc" age:2 type:(TypeInfo id:2 type:"type2")))"
"(AllInfo personList:#((PersonInfo name:"Abc" age:1 type:(TypeInfo id:1 type:"type1")), (PersonInfo name:"Abc" age:2 type:(TypeInfo id:2 type:"type2"))) Person:(PersonInfo name:"Abc" age:2 type:(TypeInfo id:2 type:"type2")) Box:(BoxInfo name:"box" count:20 type:undefined) type:undefined)"
----------------
奇怪哦。怎么没找到现成的呢。先把业务功能做好,后续再花点时间二次开发一下。
-------------------------
扩展了一下前面的MXSJson,可以直接转换struct对象了。
struct MXSJson
(
__keys__ = #(),
__value__ = #(),
__isArray__=false, --new
__array__=#(), --new
__linebreak__="", --\n有格式,好看一点,"":没有格式,字符串就一行
fn format_type value =(
local f_value = stringstream ""
class_type = classof value
if superclassof value == StructDef then(
-- print (classof value)
format "%" ("{" +value.__json__()+"}") to:f_value
return f_value as string
)
else if class_type == String then(
format "\"%\"" value to:f_value
return f_value as string
)
else if class_type == Integer then(
format "%" value to:f_value
return f_value as string
)
else if class_type == Array then(
format "[" to:f_value
for a in value do(
l_value = this.format_type a
format_s = "%,"
if a == value[value.count] do(
format_s = "%"
)
format format_s l_value to:f_value
)
format "]" to:f_value
)
else if class_type == UndefinedClass then(
format "%" "null" to:f_value
return f_value as string
)
else(
print ("format_type else class :"+(class_type as string))
)
return f_value as string
),
fn __json__ = (
data = ""
for a in __keys__ do(
item = __value__[finditem __keys__ a]
data += "\""+ a + "\":"
end_s = ","+__linebreak__
if finditem __keys__ a >= __keys__.count do(
end_s = ""
)
if superclassof item == StructDef then(
data += "{"+item.__json__()+"}"+end_s
)else(
-- end_s = ","
-- if finditem __keys__ a >= __keys__.count do(
-- end_s = ""
-- )
data += this.format_type(item)+end_s
)
)
return data
),
fn __jsonArray__ = (
data = ""
for i = 1 to __array__.count do
(
item = __array__[i]
-- end_s = ","
end_s = ","+__linebreak__
if i >= __array__.count do(
end_s = ""
)
if superclassof item == StructDef then(
data += "{"+item.__json__()+"}"+end_s
)else(
data += this.format_type(item)+end_s
)
)
return data
),
fn json = (
-- print ("json isArray:"+(__isArray__ as string))
if __isArray__ == false then
(
return "{"+__linebreak__+ this.__json__()+__linebreak__+"}"
)
else
(
return "["+ this.__jsonArray__()+"]"
)
),
fn items = (
return this.__keys__
),
fn item key =(
index = finditem this.__keys__ key
if index != 0 do(
return this.__value__[index]
)
),
fn getClassName obj = ( --new
classInfo=((classof obj) as string)
parts=FilterString classInfo "("
n= parts[1]
-- print ("getClassName:"+n)
return n
),
fn newByStruct structObj = ( --new
-- try(
if classof structObj == Array then
(
this.__isArray__=true
-- __array__
for i in structObj do
(
-- print ("newByStruct array item:"+(i as string))
mj=MXSJson()
mj.newByStruct i
append this.__array__ mj
)
)
else(
pros=getPropNames structObj
-- print (pros as string)
for prop in pros do
(
val=getProperty structObj prop
if classof val == MAXScriptFunction do continue --函数等不要
this.add prop val
)
)
-- )catch(
-- print ("newByStruct error")
-- print (structObj as string)
-- print (classof structObj)
-- print (classof structObj == Array)
-- -- print "\n"
-- )
),
fn __add__ key value = (
if value != undefined do --不要undefined
(
--original
index = finditem this.__keys__ key
if index == 0 do(
append this.__keys__ key
index = finditem this.__keys__ key
)
this.__value__[index] = value
)
),
fn add key value= (
if superclassof value == StructDef then( --new
if (this.getClassName value) == "#Struct:MXSJson" then
(
-- print ("add1:"+key)
this.__add__ key value --original
)
else
(
-- print ("add2:"+key)
mj =MXSJson()
mj.newByStruct value
value=mj
this.add key value --这里递归,可能导致死循环
-- this.__add__ key value --这里递归,可能导致异常
)
)
else(
if classof value == Array then
(
-- print ("add3:"+key)
ar=#()
for i in value do
(
-- print ("add array item:"+(i as string))
mj=MXSJson()
mj.newByStruct i
append ar mj
)
this.__add__ key ar --original
)
else(
-- print ("add4:"+key)
this.__add__ key value --original
)
)
)
)
一旦开始写手就停不下来,程序员啊,大概花了2小时多一些时间。
测试代码
fn struct2json structObject=(
print ("struct2json:"+(structObject as string))
mj = MXSJson()
mj.__linebreak__="\n" --\n有格式,好看一点,"":没有格式,字符串就一行
mj.newByStruct structObject
js=mj.json()
print ("json:\n"+js)
)
struct TypeInfo(id,type)
struct PersonInfo(
name,age,type,
typeList=#(),
fn setName =
(
this.name=n
return "abcd"
)
)
struct BoxInfo(name,count,type)
struct AllInfo(personList,person,box,type)
per=PersonInfo name:"Abc" age:12
per.type=TypeInfo id:0 type:("type0")
-- struct2json per
-- print (per as string)
pList=#()
for i= 1 to 2 do
(
p=PersonInfo name:"Abc" age:i
p.type=TypeInfo id:i type:("type"+(i as string))
append pList p
)
for i= 1 to 2 do
(
t=TypeInfo id:i type:("type"+(i as string))
append per.typeList t
)
-- print (pList as string)
b=BoxInfo name:"box" count:20
all=AllInfo()
all.personList=pList
all.person=per
all.box=b
-- struct2json b
-- struct2json per
-- struct2json pList
struct2json all
结果:
后续实际使用过程中可能还会调整,暂时这样
-----------------------------------
因为我只需要3dmax->unity的数据传导,并不需要考虑读取json构建struct对象的过程。而从现在查到的来看,好像语法上不支持动态创建struct对象。不过可以考虑用execute "" 构建一个字符串 然后让它来执行。当前不需要,而且更实际的应该是结合.net json使用吧。
------------------------------------