home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 October - Disc 2 / PCNET_CD_2006_10_2.iso / apps / k3d-all-in-one-setup-0.5.14.0.exe / k3d-setup-0.5.14.0.exe / share / scripts / make_human.py < prev    next >
Encoding:
Python Source  |  2006-03-06  |  2.8 KB  |  120 lines

  1. #python
  2.  
  3. import k3d
  4. import locale
  5.  
  6. # Make sure the script works with all locales
  7. # (especially when parsing floating point values from text)
  8. locale.setlocale(locale.LC_ALL, '')
  9.  
  10. # Experimental
  11. # Loads Make Human base mesh
  12.  
  13. def LoadBasePoints(BaseMeshFile):
  14.  
  15.     point_list = []
  16.  
  17.     file = open(BaseMeshFile)
  18.     vertexData = file.readline()
  19.     while vertexData:
  20.         coords = vertexData.split(',')
  21.         vx, vy, vz = float(coords[0]), float(coords[1]), float(coords[2])
  22.  
  23.         p = vx, vy, vz
  24.         point_list.append(p)
  25.  
  26.         vertexData = file.readline()
  27.  
  28.     file.close()
  29.  
  30.     return point_list
  31.  
  32.  
  33. def CreateBaseMesh(Document, BaseVertices, BaseFacesFile):
  34.     
  35.     Document.start_change_set()
  36.     try:
  37.         material = Document.new_node("RenderManMaterial")
  38.         material.name = "MakeHuman Material"
  39.  
  40.         frozen_mesh = Document.new_node("FrozenMesh");
  41.         frozen_mesh.name = "MakeHuman";
  42.  
  43.         mesh = frozen_mesh.new_mesh();
  44.  
  45.         # Create points
  46.         for i in range(len(BaseVertices)):
  47.             mesh.new_point(BaseVertices[i])
  48.         points = mesh.points
  49.  
  50.         # Create faces
  51.         polyhedron = mesh.new_polyhedron()
  52.  
  53.         file = open(BaseFacesFile)
  54.         faceData = file.readline()
  55.         while faceData:
  56.             face_indices = []
  57.             for idx in faceData.split(','):
  58.                 face_indices.append(long(idx))
  59.  
  60.             edges = []
  61.             for idx in face_indices:
  62.                 edges.append(polyhedron.new_edge(points[idx]))
  63.  
  64.             for i in range(len(edges)):
  65.                 edges[i].face_clockwise = edges[(i+1) % len(edges)]
  66.  
  67.             face = polyhedron.new_face(edges[0])
  68.             face.material = material
  69.     
  70.             faceData = file.readline()
  71.     
  72.         file.close()
  73.  
  74.         mesh_instance = Document.new_node("MeshInstance")
  75.         mesh_instance.name = "MakeHuman Instance"
  76.         Document.set_dependency(mesh_instance.get_property("input_mesh"), frozen_mesh.get_property("output_mesh"))
  77.  
  78.         Document.finish_change_set("Load MakeHuman Mesh")
  79.  
  80.     except:
  81.         Document.cancel_change_set()
  82.         raise
  83.     
  84.     return mesh
  85.  
  86.  
  87. def Morph(BaseMesh, BaseVertices, MorphPointsFile, MorphValue):
  88.  
  89.     file = open(MorphPointsFile)
  90.     morphData = file.readline()
  91.     while morphData:
  92.         if morphData.find("#") == -1 and morphData.find(",") != -1:
  93.             vals = morphData.split(',')
  94.             index, vx, vy, vz = long(vals[0]), float(vals[1]), float(vals[2]), float(vals[3])
  95.  
  96.             old_v0, old_v1, old_v2 = BaseMesh.get_point_position(index)
  97.  
  98.             v0 = vx
  99.             v1 = vy
  100.             v2 = vz
  101.  
  102.             new_v0 = old_v0 + MorphValue * (v0 - BaseVertices[index][0])
  103.             new_v1 = old_v1 + MorphValue * (v1 - BaseVertices[index][1])
  104.             new_v2 = old_v2 + MorphValue * (v2 - BaseVertices[index][2])
  105.  
  106.             p = new_v0, new_v1, new_v2
  107.             BaseMesh.set_point_position(index, p)
  108.  
  109.         morphData = file.readline()
  110.  
  111. base_mesh_file = "/home/romain/targets/base.mesh"
  112. base_faces_file = "/home/romain/targets/faces.mesh"
  113. morph_points_file = "~/MH/targets/head/brain-head_brain.target"
  114.  
  115. base_vertices = LoadBasePoints(base_mesh_file)
  116. base_mesh = CreateBaseMesh(Document, base_vertices, base_faces_file)
  117. #Morph(base_mesh, base_vertices, morph_points_file, 0.5)
  118.  
  119.  
  120.