#Shawn, here is an updated version of the last script. It has two switch "processing" functions: #ProcessSwitchMasks - this function (formerly ProcessSwitch in previous script) writes out a new db per mask of the switch #ProcessSwitchChildren - this function (new) writes out a new db per child of the switch #I left both functions in the script but changed the Walk Function (PreCallback) to call the latter. If you ever wanted to change it, you could make it call the former. # temp files for copying palettes between existing and new dbs created # change these for your folder structure COLORPALETTE = "e:/_temp/switch/colorpalette.color" TEXTUREPLETTE = "e:/_temp/switch/texturepalette.txt" MATERIALPALETTE = "e:/_temp/switch/materialpalette.materials" # this is where you want new files to be saved. {0} is # replaced by 1,2,3,... etc for each switch mask set saved # change this (but leave {0} somewhere in pattern) #NEWFILEROOT = "e:/_temp/switch/switchmask_{0}.flt" def CopyColorPalette(dstDb, srcDb): # copy the color palette from srcDb to dstDb mgWriteColorPalette(srcDb, COLORPALETTE) mgReadColorPalette(dstDb, COLORPALETTE) def CopyTexturePalette(dstDb, srcDb): # copy the texture palette from srcDb to dstDb mgWriteTexturePalette(srcDb, TEXTUREPLETTE) mgReadTexturePalette(dstDb, TEXTUREPLETTE) def CopyMaterialPalette(dstDb, srcDb): # copy the material palette from srcDb to dstDb mgWriteMaterialFile(srcDb, MATERIALPALETTE) mgReadMaterialFile(dstDb, MATERIALPALETTE) def CopyPalettes(dstDb, srcDb): # copy color/texture/material palettes from scrDb to dstDb # if you need other palettes there are more read/write palette # functions in the API (light point, light source, texture mapping, etc) CopyColorPalette(dstDb, srcDb) CopyTexturePalette(dstDb, srcDb) CopyMaterialPalette(dstDb, srcDb) # this function saves a new db file containing the nodes in the recList # presumably these are the nodes that are ON in a switch mask def SaveNode(fileName, srcDb, recList): # overwrite file if it already exists mgSetNewOverwriteFlag(MG_TRUE) # make the new db print 'saving new db',fileName newDb = mgNewDb(fileName) if (newDb): # new dbs always have db-g1-g2 # we'll attach new nodes under g2 which is child of g1 which is child of db g2 = mgGetChild(mgGetChild(newDb)) for rec in recList: # for each node in the recList... # make a copy in the newDb newRec = mgDuplicateToDb(rec, newDb) # attach it to g2 in newDb mgAttach(g2, newRec) # copy color/texture/material palettes from original db to newDb CopyPalettes(newDb, srcDb) # write and close mgWriteDb(newDb) mgCloseDb(newDb) print fileName,"created and saved" # this function is called when the walk function finds a switch node in # the hierarchy and you want to process the "masks" of the switch def ProcessSwitchMasks(switchNode): # get the switch node name name = mgGetName(switchNode) # set switchname var switchname = name # get the number of masks defined for the switch numMasks = mgGetSwitchMaskCount(switchNode) on_off = { 0 : "on", 1 : "off" } # get the number of children for the switch node # each switch mask will have this many bits. # each bit tells you whether or not "that" child is ON or OFF numChildren = mgCountChild(switchNode) print "switch name:",name,"numMasks:",numMasks,"numChildren:",numChildren fileNum = 1 # loop through the masks defined for the switch for maskNum in range(0, numMasks): print "mask:",maskNum # build a list of nodes that are ON for this mask recList = list() # loop through the "bits" of the mask - again there will be one # bit per child. that bit will tell you if that child is ON/OFF for childNum in range(0, numChildren): b,on = mgGetSwitchBit (switchNode, maskNum, childNum) print " child",childNum,"is",on_off[on] child = mgGetChildNth(switchNode, childNum+1) # first child is 1, not 0 !! # add this child to the list if it is ON in this mask if (on): recList.append(child) if (len(recList) > 0): # if there are any children ON in this mask, save them to a new file # make the new file name, it is the root file name with {0} replaced by 1,2,3, etc fileName = NEWFILEROOT.format(fileNum) fileNum = fileNum + 1 # ready for next new file name db = mgRec2Db(switchNode) # save the nodes in this list (recList) to this fileName SaveNode(fileName, db, recList) # this function is called when the walk function finds a switch node in # the hierarchy and you want to process the children of the switch def ProcessSwitchChildren(switchNode): # get the switch node name name = mgGetName(switchNode) switchNodeName = mgGetName(switchNode) numChildren = mgCountChild(switchNode) #print "switch name:",switchNodeName,"numChildren:",numChildren my_FileName = base_file_name + '_opt_{0}' + file_ext #print 'File name:',my_FileName #print '#### xxxxxxxxxxxx ####' fileNum = 1 # loop through the children under the switch for childNum in range(0, numChildren): # build a list of nodes (will just contain one node - the current child) recList = list() child = mgGetChildNth(switchNode, childNum+1) # first child is 1, not 0 !! recList.append(child) if (len(recList) > 0): # if there are any children ON in this mask, save them to a new file # make the new file name, it is the root file name with {0} replaced by 1,2,3, etc #fileName = NEWFILEROOT.format(fileNum) fileName = my_FileName.format(fileNum) fileNum = fileNum + 1 # ready for next new file name db = mgRec2Db(switchNode) # save the nodes in this list (recList) to this fileName SaveNode(fileName, db, recList) def PreCallback (db, parent, rec, i): code = mgGetCode(rec) if (code == fltSwitch): # this node is a switch node, process it ProcessSwitchChildren(rec) return MG_TRUE # walk the database, looking for switch nodes db = mgGetCurrentDb () path = mgRec2Filename(db) split = path.split("/") index = len(split)-1 char_count_fname = len(split[-1]) cur_path = path[0:-char_count_fname] token = str(split[index]) fileNameList = token.split('.') base_file_name = fileNameList[0] file_ext = '.' + fileNameList[1] print '################ file name ##############' print 'path:',cur_path print 'file name:',base_file_name # switchname var doesn't exist yet #FILEROOT = curPath + str(base_file_name) + file_ext FILEROOT = cur_path + str(base_file_name) #Root is: E:/Nads/ProjectData/Unity/ProjectData/Tiles/res/2ln_res_4way01/switch/s_s_y_r1 print 'File root is: ', FILEROOT print 'file name is:', base_file_name + file_ext print '##########' NEWFILEROOT = "e:/_temp/switch/switchmask_{0}.flt" mgWalk (db, PreCallback, None, None, 0)