
Thanks to reveal8n For all his help.
dds converter
Code: Select all
set MEMORY_FILE binary "\x44\x44\x53\x20\x7C\x00\x00\x00\x07\x10\x0A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x05\x00\x00\x00\x44\x58\x54\x31\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x10\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
endian big
get name basename
string name + .dds
get size asize
set size2 size
math size2 - 0x80
goto 0xC
get width long
get height long
goto 0x54
get type byte
endian little
putVarChr MEMORY_FILE 0xC width long
putVarChr MEMORY_FILE 0x10 height long
putVarChr MEMORY_FILE 0x57 type byte
append
log MEMORY_FILE 0x80 size2
append
log name 0 size MEMORY_FILE
Mesh with bones and weights scripts
Bone run first
Code: Select all
if (heapSize < 20000000) then
heapSize = 200000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
fname = getOpenFileName \
caption:"Open Folklore Model File" \
types:"Folklore Model File(*.am2)|*.am2" \
historyCategory:"FolkloreObjectPresets"
f = fopen fname "rb"
fn ReadBEShort fstream = (
short = readshort fstream #unsigned
short = bit.swapBytes short 2 1
b = (bit.get short 16)
for i = 17 to 32 do short = bit.set short i b
return short
)
fn PrintOffset Var =
(
local Var = Var
print ("This is the offset 0x" + (bit.intAsHex Var) as string)
Var
)
fn floatSwap2 f =
(
i = bit.floatAsInt f
h = bit.intashex i
while h.count < 8 do h = "0" + h
s = (substring h 7 2) + (substring h 5 2) + (substring h 3 2) + (substring h 1 2)
bit.intAsFloat (bit.hexasint s)
)
fn ReadBEword fstream = (
return (bit.swapBytes (readshort fstream #unsigned) 1 2)
)
fn convertTo32 input16 = (
inputAsInt = input16
sign = bit.get inputAsInt 16
exponent = (bit.shift (bit.and inputAsInt (bit.hexasint "7C00")) -10) as integer - 16
fraction = bit.and inputAsInt (bit.hexasint "03FF")
if sign==true then sign = 1 else sign = 0
exponentF = exponent + 127
--Ouput 32 bit integer representing a 32 bit float
outputAsFloat = bit.or (bit.or (bit.shift fraction 13) (bit.shift exponentF 23)) (bit.shift sign 31)
--Output Check
return bit.intasfloat outputasfloat
)
fn ReadBEHalfFloat fstream = (
return convertTo32(ReadBEword fstream)
)
fn ReadBElong fstream = (
long = readlong fstream
long = bit.swapBytes long 1 4
long = bit.swapBytes long 2 3
return long
)
fn ReadBEfloat fstream = (
return floatSwap2(readfloat fstream)
)
fn ReadFixedString bstream fixedLen =
(
local str = ""
for i = 1 to fixedLen do
(
str += bit.intAsChar (ReadByte bstream #unsigned)
)
str
)
struct Mesh_Info_Struct
(
Mesh_Name,vertstart,vertend,vertcount,facecount
)
unk01 = ReadBElong f
unk02 = ReadBElong f
BoneCount = ReadBEword f
unk07 = ReadBEword f
unk03 = ReadBElong f
unk04 = ReadBElong f
unk05 = ReadBElong f
unk06 = ReadBElong f
BoneStart = ReadBElong f
struct Bone_Info_Struct
(
index,child,next
)
fseek f BoneStart#seek_set
BNInf = #()
BNArr = #()
for a = 1 to BoneCount do (
fseek f 0x01#seek_cur
index = readbyte f #unsigned
fseek f 0x16#seek_cur
child = ReadBEword f
next = ReadBEword f
fseek f 0x04#seek_cur
mx = ReadBEfloat f
my = ReadBEfloat f
mz = ReadBEfloat f
fseek f 0x24#seek_cur
tfm = (quat 0 0 0 1) as matrix3
tfm.row4 = [mx,my,mz]
newBone = bonesys.createbone \
tfm.row4 \
(tfm.row4 + 0.01 * (normalize tfm.row1)) \
(normalize tfm.row3)
newBone.name = (a as string)
newBone.width = 0.01
newBone.height = 0.01
newBone.transform = tfm
newBone.wirecolor = yellow
newbone.showlinks = true
newBone.setBoneEnable false 0
newBone.pos.controller = TCB_position ()
newBone.rotation.controller = TCB_rotation ()
append BNInf (Bone_Info_Struct index:index child:child next:next)
append BNArr newBone
)
for a = 1 to BoneCount do (
p = BNArr[a]
cidx = BNInf[a].child
while (cidx != 0xFFFF) do (
b = BNArr[cidx + 1]
b.parent = p
b.transform *= p.transform
cidx = BNInf[cidx + 1].next
)
)
fclose f
Mesh import run after bone
Code: Select all
if (heapSize < 20000000) then
heapSize = 200000000
fname = getOpenFileName \
caption:"Open Folklore Model File" \
types:"Folklore Model File(*cmd)|*cmd" \
historyCategory:"FolkloreObjectPresets"
f = fopen fname "rb"
fn ReadBEShort fstream = (
short = readshort fstream #unsigned
short = bit.swapBytes short 2 1
b = (bit.get short 16)
for i = 17 to 32 do short = bit.set short i b
return short
)
fn PrintOffset Var =
(
local Var = Var
print ("This is the offset 0x" + (bit.intAsHex Var) as string)
Var
)
fn floatSwap2 f =
(
i = bit.floatAsInt f
h = bit.intashex i
while h.count < 8 do h = "0" + h
s = (substring h 7 2) + (substring h 5 2) + (substring h 3 2) + (substring h 1 2)
bit.intAsFloat (bit.hexasint s)
)
fn ReadBEword fstream = (
return (bit.swapBytes (readshort fstream #unsigned) 1 2)
)
fn convertTo32 input16 = (
inputAsInt = input16
sign = bit.get inputAsInt 16
exponent = (bit.shift (bit.and inputAsInt (bit.hexasint "7C00")) -10) as integer - 16
fraction = bit.and inputAsInt (bit.hexasint "03FF")
if sign==true then sign = 1 else sign = 0
exponentF = exponent + 127
--Ouput 32 bit integer representing a 32 bit float
outputAsFloat = bit.or (bit.or (bit.shift fraction 13) (bit.shift exponentF 23)) (bit.shift sign 31)
--Output Check
return bit.intasfloat outputasfloat
)
fn ReadBEHalfFloat fstream = (
test = ReadBEword fstream
if test == 0 then return 0
else
return convertTo32(test) * 2
)
fn ReadBElong fstream = (
long = readlong fstream
long = bit.swapBytes long 1 4
long = bit.swapBytes long 2 3
return long
)
fn ReadBEfloat fstream = (
return floatSwap2(readfloat fstream)
)
fn ReadFixedString bstream fixedLen =
(
local str = ""
for i = 1 to fixedLen do
(
str += bit.intAsChar (ReadByte bstream #unsigned)
)
str
)
struct weight_data
(
boneids,weights
)
idstring = ReadFixedString f 4
Unk01 = ReadBElong f
MeshCount = ReadBEword f
TextureCount = ReadBEword f
Version = ReadFixedString f 4
Unk02 = ReadBElong f
NameOffset = ReadBElong f
Unk03 = ReadBElong f
Unk04 = ReadBElong f
struct Mesh_Info_Struct
(
Unk11,FaceCount,VertCount,FaceOffset,MeshOffset,TexInfoOff,BonePalletOff
)
Mesh_Info_Array = #()
for a = 1 to MeshCount Do (
fseek f 0x7C#seek_cur
Unk11 = ReadBElong f
FaceCount = ReadBElong f
VertCount = ReadBElong f
FaceOffset = ReadBElong f
MeshOffset = ReadBElong f
TexInfoOff = ReadBElong f
BonePalletOff = ReadBElong f
fseek f 0x18#seek_cur
append Mesh_Info_Array (Mesh_Info_Struct Unk11:Unk11 FaceCount:FaceCount VertCount:VertCount FaceOffset:FaceOffset MeshOffset:MeshOffset TexInfoOff:TexInfoOff BonePalletOff:BonePalletOff)
)
for a = 1 to MeshCount Do (
fseek f Mesh_Info_Array[a].BonePalletOff#seek_set
bidArray = #()
for b = 1 to 0x100 do (
boneid = ((readbyte f#unsigned) + 1)
if boneid != 256 do (
append bidArray boneid
)
)
fseek f Mesh_Info_Array[a].FaceOffset #seek_set
Vert_array = #()
Normal_array = #()
UV_array = #()
Face_array = #()
Weight_array = #()
StartDirection = -1
f1 = (ReadBElong f) + 1
f2 = (ReadBElong f) + 1
FaceDirection = StartDirection
while (ftell f) != (Mesh_Info_Array[a].FaceOffset + (Mesh_Info_Array[a].FaceCount * 4))Do (
f3 = (ReadBElong f)
if (f3==0xFFFF) then (
f1 = (ReadBElong f) + 1
f2 = (ReadBElong f) + 1
FaceDirection = StartDirection
) else (
f3 += 1
FaceDirection *= -1
if (f1!=f2)AND(f2!=f3)AND(f3!=f1) then (
if FaceDirection > 0 then append Face_array [(f1),(f2),(f3)]
else append Face_array [(f1),(f3),(f2)]
)
f1 = f2
f2 = f3
)
)
fseek f Mesh_Info_Array[a].MeshOffset #seek_set
for b = 1 to Mesh_Info_Array[a].VertCount Do (
vx = ReadBEfloat f
vy = ReadBEfloat f
vz = ReadBEfloat f
vw = ReadBEfloat f
nx = ReadBEHalfFloat f
ny = ReadBEHalfFloat f
nz = ReadBEHalfFloat f
nw = ReadBEHalfFloat f
ColorA = ReadBEHalfFloat f
ColorR = ReadBEHalfFloat f
ColorG = ReadBEHalfFloat f
ColorB = ReadBEHalfFloat f
tu2 = ReadBEHalfFloat f
tv2 = ReadBEHalfFloat f * -1
tu = ReadBEHalfFloat f
tv = ReadBEHalfFloat f * -1
TangentX = ReadBEHalfFloat f
TangentY = ReadBEHalfFloat f
TangentZ = ReadBEHalfFloat f
TangentW = ReadBEHalfFloat f
weight1 = ReadBEHalfFloat f
weight2 = ReadBEHalfFloat f
weight3 = ReadBEHalfFloat f
weight4 = ReadBEHalfFloat f
bone1 = ReadBEHalfFloat f
bone2 = ReadBEHalfFloat f
bone3 = ReadBEHalfFloat f
bone4 = ReadBEHalfFloat f
w = (weight_data boneids:#() weights:#())
maxweight = 0
if(weight1 != 0) then
maxweight = maxweight + weight1
if(weight2 != 0) then
maxweight = maxweight + weight2
if(weight3 != 0) then
maxweight = maxweight + weight3
if(weight4 != 0) then
maxweight = maxweight + weight4
if(maxweight != 0) then (
if(weight1 != 0) then (
w1 = weight1 as float
append w.boneids (bone1 as integer + 1)
append w.weights w1
)
if(weight2 != 0) then (
w2 = weight2 as float
append w.boneids (bone2 as integer + 1)
append w.weights w2
)
if(weight3 != 0) then (
w3 = weight3 as float
append w.boneids (bone3 as integer + 1)
append w.weights w3
)
if(weight4 != 0) then (
w4 = weight4 as float
append w.boneids (bone4 as integer + 1)
append w.weights w4
)
)
append Weight_array w
append Vert_array [vx,vy,vz]
append Normal_array [nx,ny,nz]
append UV_array [tu,tv,0]
)
msh = mesh vertices:Vert_array faces:Face_array
msh.numTVerts = UV_array.count
buildTVFaces msh
--msh.name = Mesh_Name_array[a]
for j = 1 to UV_array.count do setTVert msh j UV_array[j]
for j = 1 to Face_array.count do setTVFace msh j Face_array[j]
for j = 1 to Normal_array.count do setNormal msh j Normal_array[j]
max modify mode
select msh
skinMod = skin ()
addModifier msh skinMod
for i = 1 to bidArray.count do
(
maxbone = BNArr[(bidArray[i])]
if i != bidArray.count then
skinOps.addBone skinMod maxbone 0
else
skinOps.addBone skinMod maxbone 1
)
modPanel.setCurrentObject skinMod
for i = 1 to Weight_array.count do
(
w = Weight_array[i]
bi = #()
wv = #()
for j = 1 to w.boneids.count do
(
boneid = w.boneids[j]
weight = w.weights[j]
append bi boneid
append wv weight
)
skinOps.ReplaceVertexWeights skinMod i bi wv
)
modPanel.setCurrentObject $.baseObject prompt:false
modPanel.addModToSelection (Vertex_Weld ()) ui:on
$.modifiers[#Vertex_Weld].threshold = 0.001
max create mode
)
print Mesh_Info_Array
fclose f