home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 7253 / DS_2_PC.7z / DS_2_PC.ms
Encoding:
Text File  |  2014-04-24  |  11.9 KB  |  515 lines

  1. if (heapSize < 200000) then
  2.         heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
  3.  
  4. fname = getOpenFileName \
  5. caption:"Open .flver from Mesh folder" \
  6. types:"Dark Souls (*.flver)|*.flver" \
  7. historyCategory:"DarkSoulsObjectPresets"
  8. f = fopen fname "rb"
  9.  
  10. clearlistener()
  11.  
  12. fn readFixedString bstream fixedLen = (
  13. local str = ""
  14. for i = 1 to fixedLen do (
  15. str += bit.intAsChar (ReadByte bstream #unsigned))
  16. str
  17. )
  18.  
  19. struct weight_data
  20. (
  21.     boneids,weights
  22. )
  23. struct Vertex_Info_Struct
  24. (
  25. VertexType, VertexSize,VertexCount,VertexOffset
  26. )
  27. struct Face_Info_Struct
  28. (
  29. FaceCount, FaceOffset,FaceSecSize
  30. )
  31.  
  32.  
  33. clearlistener()
  34.  
  35. fseek f 0x6 #seek_set
  36. Endian = ReadShort f
  37. VersionMajor=Readshort f
  38. VersionMinor=Readshort f
  39. dataOffset = Readlong f    
  40. print (dataOffset as string)
  41. dataSize = Readlong f    
  42. count = Readlong f
  43. numMat = Readlong f    
  44. BoneCount = Readlong f        
  45. numParts = Readlong f    
  46. numMesh = Readlong f    
  47.  
  48. fseek f 0x54  #seek_set
  49. SkipCount1 = readlong f    
  50. SkipCount2 = readlong f    
  51.  
  52. fseek f 0x80  #seek_set
  53. for a = 1 to count  do (
  54. getPos = ftell f + 64
  55. fseek f getPos #seek_set    
  56. )        
  57. for a = 1 to numMat do (
  58. getPos = ftell f + 32
  59. fseek f getPos #seek_set    
  60. )    
  61.  
  62. Print ("Bone Start @ 0x"+((bit.intAsHex(ftell f))as string))
  63.     
  64. BNArr = #()
  65. Vertex_Info    = #()
  66. Face_Info = #()    
  67. LodCount_Array = #()    
  68. Bone_Names_Array = #()    
  69. BoneMapCount_Array = #()
  70. BoneMapOff_Array = #()    
  71. BoneNameSizeArray = #()    
  72. Trans_array = #()    
  73. Rotation_array = #()    
  74. Scale_array = #()    
  75.  
  76. BonePos=ftell f
  77.     
  78. for a = 1 to BoneCount/2 do (
  79.  
  80. fseek f 0xC #seek_cur
  81. BoneNameSize1 = Readlong f    
  82. fseek f 0x70 #seek_cur
  83. Pos2=ftell f    
  84. fseek f 0xC #seek_cur
  85. BoneNameSize2 = Readlong f    
  86. fseek f 0x70 #seek_cur        
  87.     
  88. fseek f Pos2 #seek_set    
  89.  
  90. fseek f 0xC #seek_cur
  91. BoneNameSize3 = Readlong f    
  92. fseek f 0x70 #seek_cur
  93. Pos1=ftell f            
  94. fseek f 0xC #seek_cur
  95. BoneNameSize4 = Readlong f        
  96. fseek f Pos1 #seek_set        
  97.  
  98. BoneNameSize_1=(BoneNameSize2-BoneNameSize1)
  99. BoneNameSize_2=(BoneNameSize4-BoneNameSize3)    
  100.  
  101. append BoneNameSizeArray [BoneNameSize_1, BoneNameSize_2]
  102. )
  103.  
  104. print BoneNameSizeArray
  105.  
  106. fseek f BonePos #seek_set
  107.  
  108. for a = 1 to BoneCount do (
  109.  
  110. fseek f 0xC #seek_cur
  111.  
  112. BPos=ftell f
  113. BoneNameOff = Readlong f    
  114. fseek f BoneNameOff #seek_set
  115. BoneName=readFixedString f 12
  116. fseek f BPos #seek_set
  117.     
  118. fseek f 0x74 #seek_cur
  119. append Bone_Names_Array BoneName        
  120. )
  121.  
  122. fseek f BonePos #seek_set
  123.  
  124.   for a = 1 to BoneCount do (
  125.    getPos = ftell f + 128 
  126.    px = readFloat f
  127.    py = readFloat f
  128.    pz = readFloat f
  129.    BPosition = [px,py,pz]
  130.    BoneNameOff = readlong f
  131.    rx = readFloat f 
  132.    ry = readFloat f 
  133.    rz = readFloat f 
  134.    BoneRotation = ((eulerAngles (RadToDeg (rx)) (RadToDeg (ry)) (RadToDeg (rz))) as quat)
  135.    BoneParent = readShort f
  136.    Unknown01 = readShort f
  137.    sx = readFloat f
  138.    sy = readFloat f
  139.    sz = readFloat f
  140.    BScale = [sx,sy,sz]
  141.    Unk02 = readShort f
  142.    Unk03 = readShort f
  143.    fseek f getPos #seek_set 
  144.       
  145.    bne = dummy showLinks:true showLinksOnly:true
  146.    bne.scale = BScale
  147.    bne.rotation =  conjugate BoneRotation
  148.    bne.pos = BPosition
  149.    append BNArr bne
  150.    if BoneParent!=-1 then (
  151.     bne.parent = BNArr[(BoneParent+1)]
  152.     bne.transform *= bne.parent.transform   
  153.    )
  154.   )
  155.  
  156. Print ("Bone End @ 0x"+((bit.intAsHex(ftell f))as string))
  157.     
  158. for a = 1 to numParts do (
  159. getPos = ftell f + 48    
  160. fseek f 0x14 #seek_cur    
  161. BoneMapCount= readlong f
  162. Null = readlong f    
  163. BoneMapOff = readlong f        
  164. LodCount= readlong f    
  165. fseek f 0xC #seek_cur        
  166. fseek f getPos #seek_set    
  167. append LodCount_Array LodCount
  168. append BoneMapCount_Array BoneMapCount    
  169. append BoneMapOff_Array    BoneMapOff 
  170. )        
  171.  
  172. print BoneMapCount_Array
  173.  
  174. for a = 1 to numParts do (
  175. if LodCount_Array[a]==1 then
  176. (
  177. LodId= Readlong f
  178. Unk1= readbyte f    
  179. Unk2= readbyte f
  180. Unk3= readbyte f    
  181. Unk4= readbyte f    
  182. FaceCount= Readlong f    
  183. FaceOffset= Readlong f    
  184. FaceSecSize= Readlong f        
  185. null= Readlong f    
  186. null2= Readlong f
  187. null3= Readlong f    
  188. append Face_Info (Face_Info_Struct FaceCount:FaceCount FaceOffset:FaceOffset FaceSecSize:FaceSecSize )                
  189. )
  190. if LodCount_Array[a]>1 then
  191. (
  192. LodId= Readlong f
  193. Unk1= readbyte f    
  194. Unk2= readbyte f
  195. Unk3= readbyte f    
  196. Unk4= readbyte f    
  197. FaceCount= Readlong f    
  198. FaceOffset= Readlong f    
  199. FaceSecSize= Readlong f        
  200. null= Readlong f    
  201. null2= Readlong f
  202. null3= Readlong f    
  203. append Face_Info (Face_Info_Struct FaceCount:FaceCount FaceOffset:FaceOffset FaceSecSize:FaceSecSize )                    
  204. for i = 1 to (LodCount_Array[a]-1) do (    
  205. getPos = ftell f + 32            
  206. fseek f getPos #seek_set    
  207. )    
  208. )
  209.  
  210. )    
  211.  
  212. Print ("Face Info End @ 0x"+((bit.intAsHex(ftell f))as string))
  213.  
  214. print Face_Info
  215.     
  216. for a = 1 to numParts do (
  217. LodCount= Readlong f    
  218. if LodCount==1 then fseek f 0x20 #seek_cur    
  219. if LodCount==0 then fseek f 0x0 #seek_cur        
  220. VertexType= Readlong f    
  221. VertexSize= Readlong f    
  222. VertexCount= Readlong f    
  223. Unk1= Readlong f    
  224. Unk2= Readlong f    
  225. VertexSecSize= Readlong f    
  226. VertexOffset= Readlong f    
  227. append Vertex_Info (Vertex_Info_Struct VertexType:VertexType VertexSize:VertexSize VertexCount:VertexCount VertexOffset:VertexOffset)            
  228. )    
  229.  
  230. print Vertex_Info
  231.  
  232. Print ("Vertex Info End @ 0x"+((bit.intAsHex(ftell f))as string))
  233.  
  234. for a = 1 to SkipCount1 do (
  235. getPos = ftell f + 16        
  236. fseek f getPos #seek_set    
  237. )        
  238. for a = 1 to SkipCount2 do (
  239. getPos = ftell f + 32        
  240. fseek f getPos #seek_set    
  241. )    
  242.     
  243. for i = 1 to numParts do (
  244.     
  245. vertArray = #()
  246. Facearray = #()    
  247. UV_array = #()
  248. Weight_array=#()
  249. BoneMapIdArray    = #()    
  250.  
  251. BoneMapStart = BoneMapOff_Array[i]    
  252.     
  253. fseek f BoneMapStart #seek_set 
  254.     
  255. for i = 1 to BoneMapCount_Array[i] do (
  256. BoneMapIds = readlong f    
  257. append BoneMapIdArray (BoneMapIds+1)    
  258. )
  259.  
  260. print BoneMapIdArray
  261.  
  262. FaceOffset=(dataOffset+(Face_Info[i].FaceOffset))        
  263. fseek f (dataOffset+(Face_Info[i].FaceOffset)) #seek_set    
  264.     
  265. Print ("Face Start @ 0x"+((bit.intAsHex(ftell f))as string))    
  266.  
  267. StartDirection = 1
  268. f1 = ((Readshort f) + 1) 
  269. f2 = ((Readshort f) + 1) 
  270. FaceDirection = StartDirection
  271. while ((ftell f) < (FaceOffset + (Face_Info[i].FaceSecSize))) Do (
  272. f3 = (Readshort f)
  273. if (f3==0xFFFF) then (
  274. f1 = ((Readshort f) + 1) 
  275. f2 = ((Readshort f) + 1) 
  276. FaceDirection = StartDirection 
  277. ) else (
  278. f3 += 1
  279. FaceDirection *= -1
  280. if (f1!=f2)AND(f2!=f3)AND(f3!=f1) then (
  281. if FaceDirection > 0 then append Facearray [f1,f2,f3]
  282. else append Facearray [f1,f3,f2]
  283. )
  284. f1 = f2
  285. f2 = f3
  286.     
  287. VertOffset=(dataOffset+(Vertex_Info[i].VertexOffset))    
  288. fseek f (dataOffset+(Vertex_Info[i].VertexOffset))     #seek_set    
  289.  
  290. Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))        
  291. if Vertex_Info[i].VertexSize==48 then
  292. for x = 1 to Vertex_Info[i].VertexCount do (
  293. vx = Readfloat f
  294. vy = Readfloat f
  295. vz = Readfloat f
  296.         
  297. bone1 = readbyte f #unsigned    
  298. bone2 = readbyte f #unsigned    
  299. bone3 = readbyte f #unsigned    
  300. bone4 = readbyte f #unsigned
  301.     
  302. weight1 = (readshort f /32767.00)     
  303. weight2 = (readshort f /32767.00) 
  304. weight3 = (readshort f /32767.00)
  305. weight4 = (readshort f /32767.00) 
  306.     
  307. fseek f 0x10 #seek_cur
  308.     
  309. tu=Readfloat f    
  310. tv=Readfloat f*-1
  311.  
  312. w = (weight_data boneids:#() weights:#())
  313. maxweight = 0
  314. if(weight1 != 0) then
  315.    maxweight = maxweight + weight1
  316. if(weight2 != 0) then
  317.    maxweight = maxweight + weight2
  318. if(weight3 != 0) then
  319.    maxweight = maxweight + weight3
  320. if(weight4 != 0) then
  321.    maxweight = maxweight + weight4
  322.  
  323.  
  324. if(maxweight != 0) then (
  325.       if(weight1 != 0) then (
  326.          w1 = weight1 as float
  327.          append w.boneids (bone1 + 1)
  328.          append w.weights (w1)
  329.       )
  330.       if(weight2 != 0) then (
  331.          w2 = weight2 as float
  332.          append w.boneids (bone2 + 1)
  333.          append w.weights (w2)
  334.       )
  335.       if(weight3 != 0) then (
  336.          w3 = weight3 as float
  337.          append w.boneids (bone3 + 1)
  338.          append w.weights (w3)
  339.       )
  340.       if(weight4 != 0) then (
  341.          w4 = weight4 as float
  342.          append w.boneids (bone4 + 1)
  343.         append w.weights (w4)
  344.       )      
  345.    )
  346. append Weight_array w    
  347. append vertArray [vx,vy,vz]
  348. append UV_array [tu,tv,0]    
  349. )    
  350. if Vertex_Info[i].VertexSize==40 then
  351. for x = 1 to Vertex_Info[i].VertexCount do (
  352.  
  353. vx = Readfloat f
  354. vy = Readfloat f
  355. vz = Readfloat f
  356.         
  357. bone1 = readbyte f #unsigned    
  358. bone2 = readbyte f #unsigned    
  359. bone3 = readbyte f #unsigned    
  360. bone4 = readbyte f #unsigned
  361.     
  362. weight1 = (readshort f /32767.00)     
  363. weight2 = (readshort f /32767.00) 
  364. weight3 = (readshort f /32767.00)
  365. weight4 = (readshort f /32767.00) 
  366.     
  367. fseek f 0xC #seek_cur
  368.     
  369. tu=Readshort f/1024.00    
  370. tv=(Readshort f/1024.00)*-1    
  371.  
  372. w = (weight_data boneids:#() weights:#())
  373. maxweight = 0
  374. if(weight1 != 0) then
  375.    maxweight = maxweight + weight1
  376. if(weight2 != 0) then
  377.    maxweight = maxweight + weight2
  378. if(weight3 != 0) then
  379.    maxweight = maxweight + weight3
  380. if(weight4 != 0) then
  381.    maxweight = maxweight + weight4
  382.  
  383.  
  384. if(maxweight != 0) then (
  385.       if(weight1 != 0) then (
  386.          w1 = weight1 as float
  387.          append w.boneids (bone1 + 1)
  388.          append w.weights (w1)
  389.       )
  390.       if(weight2 != 0) then (
  391.          w2 = weight2 as float
  392.          append w.boneids (bone2 + 1)
  393.          append w.weights (w2)
  394.       )
  395.       if(weight3 != 0) then (
  396.          w3 = weight3 as float
  397.          append w.boneids (bone3 + 1)
  398.          append w.weights (w3)
  399.       )
  400.       if(weight4 != 0) then (
  401.          w4 = weight4 as float
  402.          append w.boneids (bone4 + 1)
  403.         append w.weights (w4)
  404.       )      
  405.    )
  406. append Weight_array w    
  407. append vertArray [vx,vy,vz]
  408. append UV_array [tu,tv,0]    
  409. )    
  410. if Vertex_Info[i].VertexSize==36 then
  411. for x = 1 to Vertex_Info[i].VertexCount do (
  412.  
  413. vx = Readfloat f
  414. vy = Readfloat f
  415. vz = Readfloat f
  416.         
  417. bone1 = readbyte f #unsigned    
  418. bone2 = readbyte f #unsigned    
  419. bone3 = readbyte f #unsigned    
  420. bone4 = readbyte f #unsigned
  421.     
  422. weight1 = (readshort f /32767.00)     
  423. weight2 = (readshort f /32767.00) 
  424. weight3 = (readshort f /32767.00)
  425. weight4 = (readshort f /32767.00) 
  426.     
  427. fseek f 0x8 #seek_cur
  428.     
  429. tu=Readshort f/1024.00    
  430. tv=(Readshort f/1024.00)*-1    
  431.  
  432. w = (weight_data boneids:#() weights:#())
  433. maxweight = 0
  434. if(weight1 != 0) then
  435.    maxweight = maxweight + weight1
  436. if(weight2 != 0) then
  437.    maxweight = maxweight + weight2
  438. if(weight3 != 0) then
  439.    maxweight = maxweight + weight3
  440. if(weight4 != 0) then
  441.    maxweight = maxweight + weight4
  442.  
  443.  
  444. if(maxweight != 0) then (
  445.       if(weight1 != 0) then (
  446.          w1 = weight1 as float
  447.          append w.boneids (bone1 + 1)
  448.          append w.weights (w1)
  449.       )
  450.       if(weight2 != 0) then (
  451.          w2 = weight2 as float
  452.          append w.boneids (bone2 + 1)
  453.          append w.weights (w2)
  454.       )
  455.       if(weight3 != 0) then (
  456.          w3 = weight3 as float
  457.          append w.boneids (bone3 + 1)
  458.          append w.weights (w3)
  459.       )
  460.       if(weight4 != 0) then (
  461.          w4 = weight4 as float
  462.          append w.boneids (bone4 + 1)
  463.         append w.weights (w4)
  464.       )      
  465.    )
  466. append Weight_array w    
  467. append vertArray [vx,vy,vz]
  468. append UV_array [tu,tv,0]    
  469. )    
  470.  
  471. try(
  472. msh = mesh vertices:vertArray faces:faceArray
  473. msh.numTVerts = UV_array.count
  474. buildTVFaces msh
  475. for j = 1 to UV_array.count do setTVert msh j UV_array[j]
  476. for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
  477. msh.displayByLayer = false
  478. msh.backfacecull = false
  479. msh.wirecolor = (color 230 200 210)
  480.  
  481. max modify mode
  482. select msh
  483. skinMod = skin ()
  484. addModifier msh skinMod
  485. for i = 1 to BoneMapIdArray.count do
  486. (
  487.     maxbone = BNArr[(BoneMapIdArray[i])] 
  488.     if i != BoneMapIdArray.count then 
  489.         skinOps.addBone skinMod maxbone 0
  490.     else
  491.         skinOps.addBone skinMod maxbone 1
  492. )
  493. modPanel.setCurrentObject skinMod
  494. for i = 1 to (Vertex_Info[i].VertexCount) do (
  495.    w = Weight_array[i]
  496.    bi = #() --bone index array
  497.    wv = #() --weight value array
  498.    
  499.    for j = 1 to w.boneids.count do
  500.    (
  501.       boneid = w.boneids[j]
  502.       weight = w.weights[j]
  503.       append bi boneid
  504.       append wv weight
  505.    )   
  506.    
  507.    skinOps.ReplaceVertexWeights skinMod i bi wv
  508.    
  509. )
  510. )catch(format "Error!!!\n")
  511. )
  512.  
  513. fclose f