home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5888 / fmt_noepyxml_0.01.7z / fmt_noepyxml.py
Encoding:
Python Source  |  2012-10-07  |  8.0 KB  |  205 lines

  1. #Noesis Python model import+export test module, imports/exports some data from/to a made-up format
  2. #version 0.01
  3.  
  4. from inc_noesis import *
  5. import xml.dom.minidom as xd
  6.  
  7. import noesis
  8.  
  9. #rapi methods should only be used during handler callbacks
  10. import rapi
  11.  
  12. #registerNoesisTypes is called by Noesis to allow the script to register formats.
  13. #Do not implement this function in script files unless you want them to be dedicated format modules!
  14. def registerNoesisTypes():
  15.     handle = noesis.register("Noesis Python XML Test Model", ".noepyxml")
  16.     noesis.setHandlerTypeCheck(handle, noepyXmlCheckType)
  17.     noesis.setHandlerLoadModel(handle, noepyXmlLoadModel) #see also noepyLoadModelRPG
  18.     noesis.setHandlerWriteModel(handle, noepyXmlWriteModel)
  19.     noesis.setHandlerWriteAnim(handle, noepyXmlWriteAnim)
  20.  
  21.     #noesis.logPopup()
  22.     #print("The log can be useful for catching debug prints from preview loads.\nBut don't leave it on when you release your script, or it will probably annoy people.")
  23.     return 1
  24.  
  25. #check if it's this type based on the data
  26. def noepyXmlCheckType(data):
  27.     if len(data) < 8:
  28.         return 0
  29.     bs = NoeBitStream(data)
  30.  
  31.     return 1
  32.  
  33. #load the model
  34. def noepyXmlLoadModel(data, mdlList):
  35.     ctx = rapi.rpgCreateContext()
  36.     bs = NoeBitStream(data)
  37.     rapi.rpgClearBufferBinds()    
  38.     return 1
  39.  
  40.  
  41. #write it
  42. def noepyXmlWriteModel(mdl, bs):
  43.     anims = rapi.getDeferredAnims()
  44.  
  45.     #Create the root XML node ans set attributes
  46.     doc = xd.Document()
  47.     root_node = doc.createElement("root")
  48.     doc.appendChild(root_node)
  49.     root_node.setAttribute("NOEPY_HEADER", str(0x1337455))
  50.     root_node.setAttribute("NOEPY_VERSION", str(0x7178173))
  51.     #Write the mesh node
  52.     mesh_node = doc.createElement("meshes")
  53.     root_node.appendChild(mesh_node)
  54.     mesh_node.setAttribute("count", str(len(mdl.meshes)))
  55.     for mesh in mdl.meshes:
  56.         object_node = doc.createElement(mesh.name)
  57.         mesh_node.appendChild(object_node)
  58.         object_node.setAttribute("material_name", mesh.matName)
  59.         #create sub object nodes
  60.         #indices
  61.         indices_node = doc.createElement("indices")
  62.         object_node.appendChild(indices_node)
  63.         indices_node.setAttribute("count", str(len(mesh.indices)))
  64.         for idx in mesh.indices:
  65.             sub_indices_node = doc.createTextNode(str(idx))
  66.             indices_node.appendChild(sub_indices_node)
  67.         #verts
  68.         #positions
  69.         positions_node = doc.createElement("positions")
  70.         object_node.appendChild(positions_node)
  71.         positions_node.setAttribute("count", str(len(mesh.positions)))
  72.         for vcmp in mesh.positions:
  73.             sub_positions_node = doc.createTextNode(str(vcmp))
  74.             positions_node.appendChild(sub_positions_node)
  75.         #normals
  76.         normals_node = doc.createElement("normals")
  77.         object_node.appendChild(normals_node)
  78.         normals_node.setAttribute("count", str(len(mesh.normals)))
  79.         for vcmp in mesh.normals:
  80.             sub_normals_node = doc.createTextNode(str(vcmp))
  81.             normals_node.appendChild(sub_normals_node)
  82.         #uvs
  83.         uvs_node = doc.createElement("uvs")
  84.         object_node.appendChild(uvs_node)
  85.         uvs_node.setAttribute("count", str(len(mesh.uvs)))
  86.         for vcmp in mesh.uvs:
  87.             sub_uvs_node = doc.createTextNode(str(vcmp))
  88.             uvs_node.appendChild(sub_uvs_node)
  89.         #tangents
  90.         tangents_node = doc.createElement("tangents")
  91.         object_node.appendChild(tangents_node)
  92.         tangents_node.setAttribute("count", str(len(mesh.tangents)))
  93.         for vcmp in mesh.tangents:
  94.             sub_tangents_node = doc.createTextNode(str(vcmp))
  95.             tangents_node.appendChild(sub_tangents_node)
  96.         #colors
  97.         colors_node = doc.createElement("colors")
  98.         object_node.appendChild(colors_node)
  99.         colors_node.setAttribute("count", str(len(mesh.colors)))
  100.         for vcmp in mesh.colors:
  101.             sub_colors_node = doc.createTextNode(str(vcmp))
  102.             colors_node.appendChild(sub_colors_node)
  103.         #weights
  104.         weights_node = doc.createElement("weights")
  105.         object_node.appendChild(weights_node)
  106.         weights_node.setAttribute("count", str(len(mesh.weights)))
  107.         for vcmp in mesh.weights:
  108.             sub_weights_node = doc.createElement("bw")
  109.             sub_weights_node.setAttribute("count", str(vcmp.numWeights()))
  110.             weights_node.appendChild(sub_weights_node)
  111.             bIndices_node = doc.createElement("bIndices")
  112.             sub_weights_node.appendChild(bIndices_node)
  113.             for wval in vcmp.indices:
  114.                 sub_bIndices_node = doc.createTextNode(str(wval))
  115.                 bIndices_node.appendChild(sub_bIndices_node)
  116.             bWeights_node = doc.createElement("bWeights")
  117.             sub_weights_node.appendChild(bWeights_node)
  118.             for wval in vcmp.weights:
  119.                 sub_bWeights_node = doc.createTextNode(str(wval))
  120.                 bWeights_node.appendChild(sub_bWeights_node)
  121.         #morphList
  122.         morphList_node = doc.createElement("morphList")
  123.         object_node.appendChild(morphList_node)
  124.         morphList_node.setAttribute("count", str(len(mesh.morphList)))
  125.         for mf in mesh.morphList:
  126.             morph_node = doc.createElement("morph")
  127.             morphList_node.appendChild(morph_node)
  128.             morph_pos_node = doc.createElement("morph_pos")
  129.             morph_node.appendChild(morph_pos_node)
  130.             morph_pos_node.setAttribute("count", str(len(mf.positions)))
  131.             for vec in mf.positions:
  132.                 sub_morph_pos_node = doc.createTextNode(str(vec))
  133.                 morph_pos_node.appendChild(sub_morph_pos_node)                
  134.             morph_norm_node = doc.createElement("morph_norm")
  135.             morph_node.appendChild(morph_norm_node)
  136.             morph_norm_node.setAttribute("count", str(len(mf.normals)))
  137.             for vec in mf.normals:
  138.                 sub_morph_norm_node = doc.createTextNode(str(vec))
  139.                 morph_norm_node.appendChild(sub_morph_norm_node)
  140.  
  141.     #Write the textures node
  142.     texture_node = doc.createElement("textures")
  143.     root_node.appendChild(texture_node)
  144.     #texture_node.setAttribute("count", str(len(mesh.modelMats.texList)))
  145.  
  146.     #Write the materials node
  147.     material_node = doc.createElement("materials")
  148.     root_node.appendChild(material_node)
  149.     material_node.setAttribute("count", str(len(mdl.modelMats.matList)))
  150.     for mat in mdl.modelMats.matList:
  151.         sub_material_node = doc.createElement(str(mat.name))
  152.         material_node.appendChild(sub_material_node)
  153.         sub_material_node.setAttribute("texName", mat.texName)
  154.  
  155.     #Write the bone node
  156.     bone_node = doc.createElement("bones")
  157.     root_node.appendChild(bone_node)
  158.     bone_node.setAttribute("count", str(len(mdl.bones)))
  159.     for bone in mdl.bones:
  160.         object_node = doc.createElement(bone.name)
  161.         bone_node.appendChild(object_node)
  162.         object_node.setAttribute("index", str(bone.index))
  163.         object_node.setAttribute("parentName", bone.parentName)
  164.         object_node.setAttribute("parentIndex", str(bone.parentIndex))
  165.         object_node.setAttribute("matrix", str(bone.getMatrix()))
  166.  
  167.     #Write the anims node
  168.     anims_node = doc.createElement("anims")
  169.     root_node.appendChild(anims_node)
  170.     anims_node.setAttribute("count", str(len(anims)))
  171.     for anim in anims:
  172.         anim_node = doc.createElement(anim.name)
  173.         anims_node.appendChild(anim_node)
  174.         anim_bone_node = doc.createElement("anim_bones")
  175.         anim_node.appendChild(anim_bone_node)
  176.         anim_bone_node.setAttribute("count", str(len(anim.bones)))
  177.         for bone in anim.bones:
  178.             bone_node = doc.createElement(bone.name)
  179.             anim_bone_node.appendChild(bone_node)
  180.             bone_node.setAttribute("index", str(bone.index))
  181.             bone_node.setAttribute("parentName", bone.parentName)
  182.             bone_node.setAttribute("parentIndex", str(bone.parentIndex))
  183.             bone_node.setAttribute("matrix", str(bone.getMatrix()))
  184.         anim_node.setAttribute("numFrames", str(anim.numFrames))
  185.         anim_node.setAttribute("frameRate", str(anim.frameRate))
  186.         anim_mats_node = doc.createElement("anim_mats")
  187.         anim_node.appendChild(anim_mats_node)
  188.         anim_mats_node.setAttribute("count", str(len(anim.frameMats)))
  189.         for mat in anim.frameMats:
  190.             sub_mat_node = doc.createTextNode(str(mat))
  191.             anim_mats_node.appendChild(sub_mat_node)
  192.  
  193.     bs.writeBytes(doc.toprettyxml(encoding="ascii"))
  194.     return 1
  195.         
  196.  
  197. #when you want animation data to be written out with a model format, you should make a handler like this that catches it and defers it
  198. def noepyXmlWriteAnim(anims, bs):
  199.     #it's good practice for an animation-deferring handler to inform the user that the format only supports joint model-anim export
  200.     if rapi.isGeometryTarget() == 0:
  201.         print("WARNING: Stand-alone animations cannot be written to the .noepy format.")
  202.         return 0
  203.  
  204.     rapi.setDeferredAnims(anims)
  205.     return 0