PDA

View Full Version : ReScript


malu05
14-05-2006, 05:44 AM
I have tried and tried overagain to reverse this script's functionality but i just cant.
It is a import script and and what i want to do is turn it so it reads the data from mesh and place it where this import script reads it.

This is the script:

--
-- Script to import m2 files
-- into GMax
--

--
-- GLOBALS AND STRUCTURES

global filename = "C:\Ballista.m2"
-- replace filename for other models and reevaluate script
global head
global bstream
global step
global verts_read = #()
global views_read = #()
global view_inds_read = #()
global view_tris_read = #()
global name_read



struct WOW2_Header
(
magic,version,namelen,ofsname,
un1,nGlobalSequences,ofsGlobalSequences,nAnimation s,
ofsAnimations,nC,ofsC,nD,ofsD,nBones,ofsBones,nF,o fsF,
nVertices,ofsVertices,nViews,ofsViews,nG,ofsG,nTex tures,
ofsTextures,nH,ofsH,nI,ofsI,nJ,ofsJ,nK,ofsK,nX,ofs X,
nY,ofsY,nTexLookup,ofsTexLookup,nL,ofsL,nM,ofsM,nN ,
ofsN,n14floats,nBoundingTriangles,ofsBoundingTrian gles,
nBoundingVertices,ofsBoundingVertices,nBoundingNor mals,
ofsBoundingNormals,nO,ofsO,nP,ofsP,nQ,ofsQ,nR,ofsR ,nS,
ofsS,nT,ofsT,nU,ofsU,nV,ofsV
)

struct WOW2_Vertex
(
pos,bw1,bw2,bw3,bw4,bi1,bi2,bi3,bi4,normal,uv,n2fl oats
)

struct WOW2_Views
(
nindex,ofsnindex, ntris,ofsntris,nverts,ofsnverts,
nsubmesh,ofsnsubmesh,ntextures,ofsntextures,unknow n1,
ind,tri -- loaded vert and tri index
)

struct WOW2_Submesh
(
id,ofsvert,nverts,oftri,tris,unkown1,unknown2,
unknown3,unknown4,n3floats,
vs,fs,ts
)

struct WOW2_Textureunit
(
flags,submesh1,submesh2,color,texpass,texunit,unkn own1,
lookupindex,texunit2,lightemitt
)

struct WOW2_Texture
(
-- type:
--0 Texture given in filename
--1 Body + clothes
--2 Cape
--6 Hair, beard
--8 Tauren fur
--11 Skin for creatures

type,flags,namelen,ofsname
)

fn echo msg =
(
format "%\n" (msg) to:listener
)

