Allbone =#() -- all bone group
OldBoneName = #() -- bone name
BoneID = #() -- bone id as name
meshExport = #() -- export mesh
fn getVertexNormal polyObj faceIndex vertexNum = (
local norm = [0,0,0]
local faceSG = polyOp.getFaceSmoothGroup polyObj faceIndex
if faceSG == 0 then (
norm = in coordsys world polyOp.getFaceNormal polyObj faceIndex
)
else (
local indices = polyOp.getFaceVerts polyObj faceIndex
local vertexIndex = indices[vertexNum]
local sharedFaces = polyOp.getFacesUsingVert polyObj vertexIndex
for sharedFace in sharedFaces do (
local sharedFaceSG = polyOp.getFaceSmoothGroup polyObj sharedFace
if (bit.and sharedFaceSG faceSG) != 0 do
norm += in coordsys world (polyOp.getFaceNormal polyObj sharedFace)
)
)
normalize norm
) -- function get in:
http://forums.cgsociety.org/archive/index.php/t-440711.html actionMan.executeAction 0 "40021" -- Select All
for i in selection do --sort bone and mesh to group
(
if (classof i== BoneGeometry) then --without bibed object
(
append allbone i -- bone object to allbone group
append oldBoneName i.name -- bone name to group
)
else
(
append meshExport i --export mesh to group
)
)
if meshExport.count ==1 then
(
SkinMesh = meshexport[1]
if (classof skinmesh == PolyMeshObject) then
(
SkinMeshname=SkinMesh.name
thePath = getSavepath()
if thePath == undefined then thePath=(getDir #export)
out_name = ((thePath)+"/"+(SkinMeshname)+".smd") -- name on output file
out_file = createfile out_name -- create file
format "version 1\nnodes\n" to:out_file
--NODES
--<int|ID> "<string|Bone Name>" <int|Parent ID>
bonecount=allbone.count -- count for all bones is integer
for n=1 to bonecount do
(
id =n-1
append BoneID id
allbone[n].name = boneid[n] as string
if allbone[n].parent == undefined then allparentname= "-1"
else allparentname = allbone[n].parent.name
format "% \"%\" % \n" BoneID[n] OldBoneName[n] allparentname to:out_file
)
format "end\nskeleton\ntime 0\n" to:out_file
-- SKELETON
-- time <int>
-- Begins a new frame. Any range of numbers can be used, so long as they increase sequentially.
-- <int|bone ID> <float|PosX PosY PosZ> <float|RotX RotY RotZ>
for i in allbone do
(
if i.name == "0" then
(
pos = in coordsys world i.pos
rot = in coordsys world (i.transform.rotationPart as EulerAngles)
)
else
(
pos = in coordsys parent i.pos
rot = (i.transform.rotationPart * (inverse i.parent.transform.rotationPart)) as EulerAngles
)
rot.x = DegToRad rot.x
rot.y = DegToRad rot.y
rot.z = DegToRad rot.z
format "% % % % % % % \n" i.name pos.x pos.y pos.z rot.x rot.y rot.z to:out_file
)
format "end\n" to:out_file
-- TRIANGLES
-- <material>
-- <int|Parent bone> <float|PosX PosY PosZ> <normal|NormX NormY NormZ> <normal|U V> <int|links> <int|Bone ID> <normal|Weight> [...]
format "triangles\n" to:out_file
modelAsMesh = snapshotAsMesh SkinMesh
objSkin = SkinMesh.modifiers[1]
multiMat = SkinMesh.material
if (classOf multiMat) != MultiMaterial do
(
errorMsg = "Material must be a multi-material!"
throw errorMsg
)
max modify mode
modPanel.SetCurrentObject objSkin
FaceCount = SkinMesh.numFaces --getNumFaces <mesh>
for faceID = 1 to FaceCount do
(
local face = polyOp.getFaceVerts SkinMesh faceID
--face = getFace SkinMesh faceID
texFace = getTVFace modelAsMesh faceID
matID = getFaceMatID modelAsMesh faceID
texturePath = multiMat[matID].diffuseMap.filename
textureFilename = filenameFromPath texturePath
if textureFilename == undefined do textureFilename = "<MISSING>.BMP"
format "%\n" textureFilename to:out_file
for FaceVertID = 1 to 3 do
(
vertID =face[FaceVertID] --vertex ID 1,2,3
tvertId = texFace[FaceVertID]
numBoneWeights = skinops.GetVertexWeightCount objSkin vertID --Returns the number of bones influencing the specified vertex!!
pos = in coordsys world getVert modelAsMesh vertID
tc = getTVert modelAsMesh tvertId
for weightID = 1 to numBoneWeights do
(
if weightID == 1 then
(
BoneID = skinops.GetVertexWeightBoneID objSkin vertID weightID
bonename = skinOps.GetBoneName objSkin BoneID 1
format "% " ((bonename) as string) to:out_file
)
)
format "% % % " Pos.X Pos.Y Pos.Z to:out_file
--faceID
--FaceVertID
--norm = in coordsys world getNormal SkinMesh vertID
--Norm = normalize Norm
local norm = getVertexNormal SkinMesh faceID FaceVertID
format "% % % " Norm.X Norm.Y Norm.Z to:out_file
format "% % % " tc.x tc.y numBoneWeights to:out_file
for weightID = 1 to numBoneWeights do
(
weight = skinops.GetVertexWeight objSkin vertID weightID
BoneID = skinops.GetVertexWeightBoneID objSkin vertID weightID
bonename = skinOps.GetBoneName objSkin BoneID 1
output_BoneName_Weight = ((bonename) as string)+" "+( weight as string)
format "% " output_BoneName_Weight to:out_file
)
format "\n" to:out_file
)
)
for n=1 to bonecount do
(
allbone[n].name = oldbonename[n]
)
format "end\n" to:out_file
close out_file
msgOK = ("You file created in - "+(thePath as string)+"/"+(SkinMeshname)+".smd")
messageBox msgOK
)
else messageBox "modifier Skin is not found"
)
else messageBox "You can export only one mesh"