2個工作日
寫了一套簡單的Export/Import插件
一直對MAX的 Script感到很好奇
說說使用的感受吧。
首先,如果要使用這套Script那么 首先會陷入到龐大的MAX的概念的汪洋大海之中
MAX中很多概念不同于平常熟知的 RealTime里面的思想
例如,MAX中的紋理坐標,每個頂點都有一對UV 這個很正常
但是同時 MAX對每個Face也保存了UV,這是因為MAX中的 每個Face都有可能是不同的貼圖
等等
第二,如果熟悉了MAX中的哪些類能夠干什么 那么Script也就是很水道渠成的事情了。為了寫這2個插件,MAX Script 的help翻了無數次,幾乎每寫一句就要去翻一次。
第三,MAX中的數組起位置是1,但是MAX中有的概念 使用數組表示的時候起始位置為0 ,例如紋理的Channel,起始就是為0,并且Channel0 很危險,寫入錯誤直接讓MAX掛掉
第四,腳本編寫很考驗打字的準確度,由于變量沒有類型,不需要聲明就可以試用,打錯一個字母的話那就查吧,運氣不好的話,會讓你瘋掉的,同時MAX的腳本編輯器 超級難用。
第五,編寫導入插件的時候,發現Script 中讀入數據異常痛苦,我在Script的幫助中沒有發現任何類似c語言中的fprintf(%...)的東西,只有通過腳本提供的skiptostring來做。
第六 Max Script的函數 沒有明確的返回值的概念,不習慣
第七 那個調試器 太難用了,我打了無數個messagebox
總結:對于導入導出插件來說其實用SDK和Script 編寫其實差別不大,因為都只是涉及到數據的讀取和寫入,并不涉及到復雜的邏輯。如果編寫渲染類型或者是動畫控制類型的插件 我覺得還是用script好一些,因為可以直接在MAX中看到結果,同時還能夠一句一句的執行,很方便。
我只是初學,希望有經驗的人給點經驗,最后附上簡單的插件代碼,
導出插件只能到處一個幾何體,點擊ExportGeo按鈕,然后點選幾何體,即可輸出
導入插件沒什么特別的操作,導入插件結尾有些倉促
以下是導入插件:其實導入插件沒什么必要寫,反正是在MAX中建模,直接保存為MAX文件就可以了,這里
純屬練手
macroScript ImportGeo category: "HowTo2"
(
--從文件中讀入某個分割符 后面 的數據
--FileStream:文件句柄
--needload :儲存讀取后的值的變量 (string or value)
--spliter:分隔符(string)
--bReadLine: 是否讀取分隔符后所有的數據 1表示讀取后面所有
fn LoadData FileStream spliter bReadLine=
(
temp=skiptostring FileStream spliter
if bReadLine==1 then
needload=readline FileStream
else
needload=readvalue FileStream
)
--***************************************************
--******************************************************
--****************************************************
vert_array = #()
face_array = #()
sgroup_array = #()
matid_array = #()
edge_array = #()
tm_row1=#()
tm_row2=#()
tm_row3=#()
tm_row4=#()
nodename=""
--以下均為material變量
_Material_Name
_Material_Type
_Material_Ambient
_Material_Diffuse
_Material_Specular
_Material_Specular_Lv
_Material_Glossiness
_Material_SelfillumColor
_Material_SelfillumAmount
_Material_DiffTexPath
BitmapPath
channel_num
UVW_Ver_Num
UVW_Face_Num
Vert_UV=#() --所有的頂點的uv都放在這里
Face_UV=#() --所有的面的uv都放在這里
Vert_UV_Size=#() --用于記錄每個channel的頂點的uv總數
Face_UV_Size=#() --用語及了每個channel的面uv總數
in_name=getopenfilename()
if in_name!=undefined then
(
in_file=openfile in_name
if in_file!=undefined then
(
--node name
temp= readline in_file
token=filterString temp " "
nodename=(token[token.count] as string)
--**************************************************
--material info
_Material_Name=LoadData in_file "->" 1
_Material_Type=LoadData in_file "->" 1
_Material_Ambient=LoadData in_file "->" 0
_Material_Diffuse=LoadData in_file "->" 0
_Material_Specular=LoadData in_file "->" 0
_Material_Specular_Lv=LoadData in_file "->" 0
_Material_Glossiness=LoadData in_file "->" 0
_Material_SelfillumColor=LoadData in_file "->" 0
_Material_SelfillumAmount=LoadData in_file "->" 0
_Material_DiffTexPath=LoadData in_file "->" 1
--**************************************************
--node matrix
--temp= readline in_file
tt=readDelimitedString in_file ":"
temp=readvalue in_file
append tm_row1(temp)
tt=readDelimitedString in_file ":"
temp=readvalue in_file
append tm_row2(temp)
tt=readDelimitedString in_file ":"
temp=readvalue in_file
append tm_row3(temp)
tt=readDelimitedString in_file ":"
temp=readvalue in_file
append tm_row4(temp)
--************************************************
--vertex info
temp= readline in_file
token=filterString temp " "
num_vert=token[token.count] as integer
for i=1 to num_vert do
(
tt=readDelimitedString in_file ":"
temp=readvalue in_file
append vert_array (temp)
)
--************************************************************
--face info
temp=readline in_file
token=filterstring temp " "
num_face=(token[token.count] as integer)
for j=1 to num_face do
(
append face_array (readValue in_file)
append sgroup_array (readValue in_file)
append matid_array (readValue in_file)
edge1 = readValue in_file
edge2 = readValue in_file
edge3 = readValue in_file
append edge_array (#(edge1, edge2, edge3))
)
--************************************************************
--uv info
-- in max channel begin with 0
--the channel 0 stores the vertex color info,no uv info
channel_num=LoadData in_file ":" 0
for b=1 to (channel_num-1) do
(
--讀出Channel id
channel_id=LoadData in_file ":" 0
--texture map path
BitmapPath=LoadData in_file "->" 1
--讀出uv Vertex 的數量
temp_size=LoadData in_file ":" 0
append Vert_UV_Size temp_size
--uv的數據
for i=1 to temp_size do
(
append Vert_UV (readValue in_file)
)
--uv face的 數量
temp_size==LoadData in_file ":" 0
append Face_UV_Size temp_size
for i=1 to temp_size do
(
append Face_uv (readValue in_file)
)
)
)
close in_file
)
meditmaterials[1].ambient=_Material_Ambient
meditmaterials[1].diffuse=_Material_Diffuse
meditmaterials[1].shaderbyname=_Material_type
meditmaterials[1].specular=_material_specular
meditmaterials[1].specularlevel=_material_specular_lv
meditmaterials[1].glossiness=_material_glossiness
meditmaterials[1].selfillumcolor=_Material_SelfillumColor
meditmaterials[1].selfillumamount=_Material_SelfillumAmount
meditmaterials[1].diffuseMapEnable=true
bmp =bitmaptexture filename: _Material_DiffTexPath
meditmaterials[1].diffusemap=bmp
new_mesh = mesh vertices:vert_array faces:face_array --materialIDs:#(0,1) tverts:vert_uv
for f = 1 to num_face do
(
setFaceSmoothGroup new_mesh f sgroup_array[f]
setFaceMatID new_mesh f matid_array[f]
setEdgeVis new_mesh f 1 edge_array[f][1]
setEdgeVis new_mesh f 2 edge_array[f][2]
setEdgeVis new_mesh f 3 edge_array[f][3]
)
myTransform = new_mesh.transform
new_mesh.transform.row1 = tm_row1[1]
new_mesh.transform.row2 = tm_row2[1]
new_mesh.transform.row3 = tm_row3[1]
new_mesh.transform.row4 = tm_row4[1]
new_mesh.name=(nodename as string)
new_mesh.material=meditmaterials[1]
meshop.setMapSupport new_mesh 0 true
meshop.setMapSupport new_mesh 1 true
--set vertex uv for every vertex
meshop.setNumMapVerts new_mesh 1 Vert_UV_Size[1] keep:false
for i=1 to Vert_UV_Size[1] do
(
meshop.setMapVert new_mesh 1 i Vert_UV[i]
)
--set face uv for every face
meshop.setnummapfaces new_mesh 1 Face_UV_Size[1] keep:false
for i=1 to Face_UV_Size[1] do
(
meshop.setMapFace new_mesh 1 i Face_UV[i]
)
update new_mesh
)
以下是導出插件:
到處插件很容易寫,導入插件花了2倍于導出的時間
-- export by sssa2000
macroScript ExportGeo category:"HowTo2"
(
fn GetGeometry o =
(
Superclassof o == Geometryclass and classof o != TargetObject
)
fn DumpMaterial m file=
(
--只對Standard類型的材質處理
--獲得diffuse and diffuse map
ismat=iskindof m material
if ismat then
(
name=m.name
format "Material Name-> %\n" name to: file
class_of_mat=classof m
--messagebox class_of_mat
if (class_of_mat )==Standardmaterial then
(
type=m.shaderByName
format "Material Type->%\n" type to: file
_ambient=m.ambient
format "Ambient Value->%\n" _ambient to:file
diffuse_value=m.diffuse
format "Diffuse Value->%\n" diffuse_value to:file
_specular=m.specular
format "Specular Value->%\n" _specular to:file
_specularLevel =m.specularLevel
format "SpecularLevel Value->%\n" _specularLevel to:file
_Glossiness=m.Glossiness
format "Glossiness Value->%\n" _Glossiness to:file
_selfIllumColor =m.selfIllumColor
format "SelfIllumColor Value->%\n" _selfIllumColor to:file
_selfIllumAmount =m.selfIllumAmount
format "SelfIllumAmount Value->%\n" _selfIllumAmount to:file
diffuse_map_path=m.diffusemap.filename
format "Diffuse Map Path->%\n" diffuse_map_path to:file
)
)
format "\n" to: file
)
--/////////////////////////////////////////////////////////////////////////////////////////
obj = pickobject filter:GetGeometry
if isValidNode obj then
(
nodename=obj.name
--First export the matrix
row1=obj.transform.row1
row2=obj.transform.row2
row3=obj.transform.row3
row4=obj.transform.row4
tmesh = snapshotAsMesh obj
out_name = GetSaveFileName()
if out_name != undefined then
(
out_file = createfile out_name
format "Node Name: %\n" nodename to: out_file
format "\n" to: out_file
--******************************************************
-- material info
node_material=obj.material
num_sub_material=getNumSubMtls obj.material
DumpMaterial obj.material out_file
for y=1 to num_sub_material do
(
sub_mat=getSubMtl obj.material y
DumpMaterial sub_mat= out_file
)
--******************************************************
format "Node TM Row1: %\n" row1 to: out_file
format "Node TM Row2: %\n" row2 to: out_file
format "Node TM Row3: %\n" row3 to: out_file
format "Node TM Row4: %\n" row4 to: out_file
--******************************************************
-- vertex info
num_verts = tmesh.numverts
num_faces = tmesh.numfaces
format "Number of Ver: %\n" num_verts to:out_file
for v = 1 to num_verts do
format "Ver%: %\n" v (getVert tmesh v) to:out_file
format "\n" to:out_file
--***********************************************
--face info
format "Number of Face: %\n" num_faces to:out_file
for f = 1 to num_faces do
(
face = getFace tmesh f
sgroup = getFaceSmoothGroup tmesh f
matid = getFaceMatId tmesh f
edge1 = getEdgeVis tmesh f 1
edge2 = getEdgeVis tmesh f 2
edge3 = getEdgeVis tmesh f 3
format "%,%,%,%,%,%\n" face sgroup matid edge1 edge2 edge3 to:out_file
)
--******************************************************
--uv info
channel=meshop.getnummaps tmesh --number of texture
format "\n" to: out_file
format "Channel Number:%\n" channel to:out_file
for i=1 to (channel-1) do
(
-- channel的計數從0開始
--channel 0 is vertex color so do not export it
IsSupport=meshop.getMapSupport tmesh i
if IsSupport==true then
(
format "Channel ID:%\n" i to:out_file
if classof obj.material.maps[i+1]==Bitmaptexture then
format "Map File Path->%\n" obj.material.maps[i+1].filename to: out_file
else
format "Map File Path->Null\n" to: out_file
num_uv_ver=meshop.getNumMapVerts tmesh i
num_uv_face=meshop.getNumMapFaces tmesh i
format "UVW Vertex Number:%\n" num_uv_ver to:out_file
for j=1 to num_uv_ver do
(
vert_uvw=meshop.getMapVert tmesh i j
--messagebox (vert_uvw as string)
format "% \n" vert_uvw to: out_file
)
format "UVW Face Number:%\n" num_uv_face to:out_file
for o=1 to num_uv_face do
(
uvw_face=meshop.getMapFace tmesh i o
format "% \n" uvw_face to: out_file
)
)
else
(
--format "Do Not Support Channel %\n" i to:out_file
)
)
close out_file
edit out_name
)
)
)