fn ReadFixedString bstream fixedLen=
(
local str = ""
for i = 1 to fixedLen do
(
str += bit.intAsChar (ReadByte bstream #unsigned)
)
str
)

--
-- HELPERS

fn SkipBytes bstream count=
(
local unknown
case count of
(
2: unknown = ReadShort bstream #unsigned
4: unknown = ReadLong bstream #unsigned
default:
(
for i = 1 to count do
(
unknown = ReadByte bstream #unsigned
)
)
)
)

fn LongToString num=
(
local str = ""
for i = 1 to 4 do
(
str += bit.intAsChar (bit.and num 0xff)
-- num = bit.shift num -8
num /= 256
)
str
)

--
-- WOW M2 FUNCTIONS

fn WOW2_Open fname =
(
bstream = fopen fname "rb"
head = WOW2_Header ()


)

fn WOW2_Close =
(
step = "Close"
fclose bstream
)


fn WOW2_Read_Header =
(
if head == undefined or bstream == undefined then
echo "Nothing to read"

format "reading\n" to:listener
step = "Read header"
head.magic = ReadLong bstream #unsigned
echo ("4cc: "+(LongToString head.magic))
head.version = ReadLong bstream #unsigned
echo ("Version: "+(LongToString head.version))
head.namelen = ReadLong bstream #unsigned
echo ("Name len:"+ (head.namelen as string))
head.ofsname = ReadLong bstream #unsigned
echo ("Name offs:"+ head.ofsname as string)
head.un1 = ReadLong bstream #unsigned
--echo (LongToString head.un1)
--echo ("before glob ftell: "+(ftell bstream) as string)
head.nGlobalSequences = ReadLong bstream #unsigned
--echo ("Global seq:"+head.nGlobalSequences as string)
head.ofsGlobalSequences = ReadLong bstream #unsigned
--echo ("Global seq ofs:"+ head.ofsGlobalSequences as string)
--echo ("before anims: "+(ftell bstream) as string)
head.nAnimations = ReadLong bstream #unsigned
--echo ("Anims :"+ head.nAnimations as string)
head.ofsAnimations = ReadLong bstream #unsigned
--echo ("Anims ofs:" + head.ofsAnimations as string)
head.nC = ReadLong bstream #unsigned
--echo (LongToString head.nC)
head.ofsC = ReadLong bstream #unsigned
--echo (LongToString head.ofsC)
head.nD = ReadLong bstream #unsigned
--echo (LongToString head.nD)
head.ofsD = ReadLong bstream #unsigned
--echo (LongToString head.ofsD)
head.nBones = ReadLong bstream #unsigned
--echo ("Bones:"+head.nBones as string)
head.ofsBones = ReadLong bstream #unsigned
--echo ("Bones ofs:"+head.ofsBones as string)
head.nF = ReadLong bstream #unsigned
--echo (head.nF)
head.ofsF = ReadLong bstream #unsigned
--echo (head.ofsF)
--echo ("before verts ftell: "+(ftell bstream) as string)
head.nVertices = ReadLong bstream #unsigned
echo ("Vertices:"+head.nVertices as string)
head.ofsVertices = ReadLong bstream #unsigned
echo ("Vertices of:"+head.ofsVertices as string)
head.nViews = ReadLong bstream #unsigned
echo ("Views:"+head.nViews as string)
head.ofsViews = ReadLong bstream #unsigned
head.nG = ReadLong bstream #unsigned
head.ofsG = ReadLong bstream #unsigned
head.nTextures = ReadLong bstream #unsigned
--echo ("Textures:"+head.nTextures as string)
head.ofsTextures = ReadLong bstream #unsigned
head.nH = ReadLong bstream #unsigned
head.ofsH = ReadLong bstream #unsigned
head.nI = ReadLong bstream #unsigned
head.ofsI = ReadLong bstream #unsigned
head.nJ = ReadLong bstream #unsigned
head.ofsJ = ReadLong bstream #unsigned
head.nK = ReadLong bstream #unsigned
head.ofsK = ReadLong bstream #unsigned
head.nX = ReadLong bstream #unsigned
head.ofsX = ReadLong bstream #unsigned
head.nY = ReadLong bstream #unsigned
head.ofsY = ReadLong bstream #unsigned
head.nTexLookup = ReadLong bstream #unsigned
--echo ("TexLookup:"+head.nTexLookup as string)
head.ofsTexLookup = ReadLong bstream #unsigned
head.nL = ReadLong bstream #unsigned
head.ofsL = ReadLong bstream #unsigned
head.nM = ReadLong bstream #unsigned
head.ofsM = ReadLong bstream #unsigned
head.nN = ReadLong bstream #unsigned
head.ofsN = ReadLong bstream #unsigned
head.n14floats = undefined
for i = 1 to 14 do
(
undef = ReadFloat bstream
)
head.nBoundingTriangles = ReadLong bstream #unsigned
head.ofsBoundingTriangles = ReadLong bstream #unsigned
head.nBoundingVertices = ReadLong bstream #unsigned
head.ofsBoundingVertices = ReadLong bstream #unsigned
head.nBoundingNormals = ReadLong bstream #unsigned
head.ofsBoundingNormals = ReadLong bstream #unsigned
head.nO = ReadLong bstream #unsigned
head.ofsO = ReadLong bstream #unsigned
head.nP = ReadLong bstream #unsigned
head.ofsP = ReadLong bstream #unsigned
head.nQ = ReadLong bstream #unsigned
head.ofsQ = ReadLong bstream #unsigned
head.nR = ReadLong bstream #unsigned
head.ofsR = ReadLong bstream #unsigned
head.nS = ReadLong bstream #unsigned
head.ofsS = ReadLong bstream #unsigned
head.nT = ReadLong bstream #unsigned
head.ofsT = ReadLong bstream #unsigned
head.nU = ReadLong bstream #unsigned
head.ofsU = ReadLong bstream #unsigned
head.nV = ReadLong bstream #unsigned
head.ofsV = ReadLong bstream #unsigned
echo "---- Header finished ----"
ok
)


fn WOW2_Read_Name =
(
step = "Read Name"
if (fseek bstream head.ofsname #seek_set ) then
(
name_read = (ReadFixedString bstream head.namelen)
echo name_read
)
else
echo "unable to read name"
)


fn WOW2_Read_Verts =
(
step = "Read Verts"
if (fseek bstream head.ofsVertices #seek_set ) then
(
step = "Read verts prep"

for i=1 to head.nVertices do
(
local vert = WOW2_Vertex ()
local v4 = [0.0,0.0,0.0]
local v3 = [0.0,0.0,0.0]
local v2 = [0.0,0.0,0.0]
local v1 = [0.0,0.0]

v4.x = ReadFloat bstream
v4.y = ReadFloat bstream
v4.z = ReadFloat bstream
vert.pos = v4
vert.bw1 = ReadByte bstream #unsigned
vert.bw2 = ReadByte bstream #unsigned
vert.bw3 = ReadByte bstream #unsigned
vert.bw4 = ReadByte bstream #unsigned
vert.bi1 = ReadByte bstream #unsigned
vert.bi2 = ReadByte bstream #unsigned
vert.bi3 = ReadByte bstream #unsigned
vert.bi4 = ReadByte bstream #unsigned
v3.x = ReadFloat bstream
v3.y = ReadFloat bstream
v3.z = ReadFloat bstream
vert.normal = v3
v2.x = ReadFloat bstream
v2.y = ReadFloat bstream
v2.z = 0.0 --is empty (0.0)
vert.uv = v2
v1.x = ReadFloat bstream
v1.y = ReadFloat bstream
vert.n2floats = v1

append verts_read vert
)

echo ("Verts read: " + verts_read.count as string )
)
else
echo "unable to read vertices"
)

fn WOW2_Read_Views =
(
step = "Read Views"
if (fseek bstream head.ofsViews #seek_set ) then
(
for i=1 to head.nViews do
(
local view = WOW2_Views ()
view.nindex = ReadLong bstream #unsigned
view.ofsnindex = ReadLong bstream #unsigned
view.ntris = ReadLong bstream #unsigned
view.ofsntris = ReadLong bstream #unsigned
view.nverts = ReadLong bstream #unsigned
view.ofsnverts = ReadLong bstream #unsigned
view.nsubmesh = ReadLong bstream #unsigned
view.ofsnsubmesh = ReadLong bstream #unsigned
view.ntextures = ReadLong bstream #unsigned
view.ofsntextures = ReadLong bstream #unsigned
view.unknown1 = ReadLong bstream #unsigned

append views_read view
step = "Read Views Mod 3"
if((mod (view.ntris) 3) != 0.0 )then
echo ("View["+i as string \
+"].ntris is not a multiple of 3!\n")

if i == 1 then
( echo ("\nView nind : "+view.nindex as string)
echo ("View ofsind : "+view.ofsnindex as string)
echo ("View ntris : "+view.ntris as string)
echo ("View ofsntris : "+view.ofsntris as string)
echo ("View nverts: "+view.nverts as string)
echo ("View ofsnverts : "+view.ofsnverts as string)
echo ("View nsubm : "+view.nsubmesh as string)
echo ("View ntex : "+view.ntextures as string)
)
)
)
else
echo "unable to read views"

step = "Read View index lists"
--load tri and vert index lists
for i=1 to views_read.count do
(
local ind = #()
local tri = #()
step = "Read View index"
if (fseek bstream (views_read[i].ofsnindex) #seek_set ) then
(
for j = 1 to views_read[i].nindex do
(
local x = ReadShort bstream #unsigned
append ind x
)
views_read[i].ind = ind
)
else
echo "unable to read view indices"

step = "Read View faces"
if (fseek bstream views_read[i].ofsntris #seek_set ) then
(
for j= 1 to views_read[i].ntris do
(
local x = ReadShort bstream #unsigned
append tri x
)
views_read[i].tri = tri
)
else
echo "unable to read view faces"
)
)



fn WOW2_Create_Faces sm view =
(
local f = #()
local v = #()
local t = #()
step = "Create Faces"
local up = sm.tris as integer -1
if((mod (up+1) 3) != 0.0 )then
(
echo "#ERROR sm.tris not a multiple of 3!"
)
else
echo "#INFO sm.tris check passed!"

local ofs = sm.oftri as integer
for i=1 to (up) by 3 do
(
try
(
step = "Create Faces" +(i as string)
local a = view.tri[(ofs+ i)] + 1
local b = view.tri[(ofs+ i +1)] + 1
local c = view.tri[(ofs+ i +2)] + 1
step = "Create Faces erg lookup " + a as string + " " \
+ b as string + " "+ c as string + " "
-- echo ([a,b,c])
local erg = ([(view.ind[a] + 1),(view.ind[b] + 1), \
(view.ind[c] + 1)] as point3 )
--echo "-----------------------------------------------"
--echo (erg as string)
step = "Create Faces f append"
append f [(i),(i+1),(i+2)] --erg
-- echo ([(i),(i+1),(i+2)] as string)
step = "Create Faces v append"
append v verts_read[erg.x].pos
append v verts_read[erg.y].pos
append v verts_read[erg.z].pos
step = "Create Faces t append"
append t verts_read[erg.x].uv
append t verts_read[erg.y].uv
append t verts_read[erg.z].uv
)
catch
(
echo (getCurrentException())
throw ()
)
)
echo "done"

sm.vs = v
sm.fs = f
sm.ts = t
)


fn WOW2_Create_Submeshes view =
(
step = ("Create Submesh "+ view as string )
if (fseek bstream views_read[view].ofsnsubmesh #seek_set ) then
(
for i=1 to views_read[view].nsubmesh do
(
local sm = WOW2_Submesh ()
sm.id = ReadLong bstream #unsigned
--echo ("sm.id "+ sm.id as string)
sm.ofsvert = ReadShort bstream #unsigned
--echo ("sm.ofsvert "+ sm.ofsvert as string)
sm.nverts = ReadShort bstream #unsigned
--echo ("sm.nverts "+ sm.nverts as string)
sm.oftri = ReadShort bstream #unsigned
--echo ("sm.oftri "+ sm.oftri as string)
sm.tris = ReadShort bstream #unsigned
--echo ("sm.tris "+ sm.tris as string)
sm.unkown1 = ReadShort bstream #unsigned
sm.unknown2 = ReadShort bstream #unsigned
sm.unknown3 = ReadShort bstream #unsigned
sm.unknown4 = ReadShort bstream #unsigned
local p = [0.0,0.0,0.0]
p.x = ReadFloat bstream
p.y = ReadFloat bstream
p.z = ReadFloat bstream
sm.n3floats = p


WOW2_Create_Faces sm views_read[view]

--step = "out"
--echo (sm.vs.count)
--echo (sm.fs.count)
--echo (sm.ts.count)
--for i= 1 to uvs.count do
-- echo (uvs[i] as string)
--for i= 1 to ts.count do
-- echo (ts[i] as string)

step = "mat"
local skinMaterial = standardMaterial \
name:(name_read +"_"+view as string +"_"+i as string)
step = ("Mesh init ")
local theMesh = undefined
try
(
theMesh = mesh vertices:(sm.vs) faces:(sm.fs) \
name:(name_read +"_"+view as string +"_"+i as string) \
material:skinMaterial tverts:(sm.ts)
)
catch
(
echo (getCurrentException())
throw ()
)

step = "Build tv's"
try
(
--Set texcoord faces
--(so user just needs to load a converted skin PCX file)
buildTVFaces theMesh false
for i = 1 to sm.fs.count do
(
setTVFace theMesh i (sm.fs[i])
)

update theMesh
)
catch
(
echo (getCurrentException())
throw ()
)
update theMesh
--delete theMesh
gc()
sm = undefined
)
)
)


fn WOW2_Read_Meshes =
(
step = "Read Meshes"
for viewcnt = 1 to 1 do --views_read.count do
(
WOW2_Create_Submeshes 1
)
)



---
--- MAIN
try
(
WOW2_Open(filename)
WOW2_Read_Header()
WOW2_Read_Name()
WOW2_Read_Verts()
WOW2_Read_Views()
WOW2_Read_Meshes()
WOW2_Close()
format "last\n" to:listener

gc()
)
catch
(
format "-- Failed in \n" step to:listener
if bstream != undefined then WOW2_Close()
gc()
)


Can anyone help me with telling me what i must change inorder to reverse this script if its even possible?

malu05
14-05-2006, 05:47 AM
Here is the sample file used:

http://rapidshare.de/files/20396814/Ballista.m2.html

eeek
07-04-2007, 10:45 AM
Your'll have to write an exporter script, that format the verts of a mesh to a file. You cant simply reverse a file.