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