Skip to content Flash-fx website
flash-fx logo image1
flash-fx logo image2
Simple solutions to complex problems. Historical swordsmanship Great online resource and forums relating to Maya and 3D.
Home << 3D << Tools/Scripts << Batch 3ds file import and save as maya files (Python)

Batch 3ds file import and save as maya files (Python)

Download script (you will need to right click and 'save as')


# import python commands
import maya.cmds as mc
'''
File       : batchModify3DS
Purpose    : Batch script to import 3ds files from a directory, rotate and quadrangulate and save as a maya file
Version    : 1.1 (23/01/2008)
           : 1.0 (22/01/2008)
Author     : Simon Mann 
Date       : 22/01/2008

Process    :
    1) Get all files in specified directory
    2) Go through each one and:
        a) create a new scene
        b) import the model into the scene
        c) run the commands:
            1) open .3ds files
            2) rotate main object to maya's display
            3) freeze the transformations
            4) delete the history
            5) get all sub transforms
            6) loop through and quadrangulate them all
        d) save the scene

Requirements: Maya, Python (Only tested in Maya 2008)
Install     : Copy to your Maya scripts folder
              Copy the following code to the script editor (python mode)
                  bModObj = bMod.batchModify3DS()
              Highlight it and middle mouse drag it to your shelf
              In Window -> Settings/preferences -> Shelf editor, select the new shelf item and change the icon box to say 'bMod'
Usage       : Simply click the shelf button which will run the script
              Can also be executed from the script editor by typing the following python command:
                  bModObj = bMod.batchModify3DS()
              Will need the paths in the file changing to generic locations that you will use to import/export files to/from
              They are about 12 lines down and are called importDir and saveDir.
Updates     : 1.1 - Added removeCameras() function to delete any imported cameras
Bugs        : Once the relevant files have been converted Maya appears to lock the .3ds files so that they cannot be deleted
              Think it has something to do with me accidentally referencing the .3ds file instead of a straight import
              Until it is fixed you will have to restart Maya each time you want to convert a new batch of files :(
'''

class batchModify3DS():

    def __init__(self):
        # Change these paths to wherever you want to import and export the files *make sure the directories exist!*
        importDir = 'scenes/3ds_file_dir/'
        saveDir = 'scenes/3ds_converted/'

        # No need to change anything below here...
        fileExt = '*.3ds'
        fileType = '3DS'

        # get current workspace directory
        workDir = mc.workspace(query=True, rootDirectory=True, fullName=True)
        # set up the full paths
        startDir = workDir + importDir
        endDir = workDir + saveDir

        print 'importing from ' + startDir
        print 'saving maya files to ' + endDir

        # get a list of all files in the import folder
        fileList = mc.getFileList( folder=startDir, filespec=fileExt )
        # cycle through each one
        for i in fileList:
            print 'attempting to import ' + i
            # import the current file
            # mc.file(startDir+i, i=True, type=fileType, namespace=i, renameAll=True, preserveReferences=True, loadReferenceDepth='all')
            mc.file(startDir+i, i=True, type=fileType, renameAll=True, preserveReferences=True, loadReferenceDepth='none')
            print i + ' imported'
            # first see if there are any cameras that were imported and need removing
            self.removeCameras()
            # function to make relevant changes
            self.modify3DS()
            print i + ' modified'
            # rename the file to match the original
            mc.file(startDir+i, rename=endDir+i[:-4]+'.mb')
            print 'file renamed from ' + startDir+i+ ' to ' + endDir+i[:4] + '.mb'
            # save the file off
            mc.file(save=True, force=True, type='mayaBinary')
            print i + ' saved as '+endDir+i[:4]+'.mb'
            # create a new blank file for the next one
            mc.file(newFile=True, force=True)

    # hunts through scene, finds any imported cameras and deletes them *very hacky + probably not too trustworthy*
    def removeCameras(self):
        # select everything
        myCamSel = mc.select(ado=True, visible=True)
        # get the root shape objects
        myRootShapes = mc.ls(selection=True, dag=True, type='shape') #, type='transform')
        if myRootShapes:
            #cycle through
            for i in myRootShapes:
                # get their keyable attributes
                atList = mc.listAttr(i, keyable=True)
                # check first keyable attribute
                if atList[0] == 'horizontalFilmAperture':
                    print 'camera named '+i+ ' found'
                    # if its a camera attribute then get the parent transform node and delete the camera
                    rootName = mc.pickWalk(i, direction='up')
                    mc.delete(rootName)

    # selects the object and rotates it, then goes through each dub object and converts to polys
    def modify3DS(self):
        # select everything
        mySel = mc.select(ado=True)
        # get the root transform object
        myRoot = mc.ls(selection=True, dag=False, type='transform')
        # rotate it -90 in the x axis
        mc.rotate(-90, 0, 0, myRoot, relative=True, objectSpace=True)
        # freeze transformations
        mc.makeIdentity(myRoot, apply=True, rotate=True)
        # delete history
        mc.delete(myRoot, constructionHistory=True)
        # get all sub transforms
        myVar = mc.ls(selection=True, dag=True, type='transform')
        # loop through all sub transforms and convert them to quad
        for i in myVar:
            mc.polyQuad(i, angle=30, keepGroupBorder=True, keepHardEdges=True, worldSpace=True, constructionHistory=False)

Copyright

© 2008
Simon Mann
http://www.flash-fx.net