Advertisement
expired6978

TRIEditor

Jun 15th, 2014
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.17 KB | None | 0 0
  1. global TRIEditor
  2. (
  3. if TRIEditor != undefined do ( closerolloutfloater TRIEditor )
  4. TRIEditor = newrolloutfloater "TRI Editor" 250 500
  5.  
  6. struct TRIMorph
  7. (
  8. mname=0,
  9. multiplier=0,
  10. vertices=#()
  11. )
  12.  
  13. struct TRIHeader
  14. (
  15. identifier="FR",
  16. filetype="TRI",
  17. version="003",
  18. vertices=0,
  19. polytris=0,
  20. polyquads=0,
  21. unknown2=0,
  22. unknown3=0,
  23. uvvertices=0,
  24. flags=0,
  25. nummorphs=0,
  26. nummodifiers=0,
  27. modvertices=0,
  28. unknown7=0,
  29. unknown8=0,
  30. unknown9=0,
  31. unknown10=0
  32. )
  33.  
  34. struct TRIFile
  35. (
  36. header,
  37. vertices=#(),
  38. faces=#(),
  39. texCoords=#(),
  40. faceTex=#(),
  41. morphs=#()
  42. )
  43.  
  44. struct MorphPair
  45. (
  46. morphMod,
  47. channelIndex
  48. )
  49.  
  50. triArray = #()
  51. currentFile = undefined
  52. currentIndex = undefined
  53.  
  54. fn readTRIFile filePath = ()
  55. fn writeTRIFile filePath = ()
  56. fn loadMorphsFromModifiers referenceNode fileInfo morpherName transferProgress = ()
  57. fn createMorpher referenceNode fileInfo morpherName transferProgress = ()
  58. fn createMeshFromTRI fileInfo objectName=()
  59. fn vertexDifferenceToMorph sourceNode targetNode targetMorph = ()
  60. fn findMorphByName fileInfo morphName = ()
  61. fn buildMorpherList referenceNode = ()
  62. fn replaceTRIReference referenceNode fileInfo = ()
  63. fn findMorpherByName referenceNode morphName = ()
  64.  
  65. fn ReadFixedString bstream = ()
  66. fn WriteFixedString bstream str = ()
  67.  
  68. global exportRollout
  69. global morphEditorRollout
  70. global triEditorRollout
  71. global importRollout
  72. global morphTransferRollout
  73.  
  74. fn GetWorkingNode =
  75. (
  76. local object = undefined
  77. try
  78. (
  79. object = triEditorRollout.selectReference.object
  80. if isValidNode object == false then
  81. (
  82. triEditorRollout.selectReference.object = undefined
  83. object = $
  84. )
  85. )
  86. catch
  87. (
  88. object = $
  89. )
  90.  
  91. if isValidNode object == false then
  92. throw "No object selected or referenced."
  93.  
  94. return object
  95. )
  96.  
  97. rollout exportRollout "Export"
  98. (
  99. editText exportFilePath "Export File Path" labelOnTop:true
  100. button openFileButton "Browse..."
  101. button exportButton "Export"
  102.  
  103. on openFileButton pressed do
  104. (
  105. local exportPath = getSavePath caption:"Select Folder" initialDir:exportFilePath.text
  106. if exportPath != undefined do
  107. (
  108. exportFilePath.text = exportPath + "\\"
  109. )
  110. )
  111.  
  112. on exportButton pressed do
  113. (
  114. local workingDirectory = pathConfig.getCurrentProjectFolder() + "\\"
  115. if exportFilePath.text != "" then
  116. workingDirectory = exportFilePath.text
  117.  
  118. for i=1 to triArray.count do
  119. (
  120. local fileFullPath = workingDirectory + triEditorRollout.loadedTris.items[i]
  121. local filePath = getFilenamePath fileFullPath
  122. makeDir filePath all:true
  123. local fileInfo = triArray[i]
  124. if filePath != undefined then
  125. (
  126. try
  127. (
  128. writeTRIFile fileFullPath fileInfo
  129. )
  130. catch
  131. (
  132. MessageBox("Error writing file " + filePath + " - " + GetCurrentException())
  133. )
  134. )
  135. )
  136.  
  137. MessageBox("Export finished.")
  138. )
  139. )
  140.  
  141. rollout morphEditorRollout "Morph Editor"
  142. (
  143. ListBox selectedMorphs "Morphs" labelOnTop:true
  144.  
  145. editText editMorphNameEdit "Name" labelOnTop:true
  146. spinner editMorphScaleEdit "Scale Multiplier" range:[0.0000000001,1000000,0.0000000001] type:#float scale:0.0000000001
  147. button morphRemoveButton "Remove Selected" tooltip:"Deletes the selected morph from the selected TRI file."
  148. button morphApplyButton "Apply Changes" tooltip:"Applies the scale and name changes to the selected morph."
  149. button morphCreateButton "Create" tooltip:"Creates a morph with the specified name and data, if no name is specified it will be given one"
  150.  
  151. group "Vertex Data"
  152. (
  153. button loadFromSelection "Load From Selection" tooltip:"Loads the vertex data from the selectedobject onto the selected morph."
  154. button loadFromMorpher "Load From Morpher" tooltip:"Loads the vertex data from the Morpher of the selected object with the same Channel name as the selected morph."
  155. )
  156.  
  157. fn updateMorphSelection tIndex mIndex =
  158. (
  159. try
  160. (
  161. local morphData = triArray[tIndex].morphs[mIndex]
  162. if morphData != undefined then
  163. (
  164. editMorphNameEdit.text = morphData.mname
  165. editMorphScaleEdit.value = morphData.multiplier
  166. )
  167. )
  168. catch
  169. (
  170. editMorphNameEdit.text = ""
  171. editMorphScaleEdit.value = 0.0000000001
  172. )
  173. )
  174.  
  175. fn updateTRISelection tIndex =
  176. (
  177. free selectedMorphs.items
  178. selectedMorphs.items = #()
  179.  
  180. try
  181. (
  182. for mData in triArray[tIndex].morphs do
  183. (
  184. selectedMorphs.items = append selectedMorphs.items mData.mname
  185. )
  186.  
  187. updateMorphSelection tIndex selectedMorphs.selection
  188. )
  189. catch
  190. (
  191. updateMorphSelection undefined undefined
  192. )
  193. )
  194.  
  195. on selectedMorphs selected i do
  196. (
  197. updateMorphSelection currentIndex i
  198. )
  199.  
  200. on morphApplyButton pressed do
  201. (
  202. local mIndex = selectedMorphs.selection
  203. try
  204. (
  205. local newName = editMorphNameEdit.text
  206. if newName != "" then
  207. (
  208. local targetMorph = triArray[currentIndex].morphs[mIndex]
  209. if targetMorph != undefined then
  210. (
  211. targetMorph.mname = newName
  212. selectedMorphs.selected = newName
  213. )
  214. )
  215.  
  216. updateMorphSelection currentIndex mIndex
  217. )
  218. catch
  219. MessageBox(GetCurrentException())
  220. )
  221.  
  222. on morphRemoveButton pressed do
  223. (
  224. local mIndex = selectedMorphs.selection
  225. try
  226. (
  227. local fileInfo = triArray[currentIndex]
  228. local targetMorph = fileInfo.morphs[mIndex]
  229. if targetMorph != undefined then
  230. (
  231. fileInfo.morphs = deleteItem fileInfo.morphs mIndex
  232. selectedMorphs.items = deleteItem selectedMorphs.items mIndex
  233. fileInfo.nummorphs = fileInfo.morphs.count
  234. )
  235.  
  236. updateMorphSelection currentIndex selectedMorphs.selection
  237. )
  238. catch
  239. MessageBox(GetCurrentException())
  240. )
  241.  
  242. on morphCreateButton pressed do
  243. (
  244. try
  245. (
  246. local newName = editMorphNameEdit.text
  247. if newName != "" then
  248. (
  249. local fileInfo = triArray[currentIndex]
  250. local items = selectedMorphs.items
  251. if (appendIfUnique items newName) == true then
  252. (
  253. local newMorph = TRIMorph()
  254. newMorph.mname = newName
  255. newMorph.vertices = #()
  256. for i=0 to fileInfo.header.vertices do
  257. (
  258. vert=[0,0,0]
  259. append newMorph.vertices vert
  260. )
  261. append fileInfo.morphs newMorph
  262. fileInfo.nummorphs = fileInfo.morphs.count
  263. selectedMorphs.items = items
  264. )
  265. else
  266. (
  267. throw("Morph " + newName + " already exists.")
  268. )
  269. )
  270. )
  271. catch
  272. MessageBox(GetCurrentException())
  273. )
  274.  
  275. on loadFromSelection pressed do
  276. (
  277. local mIndex = selectedMorphs.selection
  278. try
  279. (
  280. local targetMorph = triArray[currentIndex].morphs[mIndex]
  281. if targetMorph == undefined then
  282. throw "No morph selected."
  283.  
  284. local referenceNode = GetWorkingNode()
  285. if referenceNode == $ then
  286. throw "Select a working reference in TRI selector, source and destination are the same."
  287.  
  288. vertexDifferenceToMorph $ referenceNode targetMorph
  289. updateMorphSelection currentIndex mIndex
  290. messagebox("Loaded vertex data from selection onto " + targetMorph.mname)
  291. )
  292. catch
  293. MessageBox(GetCurrentException())
  294. )
  295. on loadFromMorpher pressed do
  296. (
  297. local mIndex = selectedMorphs.selection
  298. try
  299. (
  300. local targetMorph = triArray[currentIndex].morphs[mIndex]
  301. if targetMorph == undefined then
  302. throw "No morph selected."
  303.  
  304. local referenceNode = GetWorkingNode()
  305. if referenceNode == $ then
  306. throw "Select a working reference in TRI selector, source and destination are the same."
  307.  
  308. local morpherPair = findMorpherByName $ targetMorph.mname
  309. if morpherPair != undefined then
  310. (
  311. WM3_MC_SetValue morphPair.morphMod morphPair.channelIndex 100.0
  312. vertexDifferenceToMorph $ referenceNode targetMorph
  313. WM3_MC_SetValue morphPair.morphMod morphPair.channelIndex 0.0
  314. messagebox("Loaded vertex data from target morpher onto " + targetMorph.mname)
  315. )
  316. )
  317. catch
  318. MessageBox(GetCurrentException())
  319. )
  320. )
  321.  
  322. rollout triEditorRollout "TRI Selector"
  323. (
  324. pickbutton selectReference "Select Reference" autoDisplay:true
  325. group "Batch"
  326. (
  327. progressBar transferProgress
  328. button applyTRIs "Create Modifiers From Files" tooltip:"Creates a Morph Modifier named after all of the loaded TRI files and adds all of their morphs to the Morph Channels, if the TRI has more than 100 morphs it will create a new Morph Modifier with an appended number. If a reference is selected, all Modifiers will be added to that object. If no reference is selected it will create a new mesh per file."
  329. button createTRIs "Create Files From Modifiers" tooltip:"(Not recommended) Creates new TRI files from a selected meshes Morph Modifiers. If a selected reference is chosen they will be taken from that object. If no selected reference is chosen, they will be taken from the currently selected object."
  330. button loadTRIMorphs "Load Morphs From Modifiers" tooltip:"Loads the vertex data from all Modifiers of the selected reference, or currently selected object to their associated TRI morphs if the associated file is loaded."
  331. )
  332. ListBox loadedTris "Files" labelOnTop:true
  333. button removeFile "Remove Selected" tooltip:"Removes the currently selected loaded TRI file."
  334. button clearFiles "Clear" tooltip:"Removes all loaded TRI files."
  335. editText renameEdit "Rename" labelOnTop:true
  336. button applyRename "Apply" tooltip:"Applies the rename to the selected tri file."
  337. /*dotNetControl tv "TreeView" height:290 align:#center
  338.  
  339. fn addChild parent objName =
  340. (
  341. local child = parent.Nodes.add objName
  342. local fgColor = colorMan.getColor #text
  343. child.forecolor = (dotNetClass "System.Drawing.Color").fromARGB (fgColor.x * 255) (fgColor.y * 255) (fgColor.z * 255)
  344. return child
  345. )
  346.  
  347. on triEditorRollout open do
  348. (
  349. local bgColor = colorMan.getColor #background
  350. tv.backcolor = (dotNetClass "System.Drawing.Color").fromARGB (bgColor.x * 255) (bgColor.y * 255) (bgColor.z * 255)
  351.  
  352. root = addChild tv "Root"
  353.  
  354. addChild root "Child1"
  355. addChild root "Child1"
  356. addChild root "Child1"
  357. addChild root "Child1"
  358. addChild root "Child1"
  359. addChild root "Child1"
  360. addChild root "Child1"
  361. addChild root "Child1"
  362. addChild root "Child1"
  363. addChild root "Child1"
  364. addChild root "Child1"
  365. addChild root "Child1"
  366. addChild root "Child1"
  367. addChild root "Child1"
  368. addChild root "Child1"
  369. addChild root "Child1"
  370. addChild root "Child1"
  371. addChild root "Child1"
  372. addChild root "Child1"
  373. addChild root "Child1"
  374. addChild root "Child1"
  375. addChild root "Child1"
  376. addChild root "Child1"
  377. addChild root "Child1"
  378. addChild root "Child1"
  379. addChild root "Child1"
  380. addChild root "Child1"
  381. )
  382. */
  383. group "Info"
  384. (
  385. editText flagsEdit "Flags" readOnly:true
  386. editText numVerticesEdit "Vertices" readOnly:true
  387. editText numTriangleEdit "Faces" readOnly:true
  388. editText numUVVerticesEdit "UV Vertices" readOnly:true
  389. editText numMorphsEdit "Morphs" readOnly:true
  390. )
  391.  
  392. fn addFile fileName fileInfo =
  393. (
  394. loadedTris.items = append loadedTris.items fileName
  395. append triArray fileInfo
  396. )
  397.  
  398. fn updateTRIInfo tIndex =
  399. (
  400. try
  401. (
  402. flagsEdit.text = triArray[tIndex].header.flags as string
  403. numVerticesEdit.text = triArray[tIndex].header.vertices as string
  404. numTriangleEdit.text = triArray[tIndex].header.polytris as string
  405. numUVVerticesEdit.text = triArray[tIndex].header.uvvertices as string
  406. numMorphsEdit.text = triArray[tIndex].header.nummorphs as string
  407. renameEdit.text = loadedTris.items[tIndex]
  408. )
  409. catch
  410. (
  411. flagsEdit.text = ""
  412. numVerticesEdit.text = ""
  413. numTriangleEdit.text = ""
  414. numUVVerticesEdit.text = ""
  415. numMorphsEdit.text = ""
  416. renameEdit.text = ""
  417. )
  418. )
  419.  
  420. on loadedTris selected i do
  421. (
  422. currentIndex = i
  423. try
  424. currentFile = triArray[currentIndex]
  425. catch
  426. currentFile = undefined
  427.  
  428. updateTRIInfo currentIndex
  429. morphEditorRollout.updateTRISelection currentIndex
  430. )
  431.  
  432. on loadedTris doubleClicked i do
  433. (
  434. currentIndex = i
  435. try
  436. currentFile = triArray[currentIndex]
  437. catch
  438. currentFile = undefined
  439.  
  440. updateTRIInfo currentIndex
  441. morphEditorRollout.updateTRISelection currentIndex
  442.  
  443. try
  444. createMeshFromTRI currentFile loadedTris.items[currentIndex]
  445. catch
  446. MessageBox(GetCurrentException())
  447. )
  448.  
  449. on removeFile pressed do
  450. (
  451. triArray = deleteItem triArray loadedTris.selection
  452. loadedTris.items = deleteItem loadedTris.items loadedTris.selection
  453. currentIndex = loadedTris.selection
  454. try
  455. currentFile = triArray[currentIndex]
  456. catch
  457. currentFile = undefined
  458. updateTRIInfo currentIndex
  459. morphEditorRollout.updateTRISelection currentIndex
  460. )
  461.  
  462. on clearFiles pressed do
  463. (
  464. free triArray
  465. triArray = #()
  466. free loadedTris.items
  467. loadedTris.items = #()
  468. currentFile = undefined
  469. currentIndex = undefined
  470. updateTRIInfo currentIndex
  471. morphEditorRollout.updateTRISelection currentIndex
  472. )
  473.  
  474. on applyTRIs pressed do
  475. (
  476. try
  477. (
  478. for i=1 to loadedTris.items.count do
  479. (
  480. local referenceNode = GetWorkingNode()
  481. createMorpher referenceNode triArray[i] loadedTris.items[i] transferProgress
  482. )
  483.  
  484. MessageBox("Finished creating Morphers from TRI file(s).")
  485. )
  486. catch
  487. (
  488. MessageBox(GetCurrentException())
  489. )
  490. )
  491. on createTRIs pressed do
  492. (
  493. try
  494. (
  495. local referenceNode = GetWorkingNode()
  496. local morpherList = buildMorpherList referenceNode
  497. if morpherList.count == 0 then
  498. throw "Active node has no morphers attached, or none that match the following pattern: (*.tri)"
  499. for u in morpherList do
  500. (
  501. local newFile = TRIFile()
  502. newFile.header = TRIHeader()
  503.  
  504. local morphList = #()
  505. for m in u do
  506. (
  507. for c=1 to 100 do
  508. (
  509. if WM3_MC_HasData m c then
  510. (
  511. local morphName = WM3_MC_GetName m c
  512.  
  513. local newMorph = TRIMorph()
  514. newMorph.mname = morphName
  515. multiplier = 0
  516. vertices = #()
  517. append morphList newMorph
  518. )
  519. )
  520. )
  521.  
  522. newFile.header.nummorphs = morphList.count
  523. newFile.header.flags = 1
  524. newFile.morphs = morphList
  525.  
  526. -- Add the reference to the new TRI file, inits remaining data
  527. replaceTRIReference referenceNode newFile
  528.  
  529. local tempName = u[1].name + "_temp"
  530. snapshot referenceNode name:tempName isSelected:false
  531. local sourceNode = GetNodeByName tempName
  532.  
  533. -- Copies the morpher data into the morph
  534. for m in u do
  535. (
  536. transferProgress.value = 0
  537. for c=1 to 100 do
  538. (
  539. if WM3_MC_HasData m c then
  540. (
  541. local morphName = WM3_MC_GetName m c
  542. local targetMorph = findMorphByName newFile morphName
  543. if targetMorph != undefined then
  544. (
  545. WM3_MC_SetValue m c 100.0
  546. vertexDifferenceToMorph sourceNode referenceNode targetMorph
  547. WM3_MC_SetValue m c 0.0
  548. )
  549. )
  550. transferProgress.value = c
  551. if mod c 10 == 0 then
  552. windows.processPostedMessages()
  553. )
  554. transferProgress.value = 0
  555. )
  556.  
  557. delete sourceNode
  558. select referenceNode
  559.  
  560. -- Add the new TRI to the loaded TRI list
  561. addFile u[1].name newFile
  562. )
  563.  
  564. MessageBox("Finished creating TRI file(s) from Morphers.")
  565. )
  566. catch
  567. (
  568. MessageBox(GetCurrentException())
  569. )
  570. )
  571.  
  572. on loadTRIMorphs pressed do
  573. (
  574. try
  575. (
  576. for i=1 to loadedTris.items.count do
  577. (
  578. local referenceNode = GetWorkingNode()
  579. loadMorphsFromModifiers referenceNode triArray[i] loadedTris.items[i] transferProgress
  580. )
  581.  
  582. MessageBox("Finished loading Morpher data.")
  583. )
  584. catch
  585. (
  586. MessageBox(GetCurrentException())
  587. )
  588. )
  589.  
  590. on selectReference rightclick do
  591. (
  592. selectReference.object = undefined
  593. )
  594.  
  595. on applyRename pressed do
  596. (
  597. try
  598. (
  599. if renameEdit.text != "" then
  600. loadedTris.selected = renameEdit.text
  601. )
  602. catch
  603. MessageBox("No tri selected.")
  604. )
  605. )
  606.  
  607. rollout importRollout "Import"
  608. (
  609. editText importFilePath "Import File Path" labelOnTop:true
  610. button importFilePathButton "Browse..." tooltip:"Searches the specified path for TRI files."
  611. checkbox recursiveImport "Recursive" checked:true tooltip:"Searches all sub directories for TRI files."
  612. Multilistbox displayedFiles "Files" labelOnTop:true
  613. button loadSelectedButton "Load Selected" tooltip:"Loads the selected TRI files."
  614. button clearImport "Clear" tooltip:"Clears all displayed TRI files found in the import path."
  615.  
  616. fn getFilesRecursive fileArray currentPath =
  617. (
  618. for f in getFiles (currentPath + "*.tri") do
  619. (
  620. appendIfUnique fileArray f
  621. )
  622. for d in getDirectories (currentPath + "\\*") do
  623. (
  624. getFilesRecursive fileArray d
  625. )
  626. )
  627.  
  628. on importFilePathButton pressed do
  629. (
  630. importPath = getSavePath caption:"Select Folder" initialDir:importFilePath.text
  631. if importPath != undefined do
  632. (
  633. importFilePath.text = importPath + "\\"
  634. allFiles = getFiles (importPath + "\\*.tri")
  635. if recursiveImport.checked == true then
  636. (
  637. for d in getDirectories (importPath + "\\*") do
  638. (
  639. getFilesRecursive allFiles d
  640. )
  641. )
  642.  
  643. sort allFiles
  644. for i=1 to allFiles.count do
  645. (
  646. allFiles[i] = substring allFiles[i] (importFilePath.text.count+1) -1
  647. )
  648.  
  649. displayedFiles.items = allFiles
  650. )
  651. )
  652.  
  653. on loadSelectedButton pressed do
  654. (
  655. if displayedFiles.items.count > 0 and displayedFiles.selection.count > 0 then
  656. (
  657. for i in displayedFiles.selection do
  658. (
  659. try
  660. (
  661. local filePath = importFilePath.text + displayedFiles.items[i]
  662.  
  663. local newFile = TRIFile()
  664. newFile.header = TRIHeader()
  665. readTRIFile filePath newFile
  666.  
  667. triEditorRollout.addFile displayedFiles.items[i] newFile
  668.  
  669. currentFile = newFile
  670. currentIndex = triEditorRollout.loadedTris.selection
  671. )
  672. catch
  673. (
  674. MessageBox(GetCurrentException())
  675. )
  676. )
  677.  
  678. MessageBox("Finished loading selected files.")
  679. )
  680. )
  681.  
  682. on clearImport pressed do
  683. (
  684. free displayedFiles.items
  685. displayedFiles.items = #()
  686. )
  687. )
  688.  
  689. fn replaceTRIReference referenceNode fileInfo =
  690. (
  691. fileInfo.header.vertices=meshop.getnumverts referenceNode
  692. fileInfo.header.uvvertices=fileInfo.header.vertices
  693. fileInfo.header.polytris=meshop.getnumfaces referenceNode
  694.  
  695. fileInfo.vertices = #()
  696. fileInfo.faces = #()
  697. fileInfo.texCoords = #()
  698. fileInfo.faceTex = #()
  699.  
  700. for v=1 to fileInfo.header.vertices do
  701. (
  702. mvert=meshop.getVert referenceNode v
  703. append fileInfo.vertices mvert
  704. )
  705.  
  706. for f=1 to fileInfo.header.polytris do
  707. (
  708. mface=getFace referenceNode f
  709. x=mface.x
  710. y=mface.y
  711. z=mface.z
  712. ol=[x,y,z]
  713. append fileInfo.faces ol
  714. )
  715.  
  716. --texture vertex
  717. for vt = 1 to fileInfo.header.vertices do
  718. (
  719. tv=getTVert referenceNode vt
  720. x=tv.x as float
  721. y=tv.y as float
  722. z=tv.z as float
  723. vers=[x,y,z]
  724. append fileInfo.texCoords vers
  725. )
  726.  
  727. for vt = 1 to fileInfo.header.polytris do
  728. (
  729. tv=getTVFace referenceNode vt
  730. x=tv.x
  731. y=tv.y
  732. z=tv.z
  733. vers=[x,y,z]
  734. append fileInfo.faceTex vers
  735. )
  736.  
  737. for m in fileInfo.morphs do
  738. (
  739. m.vertices = #()
  740. for i=1 to fileInfo.header.vertices do
  741. (
  742. vert=[0,0,0]
  743. append m.vertices vert
  744. )
  745. )
  746. )
  747.  
  748. fn buildMorpherList referenceNode =
  749. (
  750. uniqueList = #()
  751. morpherList = #()
  752. for m in referenceNode.modifiers do
  753. if isKindOf m Morpher then
  754. morpherList = append morpherList m
  755.  
  756. parentPattern = "*.tri"
  757. childPattern = "*.tri_*"
  758.  
  759. for m in morpherList do
  760. (
  761. if matchPattern m.name pattern:parentPattern ignoreCase:true == true and matchPattern m.name pattern:childPattern ignoreCase:true == false then
  762. (
  763. childList = #()
  764. childList = append childList m
  765. uniqueList = append uniqueList childList
  766. )
  767. )
  768.  
  769. for u in uniqueList do
  770. (
  771. for a=2 to 5 do
  772. (
  773. mName = (u[1].name + "_" + a as string)
  774. for m in morpherList do
  775. if m.name == mName then
  776. u = append u m
  777. )
  778. )
  779.  
  780. return uniqueList
  781. )
  782.  
  783. fn findMorphByName fileInfo morphName =
  784. (
  785. for m in fileInfo.morphs do
  786. (
  787. if m.mname == morphName then
  788. return m
  789. )
  790.  
  791. return undefined
  792. )
  793.  
  794. fn findMorpherByName referenceNode morphName =
  795. (
  796. for m in referenceNode.modifiers do
  797. (
  798. if isKindOf m Morpher then
  799. (
  800. for c=1 to 100 do
  801. (
  802. if WM3_MC_HasData m c then
  803. (
  804. local localName = WM3_MC_GetName m c
  805. if localName == morphName then
  806. (
  807. local morpherPair = MorphPair()
  808. morpherPair.morphMod = m
  809. morpherPair.channelIndex = c
  810. return morpherPair
  811. )
  812. )
  813. )
  814. )
  815. )
  816.  
  817. return undefined
  818. )
  819.  
  820. fn vertexDifferenceToMorph sourceNode targetNode targetMorph =
  821. (
  822. local numVerts = meshop.getnumverts sourceNode
  823. local maxVertexDisplacement = 0
  824. for v=1 to numVerts do
  825. (
  826. local sVert = meshop.getVert sourceNode v
  827. local tVert = meshop.getVert targetNode v
  828. local mx = (tVert.x - sVert.x)
  829. local my = (tVert.y - sVert.y)
  830. local mz = (tVert.z - sVert.z)
  831.  
  832. if abs(mx) > maxVertexDisplacement then
  833. maxVertexDisplacement = abs(mx)
  834. if abs(my) > maxVertexDisplacement then
  835. maxVertexDisplacement = abs(my)
  836. if abs(mz) > maxVertexDisplacement then
  837. maxVertexDisplacement = abs(mz)
  838.  
  839. targetMorph.vertices[v] = [mx, my, mz]
  840. )
  841.  
  842. local baseDifference = maxVertexDisplacement / 0x7FFF
  843. for v=1 to numVerts do
  844. (
  845. targetMorph.vertices[v].x /= baseDifference
  846. targetMorph.vertices[v].y /= baseDifference
  847. targetMorph.vertices[v].z /= baseDifference
  848. )
  849. targetMorph.multiplier = baseDifference
  850. )
  851.  
  852. fn loadMorphsFromModifiers referenceNode fileInfo morpherName transferProgress =
  853. (
  854. if fileInfo.header.vertices != meshop.getnumverts referenceNode then
  855. (
  856. if QueryBox (morpherName + " has a different vertex count from the reference node (" + referenceNode.name + "), would you like to replace it?") title:"Vertex mismatch" then
  857. (
  858. replaceTRIReference referenceNode fileInfo
  859. )
  860. else
  861. return false
  862. )
  863.  
  864. local tempName = (morpherName + "_" + (random 0x000000 0xFFFFFF) as string)
  865. snapshot referenceNode name:tempName isSelected:false
  866. local sourceNode = GetNodeByName tempName
  867.  
  868. local matchingMorphers = buildMorpherList referenceNode
  869. for u in matchingMorphers do
  870. (
  871. for m in u do
  872. (
  873. transferProgress.value = 0
  874. for c=1 to 100 do
  875. (
  876. if WM3_MC_HasData m c then
  877. (
  878. local morphName = WM3_MC_GetName m c
  879. local targetMorph = findMorphByName fileInfo morphName
  880. if targetMorph != undefined then
  881. (
  882. WM3_MC_SetValue m c 100.0
  883. vertexDifferenceToMorph sourceNode referenceNode targetMorph
  884. WM3_MC_SetValue m c 0.0
  885. )
  886. )
  887. transferProgress.value = c
  888. if mod c 10 == 0 then
  889. windows.processPostedMessages()
  890. )
  891. transferProgress.value = 0
  892. )
  893. )
  894.  
  895. delete sourceNode
  896. select referenceNode
  897. return true
  898. )
  899.  
  900. fn createMeshFromTRI fileInfo objectName=
  901. (
  902. o=mesh vertices:fileInfo.vertices faces:fileInfo.faces
  903. setNumTVerts o fileInfo.texCoords.count
  904. for i = 1 to fileInfo.header.uvvertices do
  905. (
  906. setTVert o i fileInfo.texCoords[i]--set texture vertice ID- to 3point array
  907. )
  908. buildTVFaces o--enable texture faces arrays
  909. for i = 1 to fileInfo.header.polytris do
  910. (
  911. setTVFace o i fileInfo.faceTex[i]
  912. )
  913.  
  914. o.name=objectName
  915. obj = GetNodeByName objectName
  916. select obj
  917. modPanel.addModToSelection (UVW_Xform ()) ui:on
  918. obj.modifiers[#UVW_Xform].V_Flip = 1
  919. macros.run "Modifier Stack" "Convert_to_Mesh"
  920. )
  921.  
  922. fn createMorpher referenceNode fileInfo morpherName transferProgress =
  923. (
  924. if fileInfo.morphs.count > 0 then
  925. (
  926. transferProgress.value = 0
  927.  
  928. local iterations = 1
  929. if ((mod fileInfo.morphs.count 100) > 0) or ((fileInfo.morphs.count / 100) > 1) then
  930. (
  931. iterations = (fileInfo.morphs.count / 100) + 1
  932. )
  933.  
  934. local start = referenceNode
  935. for iter=1 to iterations do
  936. (
  937. local newMorpher = Morpher()
  938. newMorpher.name = morpherName
  939. if iter > 1 then
  940. newMorpher.name += "_" + iter as string
  941.  
  942. addModifier start newMorpher
  943.  
  944. local begin = (((iter-1) * 100) + 1) as integer
  945. local end = (iter * 100) as integer
  946.  
  947. if iter == iterations and (mod fileInfo.morphs.count 100) > 0 then -- Add the remaining entries
  948. (
  949. end = ((iter-1) * 100) + (mod fileInfo.morphs.count 100) as integer
  950. )
  951.  
  952. for i=begin to end do
  953. (
  954. local modChannel = (mod i 100) as integer
  955. if (mod i 100) == 0 then
  956. modChannel = (iter * 100) as integer
  957.  
  958. local tempName = fileInfo.morphs[i].mname
  959. snapshot start name:tempName isSelected:false
  960. local newMesh = GetNodeByName tempName
  961. for v=1 to fileInfo.vertices.count do
  962. (
  963. local mvert=meshop.getVert newMesh v
  964. local a=mvert.x + fileInfo.morphs[i].vertices[v].x * fileInfo.morphs[i].multiplier
  965. local b=mvert.y + fileInfo.morphs[i].vertices[v].y * fileInfo.morphs[i].multiplier
  966. local c=mvert.z + fileInfo.morphs[i].vertices[v].z * fileInfo.morphs[i].multiplier
  967. local morpi=[a,b,c]
  968. meshop.setVert newMesh v morpi
  969. )
  970. WM3_MC_BuildFromNode newMorpher modChannel newMesh
  971. delete newMesh
  972. select start
  973. transferProgress.value = ((i as float / fileInfo.morphs.count as float) * 100.0) as integer
  974.  
  975. if mod i 10 == 0 then
  976. windows.processPostedMessages()
  977. )
  978. )
  979.  
  980. transferProgress.value = 0
  981. select start
  982. )
  983. )
  984.  
  985. fn writeTRIFile filePath fileInfo =
  986. (
  987. local f
  988. try
  989. (
  990. ascii_encoder = dotNetObject "System.Text.ASCIIEncoding"
  991. bytes = ascii_encoder.GetBytes ( "FRTRI003" )
  992. f=fopen filePath "wb"
  993. WriteByte f bytes[1] #unsigned
  994. WriteByte f bytes[2] #unsigned
  995. WriteByte f bytes[3] #unsigned
  996. WriteByte f bytes[4] #unsigned
  997. WriteByte f bytes[5] #unsigned
  998. WriteByte f bytes[6] #unsigned
  999. WriteByte f bytes[7] #unsigned
  1000. WriteByte f bytes[8] #unsigned
  1001. WriteLong f fileInfo.header.vertices #unsigned
  1002. WriteLong f fileInfo.header.polytris #unsigned
  1003. WriteLong f fileInfo.header.polyquads #unsigned
  1004. WriteLong f fileInfo.header.unknown2 #unsigned
  1005. WriteLong f fileInfo.header.unknown3 #unsigned
  1006. WriteLong f fileInfo.header.uvvertices #unsigned
  1007. WriteLong f fileInfo.header.flags #unsigned
  1008. WriteLong f fileInfo.header.nummorphs #unsigned
  1009. WriteLong f fileInfo.header.nummodifiers #unsigned
  1010. WriteLong f fileInfo.header.modvertices #unsigned
  1011. WriteLong f fileInfo.header.unknown7 #unsigned
  1012. WriteLong f fileInfo.header.unknown8 #unsigned
  1013. WriteLong f fileInfo.header.unknown9 #unsigned
  1014. WriteLong f fileInfo.header.unknown10 #unsigned
  1015.  
  1016. for a=1 to fileInfo.header.vertices do
  1017. (
  1018. WriteFloat f fileInfo.vertices[a].x
  1019. WriteFloat f fileInfo.vertices[a].y
  1020. WriteFloat f fileInfo.vertices[a].z
  1021. )
  1022.  
  1023. for b=1 to fileInfo.header.polytris do
  1024. (
  1025. x=fileInfo.faces[b].x
  1026. y=fileInfo.faces[b].y
  1027. z=fileInfo.faces[b].z
  1028. ver = [x-1, y-1, z-1]
  1029. WriteLong f ver.x #unsigned
  1030. WriteLong f ver.y #unsigned
  1031. WriteLong f ver.z #unsigned
  1032. )
  1033.  
  1034. if fileInfo.header.uvvertices > 0 then
  1035. (
  1036. for d=1 to fileInfo.header.uvvertices do
  1037. (
  1038. WriteFloat f fileInfo.texCoords[d].x
  1039. WriteFloat f fileInfo.texCoords[d].y
  1040. )
  1041. )
  1042.  
  1043. for el=1 to fileInfo.header.polytris do
  1044. (
  1045. x=fileInfo.faceTex[el].x
  1046. y=fileInfo.faceTex[el].y
  1047. z=fileInfo.faceTex[el].z
  1048. ver = [x-1, y-1, z-1]
  1049. WriteLong f ver.x #unsigned
  1050. WriteLong f ver.y #unsigned
  1051. WriteLong f ver.z #unsigned
  1052. )
  1053.  
  1054. for k=1 to fileInfo.header.nummorphs do
  1055. (
  1056. WriteFixedString f fileInfo.morphs[k].mname
  1057. WriteFloat f fileInfo.morphs[k].multiplier
  1058. for p=1 to fileInfo.header.vertices do
  1059. (
  1060. WriteShort f fileInfo.morphs[k].vertices[p].x #signed
  1061. WriteShort f fileInfo.morphs[k].vertices[p].y #signed
  1062. WriteShort f fileInfo.morphs[k].vertices[p].z #signed
  1063. )
  1064. )
  1065. fclose f
  1066. )
  1067. catch
  1068. (
  1069. fclose f
  1070. throw
  1071. )
  1072. )
  1073.  
  1074. fn readTRIFile filePath fileInfo =
  1075. (
  1076. local f
  1077. try
  1078. (
  1079. fileInfo.header = TRIHeader()
  1080. f = fopen filePath "rb"
  1081. fseek f 0x0008 #seek_cur
  1082. fileInfo.header.vertices=ReadLong f #unsigned
  1083. fileInfo.header.polytris=ReadLong f #unsigned--// number of tri faces
  1084. fileInfo.header.polyquads=ReadLong f #unsigned--// number of quad faces
  1085. fileInfo.header.unknown2=ReadLong f #unsigned
  1086. fileInfo.header.unknown3=ReadLong f #unsigned
  1087. fileInfo.header.uvvertices=ReadLong f #unsigned--// number of uv map vertices
  1088. fileInfo.header.flags=ReadLong f #unsigned--// flags?
  1089. --// bit 0 must be 1 or crashes
  1090. --// bit 1 true=don't display face
  1091. ---// bit 2 no effect?
  1092. fileInfo.header.nummorphs=ReadLong f #unsigned--// number of morphs
  1093. fileInfo.header.nummodifiers=ReadLong f #unsigned--// number of modifiers
  1094. fileInfo.header.modvertices=ReadLong f #unsigned--// number of vertices to replace by modifiers
  1095. fileInfo.header.unknown7=ReadLong f #unsigned
  1096. fileInfo.header.unknown8=ReadLong f #unsigned
  1097. fileInfo.header.unknown9=ReadLong f #unsigned
  1098. fileInfo.header.unknown10=ReadLong f #unsigned
  1099.  
  1100. for a=1 to fileInfo.header.vertices do
  1101. (
  1102. x=ReadFloat f
  1103. y=ReadFloat f
  1104. z=ReadFloat f
  1105. ver = [x, y, z]
  1106. append fileInfo.vertices ver
  1107. )
  1108.  
  1109. for b=1 to fileInfo.header.polytris do
  1110. (
  1111. x=ReadLong f #unsigned
  1112. y=ReadLong f #unsigned
  1113. z=ReadLong f #unsigned
  1114. ver = [x+1, y+1, z+1]
  1115. append fileInfo.faces ver
  1116. )
  1117.  
  1118. if fileInfo.header.uvvertices > 0 then
  1119. (
  1120. for c=1 to fileInfo.header.uvvertices do
  1121. (
  1122. u=ReadFloat f
  1123. v=ReadFloat f
  1124. ver = [u,v,0]
  1125. append fileInfo.texCoords ver
  1126. )
  1127. )
  1128.  
  1129. for d=1 to fileInfo.header.polytris do
  1130. (
  1131. x=ReadLong f #unsigned
  1132. y=ReadLong f #unsigned
  1133. z=ReadLong f #unsigned
  1134. ver = [x+1, y+1, z+1]
  1135. append fileInfo.faceTex ver
  1136. )
  1137.  
  1138. if fileInfo.header.nummorphs > 0 then
  1139. (
  1140. for i=1 to fileInfo.header.nummorphs do
  1141. (
  1142. morphData=TRIMorph()
  1143. morphData.mname=ReadFixedString f
  1144. morphData.multiplier=ReadFloat f
  1145. for l=1 to fileInfo.header.vertices do
  1146. (
  1147. x=ReadShort f #signed
  1148. y=ReadShort f #signed
  1149. z=ReadShort f #signed
  1150.  
  1151. vers=[x ,y ,z ]
  1152. append morphData.vertices vers
  1153. )
  1154.  
  1155. append fileInfo.morphs morphData
  1156. )
  1157. )
  1158. fclose f
  1159. )
  1160. catch
  1161. (
  1162. fclose f
  1163. throw
  1164. )
  1165. )
  1166.  
  1167. fn ReadFixedString bstream =
  1168. (
  1169. local str = ""
  1170. local len =ReadLong bstream #unsigned
  1171. for i = 1 to len do
  1172. (
  1173. str += bit.intAsChar (ReadByte bstream #unsigned)
  1174. )
  1175. return str
  1176. )
  1177.  
  1178. fn WriteFixedString bstream str =
  1179. (
  1180. if str.count == 0 then
  1181. (
  1182. WriteLong bstream 0 #unsigned
  1183. )
  1184. else
  1185. (
  1186. WriteLong bstream (str.count+1) #unsigned
  1187. for i = 1 to str.count do
  1188. (
  1189. WriteByte bstream (bit.CharAsInt(str[i])) #unsigned
  1190. )
  1191. WriteByte bstream 0 #unsigned
  1192. )
  1193. )
  1194.  
  1195. rollout morphTransferRollout "Morph Transfer"
  1196. (
  1197. pickbutton selectOriginal "Original head" autoDisplay:true
  1198. pickbutton selectTransfer "Target Head" autoDisplay:true
  1199. button bakeTransfer "Transfer"
  1200. progressBar transferProgress
  1201.  
  1202. on selectOriginal rightclick do
  1203. (
  1204. selectOriginal.object = undefined
  1205. )
  1206.  
  1207. on selectTransfer rightclick do
  1208. (
  1209. selectTransfer.object = undefined
  1210. )
  1211.  
  1212. fn GetOriginalHead =
  1213. (
  1214. local object = undefined
  1215. try
  1216. (
  1217. object = selectOriginal.object
  1218. if isValidNode object == false then
  1219. selectOriginal.object = undefined
  1220. )
  1221. catch
  1222. (
  1223. object = undefined
  1224. )
  1225.  
  1226. if isValidNode object == false then
  1227. throw "No original object selected."
  1228.  
  1229. return object
  1230. )
  1231.  
  1232. fn GetTransferHead =
  1233. (
  1234. local object = undefined
  1235. try
  1236. (
  1237. object = selectTransfer.object
  1238. if isValidNode object == false then
  1239. selectTransfer.object = undefined
  1240. )
  1241. catch
  1242. (
  1243. object = undefined
  1244. )
  1245.  
  1246. if isValidNode object == false then
  1247. throw "No transfer object selected."
  1248.  
  1249. return object
  1250. )
  1251.  
  1252. on bakeTransfer pressed do
  1253. (
  1254. try
  1255. (
  1256. local originalHead = GetOriginalHead()
  1257. local transferHead = GetTransferHead()
  1258.  
  1259. if originalHead == transferHead then
  1260. throw "Original head is the same as the transfer head."
  1261.  
  1262. local totalMorphers = 0
  1263. local morphers = #()
  1264. for i=originalHead.modifiers.count to 1 by -1 do
  1265. (
  1266. local m = originalHead.modifiers[i]
  1267. if isKindOf m Morpher then
  1268. (
  1269. for c=1 to 100 do
  1270. (
  1271. if WM3_MC_HasData m c then
  1272. (
  1273. totalMorphers += 1
  1274. )
  1275. )
  1276. )
  1277. )
  1278.  
  1279. if totalMorphers == 0 then
  1280. throw "Original head has no Morpher with data to transfer."
  1281.  
  1282. local currentMorpher = 0
  1283. transferProgress.value = 0
  1284. for i=originalHead.modifiers.count to 1 by -1 do
  1285. (
  1286. m = originalHead.modifiers[i]
  1287. if isKindOf m Morpher then
  1288. (
  1289. local newMorpher = Morpher()
  1290. newMorpher.name = m.name
  1291.  
  1292. addModifier transferHead newMorpher
  1293. for c=1 to 100 do
  1294. (
  1295. if WM3_MC_HasData m c then
  1296. (
  1297. local morphName = WM3_MC_GetName m c
  1298. WM3_MC_SetValue m c 100.0
  1299. snapshot transferHead name:morphName
  1300. local newMesh = GetNodeByName morphName
  1301. WM3_MC_BuildFromNode newMorpher c newMesh
  1302. WM3_MC_SetValue m c 0.0
  1303. delete newMesh
  1304. currentMorpher += 1
  1305. transferProgress.value = ((currentMorpher as float / totalMorphers as float) * 100.0) as integer
  1306.  
  1307. if mod currentMorpher 10 == 0 then
  1308. windows.processPostedMessages()
  1309. )
  1310. )
  1311. )
  1312. )
  1313. transferProgress.value = 0
  1314. )
  1315. catch
  1316. (
  1317. MessageBox(GetCurrentException())
  1318. )
  1319. )
  1320. )
  1321.  
  1322. addRollout importRollout TRIEditor
  1323. addRollout exportRollout TRIEditor rolledup:true
  1324. addRollout triEditorRollout TRIEditor rolledup:true
  1325. addRollout morphEditorRollout TRIEditor rolledup:true
  1326. addRollout morphTransferRollout TRIEditor rolledup:true
  1327. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement