2013年3月26日火曜日

Original Python Script : 階段

階段を作ることがあって、めんどくさいからスクリプト化


二本のエッジを選択して 実行

エラー処理しておりません



#CreateStaps
#階段状のポリゴンを作成

app = Application; log = app.LogMessage

nbStep = 50

#エッジを2本選択する

def getQuadPosition(oSub):
 quadPos = []
 oObj = oSub.Parent3DObject
 m = XSIMath.CreateMatrix4()
 oObj.Kinematics.Global.TransForm.GetMatrix4(m)
 oCol = oSub.ComponentCollection
 aEdge = oCol(0)
 bEdge = oCol(1)
 a1Pos = aEdge.Points(0).Position
 a1Pos.MulByMatrix4InPlace(m)
 a2Pos = aEdge.Points(1).Position
 a2Pos.MulByMatrix4InPlace(m)
 b1Pos = bEdge.Points(0).Position
 b1Pos.MulByMatrix4InPlace(m)
 b2Pos = bEdge.Points(1).Position
 b2Pos.MulByMatrix4InPlace(m)

 v1 = XSIMath.CreateVector3()
 v2 = XSIMath.CreateVector3()
 v1.Sub(a2Pos,a1Pos)
 v2.Sub(b2Pos,b1Pos)
 
 quadPos.append(a1Pos)
 quadPos.append(a2Pos)
 
 if v1.Dot(v2) > 0: 
  quadPos.append(b1Pos)
  quadPos.append(b2Pos)
  
 elif v1.Dot(v2) < 0:
  quadPos.append(b2Pos)
  quadPos.append(b1Pos)
  
 else :
  return -1
 
 return quadPos

def vLog(i,v):
 log(str( i ) + " : " + str(v.X) + ", " + str(v.Y) + ", " + str(v.Z))

def createIndexPosArray(nbStep,qp):
 aPos = []
 s0 = qp[0]
 s1 = qp[1]
 s2 = qp[2]
 s3 = qp[3]
 
 aH = s2.Y - s0.Y
 bH = s3.Y - s1.Y
 aS = aH * 1.0 / nbStep
 bS = bH * 1.0 / nbStep
 
 
 #p1 = XSIMath.CreateVector3()
 #p2 = XSIMath.CreateVector3()
 v1 = XSIMath.CreateVector3()
 v2 = XSIMath.CreateVector3()
 
 v1.Sub(s2,s0)
 v2.Sub(s3,s1)

 aPos.append(s0)
 aPos.append(s1)
 
 for i in range(nbStep):
 
  p1 = XSIMath.CreateVector3()
  p2 = XSIMath.CreateVector3()
  p3 = XSIMath.CreateVector3()
  p4 = XSIMath.CreateVector3()
  
  temp = ( i + 1.0) / nbStep
  #log( temp )
  p1.ScaleAdd( temp ,v1 , s0 )
  p2.ScaleAdd( temp ,v2 , s1 )
  
  p3.Set(p1.X,p1.Y - aS ,p1.Z)
  aPos.append(p3)
  p4.Set(p2.X,p2.Y - bS ,p2.Z)
  aPos.append(p4)
 
  aPos.append(p1)
  aPos.append(p2)
 
 return aPos


def createMesh(aPos):
 oRoot = app.ActiveSceneRoot
 Name = "Step"
 vertex = []
 for v in aPos:
  vertex.append(v.X)
  vertex.append(v.Y)
  vertex.append(v.Z)
 
 mesh = []
 for i in range(len(aPos) / 2 - 1):
  mesh.append(4)
  mesh.append(i * 2)
  mesh.append(i * 2 +1)
  mesh.append(i * 2 +3)
  mesh.append(i * 2 +2)
  
 #log("mesh : "  + str(mesh))
  
 oMesh = oRoot.addPolygonMesh(vertex,mesh,Name)
 
 return oMesh
 
 
def main ():
 log("----------------------------")
 oSel = app.Selection
 oSub = oSel(0).SubComponent
 
 qp = getQuadPosition(oSub)
 if qp == -1:
  return
 
  
 aPos = createIndexPosArray(nbStep,qp)

 #log("aPos")
 #for i,v in enumerate(aPos):
 # vLog(i,v)
 
 oMesh = createMesh(aPos)
 
 

main()

0 件のコメント:

コメントを投稿