面張り

どうも思っているのと違ったようなので確認してみた。
テキストエディタで次のコードを実行してから3D viewで法線を確認してみると、

import Blender
scene=Blender.Scene.GetCurrent()

def createRectangle():
	mesh = Blender.Mesh.New("MeshName")
	mesh.verts.extend([
	 [-1.0, -1.0, 0],
	 [-1.0,  1.0, 0],
	 [ 1.0,  1.0, 0],
	 [ 1.0, -1.0, 0],
	])
	mesh.faces.extend([
	 [0, 3, 2, 1],
	])
	return scene.objects.new(mesh, "NewMesh")

def printMesh(obj):
	mesh = obj.getData(mesh=True)
	for f in mesh.faces:
		print f

rectObj=createRectangle()
printMesh(rectObj)
scene.update(0)

上(z軸+)が表だった。ということは面を[0, 3, 2, 1]で指定したので時計周り(ClockWise)が表だ。
メタセコ(CounterClockWise)と逆まわり。


さらに同じ頂点を使って表裏両面を張ると、

	mesh.faces.extend([
         [0, 1, 2, 3],
	 [0, 3, 2, 1],
	])

同じ頂点を使う面は後からきたもので上書きされる。
[MFace (0 3 2 1) 0]


2つの面の表と裏を同じ頂点で張るには追加の引数が要る。

	mesh.faces.extend([
         [0, 1, 2, 3],
	 [0, 3, 2, 1],
	],
        ignoreDups=True)

メッシュをインポートした時に微妙におかしい原因になっていた様子。


あと
http://www.blender.org/documentation/249PythonDoc/Mesh.MFaceSeq-class.html
のextendメソッドの最後にある
Warning: Faces using the first vertex at the 3rd or 4th location in the face's vertex list will have their order rotated so that the zero index on in the first or second location in the face. When creating face data with UVs or vertex colors, you may need to work around this, either by checking for zero indices yourself or by adding a dummy first vertex to the mesh that can be removed when your script has finished.
というのが罠らしい。
意訳:頂点のインデックス指定の最後が0の時は頂点の指定順を回すよ
やってみる。

	mesh.faces.extend([
         [1, 2, 3, 0],
        ])

結果は、
[MFace (3 0 1 2) 0]
確かに回転されとる。
嫌な仕様だなー。


ちなみに三角形だと

	mesh.faces.extend([
         [1, 2, 0],
        ])

から
[MFace (2 0 1) 0]
となった。
三角形と四角形のインデックスを同じ配列に突っ込んで0値で判定するとかしていそうだな。


以上、Blenderpythonによる面張りメモでした。