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 “” is the script that has the function to read the *.ms3d file and return the data.

“” 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
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
# must be in same folder as this script
import ms3dreader

# global variables
doc = MQSystem.getDocument()

# function definitions
def mqprint(message):

# 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
   # read the file
   data = ms3dreader.readMS3D(f,True)
   if not data:
   obj = MQSystem.newObject()
   for v in data["vertices"]:
   # 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:
   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)
   # hide objects in document
   for ob in doc.object:
    ob.visible = 0
   idx = doc.addObject(obj)

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

if __name__ == "__main__":
 # clear output window
 # run main function
 # 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