demoMeshAndCPV.py

demoMeshAndCPV.py
1 '''
2  Demonstrates how to create a mesh from scratch and to set color per vertex data.
3 '''
4 import MaxPlus
5 
6 def makePyramidMesh(mesh, side = 20.0):
7  mesh.SetNumVerts(4)
8  mesh.SetNumFaces(4)
9  halfside = side / 2.0
10  mesh.SetVert(0, MaxPlus.Point3(0.0, 0.0, side))
11  mesh.SetVert(1, MaxPlus.Point3(-halfside, -halfside, 0.0))
12  mesh.SetVert(2, MaxPlus.Point3(-halfside, halfside, 0.0))
13  mesh.SetVert(3, MaxPlus.Point3(halfside, 0.0, 0.0))
14  mesh.GetFace(0).SetVerts(0, 1, 2)
15  mesh.GetFace(0).SetEdgeVisFlags(1,1,0)
16  mesh.GetFace(1).SetVerts(0, 2, 3)
17  mesh.GetFace(1).SetEdgeVisFlags(1,1,0)
18  mesh.GetFace(2).SetVerts(0, 3, 1)
19  mesh.GetFace(2).SetEdgeVisFlags(1,1,0)
20  mesh.GetFace(3).SetVerts(1, 2, 3)
21  mesh.GetFace(3).SetEdgeVisFlags(1,1,0)
22  mesh.InvalidateGeomCache()
23  mesh.InvalidateTopologyCache()
24 
25 def outputChannel(mc, name):
26  MaxPlus.Core.WriteLine("Channel " + name)
27  if not mc.Enabled:
28  MaxPlus.Core.WriteLine("Not enabled");
29  return
30 
31  MaxPlus.Core.WriteLine("Number of texture vertices %d" % mc.NumTextureVertices)
32  for tv in mc.TextureVertices:
33  MaxPlus.Core.WriteLine("Texture vertex %f, %f, %f" % (tv.X, tv.Y, tv.Z))
34 
35  MaxPlus.Core.WriteLine("Number of faces %d" % mc.NumFaces)
36  for tf in mc.TextureFaces:
37  MaxPlus.Core.WriteLine("Texture vertex indices %d, %d, %d" % (tf.A, tf.B, tf.C));
38 
39 def outputChannels(mesh):
40  outputChannel(mesh.DefaultMap, "default")
41  outputChannel(mesh.AlphaMap, "alpha")
42  outputChannel(mesh.ColorPerVertexMap, "color per vertex")
43  outputChannel(mesh.ShadingMap, "shading")
44 
45 def main():
46  geom = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.TriMeshGeometry)
47  tri = MaxPlus.TriObject._CastFrom(geom)
48  mesh = tri.GetMesh()
49  makePyramidMesh(mesh)
50  node = MaxPlus.Factory.CreateNode(tri)
51  outputChannels(mesh)
52  MaxPlus.Core.WriteLine("Updating the color per vertex channel")
53  mesh.ColorPerVertexMap.SetNumTextureVertices(2)
54  mesh.ColorPerVertexMap.SetTextureVertex(0, MaxPlus.Point3(1, 0, 0))
55  mesh.ColorPerVertexMap.SetTextureVertex(1, MaxPlus.Point3(0, 0, 1))
56  mesh.ColorPerVertexMap.SetTextureFace(0, 0, 0, 1)
57  mesh.ColorPerVertexMap.SetTextureFace(1, 0, 1, 1)
58  mesh.ColorPerVertexMap.SetTextureFace(2, 1, 1, 1)
59  mesh.ColorPerVertexMap.SetTextureFace(3, 0, 0, 0)
60  outputChannels(mesh)
61  node.VertexColorMode = True
62 
63 if __name__ == "__main__":
64  main()