Saturday, 21 June 2014

Metasequoia Script – Display an OpenFile Dialog

This script shows how to display an OpenFile dialog in Metasequoia 4.

Metasequoia 3 doesn’t have the MQWidget classes so this script is only for Metasequoia 4.

You use an OpenFile dialog to get a filename (actually the full path to a file) for your script.

Once your script has a filename you could use standard Python code to read the file.

You could, for example, write a script to open a 3D file format that Metasequoia does not support.

My tabs have been converted to one space indentation in the code below.

# Metasequoia 4 Python script
# Title
'''

description

'''

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

# global variables
doc = MQSystem.getDocument()


# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return
 
def readFile(name):
 # not implemented!
 pass


# main script function
def main():

 # create the OpenFile dialog as a child control of Metasequoia
 od = MQWidget.OpenFileDialog(MQWidget.getMainWindow())
 
 # add filters to the dialog
 od.addFilter("BVH Files (*.bvh)|*.bvh")
 
 # apparently good practice 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)
  
  # Since we allow All Files, must make sure we guard 
  # against wrong file type being selected
  
  if ext.lower() in [".bvh"]:
   # correct extension so extract filename and store in name
   path,name = os.path.split(f)
   
   # print path to file
   mqprint(f)
   
   # read the file
   readFile(f)
   
  else:
   # user select incorrect file type
   mqprint("Not a *.bvh 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")

 
 

Metasequoia script OpenFile dialog


See this post [link] for the basics of using Metasequoia’s Script Editor.

No comments:

Post a Comment