Saturday 12 July 2014

Metasequoia Script – Import MilkShape3D *.ms3d

Here is a script that imports a model from a MilkShape3D *.ms3d file.

The script uses Python 3 modules, syntax and objects so can only be used as is with Metasequoia 4.

MS3D model imported into Metasequoia 4



There are two Python scripts.

The first script which must be named “ms3dreader.py” is the script that has the function to read the *.ms3d file and return the data.

“ms3dreader.py” is imported into the main script so must be copied to the same folder as the main script.



The script to open and run in Metasequoia’s Script Editor is listed below.

# Metasequoia 4 Python script
# ReadMS3D.py
'''
Imports a model from a MilkShape3D *.ms3d file
No scaling –> model may be small

'''

# import Python's os.path module so can extract filename
# and file extension from full path
import os.path

# import module that has function to read *.ms3d
# ms3dreader.py must be in same folder as this script
import ms3dreader

# global variables
doc = MQSystem.getDocument()

# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return


# main script function
def main():

 # create the OpenFile dialog as a child control of Metasequoia
 od = MQWidget.OpenFileDialog(MQWidget.getMainWindow())
 
 # add filter for MilkShape3D files
 od.addFilter("MilkShape3D files (*.ms3d)|*.ms3d")
 
 # good practise to always have All Files option
 od.addFilter("All Files (*.*)|*.*")
 
 # show the dialog
 if od.execute():
  # user selected a file and clicked ok
  
  # store path string in variable named f
  f = od.filename
  
  # extract file's extension using an os.path function
  path,ext = os.path.splitext(f)
  
  
  if ext.lower() in [".ms3d"]:
   # correct extension so extract filename and store in name
   path,name = os.path.split(f) # unused
   
   # print path to file
   mqprint(f)
   
   # read the file
   data = ms3dreader.readMS3D(f,True)
   if not data:
    return
   obj = MQSystem.newObject()
   for v in data["vertices"]:
    obj.addVertex(*v)
   # need to reverse winding of faces otherwise all inverted
   faces = [[t[0],t[2],t[1]] for t in data["faces"]]
   uvs = [[t[0],t[2],t[1]] for t in data["uvs"]]
   
   for t in faces:
    obj.addFace(t)
   for index in range(obj.numFace):
    if index < len(uvs)-1:
     uv = MQSystem.newCoordinate(uvs[index][0][0],uvs[index][0][1])
     obj.face[index].setCoord(0, uv)
     uv = MQSystem.newCoordinate(uvs[index][1][0],uvs[index][1][1])
     obj.face[index].setCoord(1, uv)
     uv = MQSystem.newCoordinate(uvs[index][2][0],uvs[index][2][1])
     obj.face[index].setCoord(2, uv)
    else:
     pass        
   
   # hide objects in document
   for ob in doc.object:
    ob.visible = 0
   idx = doc.addObject(obj)

   # make imported model current object
   doc.currentObjectIndex = idx       
   
  else:
   # user selected incorrect file type
   mqprint("Not an *.ms3d file")
   
 else:
  # user clicked "Cancel" 
  mqprint("No file selected")
 return
 

if __name__ == "__main__":
 # clear output window
 MQSystem.clearLog()
 
 # run main function
 main()
 
 # let user know script finished
 mqprint("Script finished")

 
 

This post discusses some of the basics of using the Script Editor in Metasequoia. [link]



No comments:

Post a Comment