import win32com.client from win32com.client import constants xsi = win32com.client.Dispatch("XSI.Application").Application xsiPrint = xsi.LogMessage class SubCurve: """ class to hold subcurve points in """ def __init__(self): self.points = [] def AddPoint(self, x, y, z, w = 1.0): self.points.append( x ) self.points.append( y ) self.points.append( z ) self.points.append( w ) def MakeUniformKnots(self): self.knots = [0.0]*(len(self.points)/4+2) knotVal = 1.0 for i in range(3,len(self.knots)): self.knots[i] = knotVal if i < len(self.knots)-3: knotVal += 1.0 def RotatePoints(self): rotMatrix = XSIMath.CreateMatrix3(1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0) # 90 degree rotation for i in range(0,len(self.points),4): vec = XSIMath.CreateVector3( self.points[i], self.points[i+1], self.points[i+2] ) vec.MulByMatrix3InPlace(rotMatrix) self.points[i] = vec.X self.points[i+1] = vec.Y self.points[i+2] = vec.Z class CurveList: """ basic curve list class """ def __init__(self): self.name = "ASECurve_1" self.subCurves = [] self.position = XSIMath.CreateVector3() def AddSubCurve(self, curve): self.subCurves.append( curve ) def RotatePosition(self): rotMatrix = XSIMath.CreateMatrix3(1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0) # 90 degree rotation self.position.MulByMatrix3InPlace(rotMatrix) # no undos, takes up a lot of memory, also keep logging quiet undoPref = xsi.GetValue("preferences.General.undo") logMessagePref = xsi.GetValue("preferences.scripting.cmdlog") xsi.SetValue("preferences.General.undo", 0, "") xsi.SetValue("preferences.scripting.cmdlog", False, "") # all curves objects to be made ASECurves = [] # get file from user input filebrowser = XSIUIToolkit.FileBrowser filebrowser.DialogTitle = "Select an .ase file..." filebrowser.InitialDirectory = xsi.ActiveProject2.Path filebrowser.Filter = "ASE (*.ASE)|*.ASE|All Files (*.*)|*.*||" filebrowser.ShowOpen() if filebrowser.FilePathName: file = open( filebrowser.FilePathName, "r" ) progress = XSIUIToolkit.ProgressBar # parse the file once to get the line count progress.Maximum = len(file.readlines()) file.close() progress.Caption = "Parsing %s..." % (filebrowser.FileName) # actually do the work this time file = open( filebrowser.FilePathName, "r" ) curveName = "" currentCurve = None currentSubCurve = None progress.Visible = True for line in file: progress.Increment(1) if "*SHAPEOBJECT" in line: currentCurve = CurveList() if "*NODE_NAME" in line: curveName = line.replace("*NODE_NAME ","") curveName = curveName.strip() curveName = curveName.strip('"') curveName = curveName.replace("-","_") currentCurve.name = curveName continue if "*TM_POS" in line: position = line.split() currentCurve.position.X = float(position[1]) currentCurve.position.Y = float(position[2]) currentCurve.position.Z = float(position[3]) if "*SHAPE_LINE" in line: if "COUNT" in line: continue currentSubCurve = SubCurve() continue if "*SHAPE_VERTEX_KNOT" in line or "*SHAPE_VERTEX_INTERP" in line: currentVert = line.split() currentSubCurve.AddPoint( float(currentVert[2]), float(currentVert[3]), float(currentVert[4]) ) continue if "}" in line: if currentCurve is not None and currentSubCurve is not None: currentCurve.AddSubCurve( currentSubCurve ) currentSubCurve = None continue elif currentCurve is not None and len(currentCurve.subCurves) > 0: ASECurves.append( currentCurve ) currentCurve = None continue if progress.CancelPressed == True: break file.close() if progress.CancelPressed == False: # no create the curves in softimage progress.Value = 0 progress.Maximum = len(ASECurves) for i in range(len(ASECurves)): curve = ASECurves[i] progress.Caption = "Creating curve %s..." % (curve.name) hairGroup = xsi.ActiveSceneRoot.AddNull( curve.name ) curve.RotatePosition() xform = hairGroup.Kinematics.Local.Transform xform.SetTranslation( curve.position ) hairGroup.Kinematics.Local.Transform = xform siCurve = hairGroup.AddNurbsCurveList2() siCurve.Name = curve.name for j in range(len(curve.subCurves)): subCurve = curve.subCurves[j] progress.Maximum = len(curve.subCurves) subCurve.MakeUniformKnots() subCurve.RotatePoints() siCurve.ActivePrimitive.Geometry.AddCurve( subCurve.points, subCurve.knots, False, 3, 0, constants.siSINurbs ) progress.Value = j progress.Value = i progress.Visible = False xsi.SetValue("preferences.General.undo", undoPref, "") xsi.SetValue("preferences.scripting.cmdlog", logMessagePref, "")