예제 #1
0
    def reader(self, fileObject, optionString, accessMode):
        drc = Draco()
        mesh = drc.decode(fileObject.fullName())

        # vertices, normals, uvs
        vertices = []
        normals = []
        us = OpenMaya.MFloatArray()
        vs = OpenMaya.MFloatArray()
        poly_count = [3] * mesh.faces_num
        for n in range(mesh.vertices_num):
            i = 3 * n
            vertices.append(
                OpenMaya.MPoint(mesh.vertices[i], mesh.vertices[i + 1],
                                mesh.vertices[i + 2]))
            if mesh.normals:
                normals.append(
                    OpenMaya.MFloatVector(mesh.normals[i], mesh.normals[i + 1],
                                          mesh.normals[i + 2]))
            if mesh.uvs:
                i = 2 * n
                us.append(mesh.uvs[i])
                vs.append(mesh.uvs[i + 1])

        #create mesh
        fnMesh = OpenMaya.MFnMesh()
        newMesh = fnMesh.create(vertices, poly_count, mesh.faces)

        if mesh.normals:
            fnMesh.setVertexNormals(normals, range(len(vertices)))
        if mesh.uvs:
            uvSetsNames = fnMesh.getUVSetNames()
            fnMesh.setUVs(us, vs, uvSetsNames[0])
            fnMesh.assignUVs(poly_count, mesh.faces)

        fnMesh.updateSurface()

        slist = OpenMaya.MGlobal.getSelectionListByName("initialShadingGroup")
        initialSG = slist.getDependNode(0)

        fnSG = OpenMaya.MFnSet(initialSG)
        if fnSG.restriction() == OpenMaya.MFnSet.kRenderableOnly:
            fnSG.addMember(newMesh)
예제 #2
0
    def writer(self, fileObject, optionString, accessMode):
        exportUVs = True
        exportNormals = True

        selection = OpenMaya.MGlobal.getActiveSelectionList()
        dagIterator = OpenMaya.MItSelectionList(selection,
                                                OpenMaya.MFn.kGeometric)

        drcIndices = []
        drcVertices = []
        drcNormals = []
        drcUvs = []
        indicesOffset = 0

        while not dagIterator.isDone():
            dagPath = dagIterator.getDagPath()
            try:
                fnMesh = OpenMaya.MFnMesh(dagPath)
            except Exception as e:
                dagIterator.next()
                continue

            meshPoints = fnMesh.getPoints(OpenMaya.MSpace.kWorld)
            polygonsIterator = OpenMaya.MItMeshPolygon(dagPath)

            meshNormals = normals = u = v = uvSetsNames = uvs = None

            if exportNormals:
                meshNormals = fnMesh.getNormals()
                normals = {}

            if exportUVs:
                uvSetsNames = fnMesh.getUVSetNames()
                u, v = fnMesh.getUVs(uvSetsNames[0])
                uvs = {}

            while not polygonsIterator.isDone():
                if not polygonsIterator.hasValidTriangulation():
                    raise ValueError("The mesh has not valid triangulation")
                polygonVertices = polygonsIterator.getVertices()
                numTriangles = polygonsIterator.numTriangles()

                localindices = []
                for i in range(numTriangles):
                    points, indices = polygonsIterator.getTriangle(i)
                    drcIndices.append(indicesOffset)
                    indicesOffset += 1
                    drcIndices.append(indicesOffset)
                    indicesOffset += 1
                    drcIndices.append(indicesOffset)
                    indicesOffset += 1
                    localindices.append(indices[0])
                    localindices.append(indices[1])
                    localindices.append(indices[2])

                    localIndex = []
                    for gt in range(len(indices)):
                        for gv in range(len(polygonVertices)):
                            if indices[gt] == polygonVertices[gv]:
                                localIndex.append(gv)
                                break
                    if exportNormals:
                        normals[indices[0]] = meshNormals[
                            polygonsIterator.normalIndex(localIndex[0])]
                        normals[indices[1]] = meshNormals[
                            polygonsIterator.normalIndex(localIndex[1])]
                        normals[indices[2]] = meshNormals[
                            polygonsIterator.normalIndex(localIndex[2])]

                    if exportUVs and polygonsIterator.hasUVs():
                        uvID = [0, 0, 0]
                        for vtxInPolygon in range(3):
                            uvID[vtxInPolygon] = polygonsIterator.getUVIndex(
                                localIndex[vtxInPolygon], uvSetsNames[0])
                        uvs[indices[0]] = (u[uvID[0]], v[uvID[0]])
                        uvs[indices[1]] = (u[uvID[1]], v[uvID[1]])
                        uvs[indices[2]] = (u[uvID[2]], v[uvID[2]])

                for i in localindices:
                    drcVertices.append(meshPoints[i].x)
                    drcVertices.append(meshPoints[i].y)
                    drcVertices.append(meshPoints[i].z)
                    if exportNormals:
                        drcNormals.append(normals[i][0])
                        drcNormals.append(normals[i][1])
                        drcNormals.append(normals[i][2])
                    if exportUVs and polygonsIterator.hasUVs():
                        drcUvs.append(uvs[i][0])
                        drcUvs.append(uvs[i][1])

                polygonsIterator.next(None)
            dagIterator.next()

        drcMesh = DrcMesh()
        drcMesh.faces_num = len(drcIndices) / 3
        drcMesh.faces_len = len(drcIndices)
        drcMesh.faces = drcIndices

        drcMesh.vertices_num = len(drcVertices) / 3
        drcMesh.vertices_len = len(drcVertices)
        drcMesh.vertices = drcVertices

        if exportNormals:
            drcMesh.normals = drcNormals
            drcMesh.normals_len = len(drcNormals)
            drcMesh.normals_num = len(drcVertices) / 3

        if exportUVs:
            drcMesh.uvs = drcUvs
            drcMesh.uvs_len = len(drcUvs)
            drcMesh.uvs_num = len(drcVertices) / 3

        draco = Draco()
        draco.encode(drcMesh, fileObject.fullName())
예제 #3
0
	def setUp(self): 
		self.drc = Draco()
예제 #4
0
class DracoTest(unittest.TestCase):
	
	def setUp(self): 
		self.drc = Draco()

	def test_decode_bunny_drc(self):
		mesh = self.drc.decode(os.path.join(dir_path, 'res/bunny.drc'))
		# Faces check
		self.assertEqual(69451, mesh.faces_num, 'Number of faces')
		self.assertEqual(208353, mesh.faces_len,'Length of faces array precalculated')
		self.assertEqual(208353, len(mesh.faces),'Length of faces array by len')
		# Vertices check
		self.assertEqual(34834, mesh.vertices_num, 'Number of vertices')
		self.assertEqual(104502, mesh.vertices_len,'Length of vertices array precalculated')
		self.assertEqual(104502, len(mesh.vertices),'Length of vertices array by len')
		# Normals check
		self.assertEqual(0, mesh.normals_num, 'Number of normals')
		self.assertEqual(0, mesh.normals_len,'Length of normals array precalculated')
		self.assertEqual(0, len(mesh.normals),'Length of normals array by len')
		# Uvs check
		self.assertEqual(0, mesh.uvs_num, 'Number of uvs')
		self.assertEqual(0, mesh.uvs_len,'Length of uvs ')
		self.assertEqual(0, len(mesh.uvs),'Length of uvs array by len')

	def test_decode_trooper_drc(self):
		mesh = self.drc.decode(os.path.join(dir_path, 'res/stormtrooper.drc'))
		# Faces check
		self.assertEqual(6518, mesh.faces_num, 'Number of faces')
		self.assertEqual(19554, mesh.faces_len,'Length of faces array precalculated')
		self.assertEqual(19554, len(mesh.faces),'Length of faces array by len')
		# Vertices check
		self.assertEqual(5176, mesh.vertices_num, 'Number of vertices')
		self.assertEqual(15528, mesh.vertices_len,'Length of vertices array precalculated')
		self.assertEqual(15528, len(mesh.vertices),'Length of vertices array by len')
		# Normals check
		self.assertEqual(5176, mesh.normals_num, 'Number of normals')
		self.assertEqual(15528, mesh.normals_len, 'Length of normals array precalculated')
		self.assertEqual(15528, len(mesh.normals),'Length of normals array by len')
		# Uvs check
		self.assertEqual(5176, mesh.uvs_num, 'Number of uvs')
		self.assertEqual(10352, mesh.uvs_len, 'Length of uvs array')
		self.assertEqual(10352, len(mesh.uvs), 'Length of uvs array by len')

	def test_decode_unexistent_drc(self):
		self.assertRaises(Exception, self.drc.decode, 'res/unexistent.drc')

	def test_encode_triangle_mesh(self):
		mesh = DrcMesh()
		mesh.faces = [0, 1, 2]
		mesh.faces_num = 1
		mesh.vertices = [0, 0, 0, 1, 1, 1, 2, 2, 2]
		mesh.vertices_num = 3
		
		file = os.path.join(dir_path,'triangle.drc')
		file_del(file)
		
		self.drc.encode(mesh, file)
		self.assertTrue(os.path.isfile(file), 'File should exists!')
		
		file_del(file)
		
	def test_encode_and_decode_triangle_mesh(self):
		mesh = DrcMesh()
		mesh.faces = [0, 1, 2]
		mesh.faces_num = 1
		mesh.vertices = [0, 0, 0, 1, 1, 1, 2, 2, 2]
		mesh.vertices_num = 3
		file = os.path.join(dir_path, 'res/triangle.drc')
		file_del(file)
		self.drc.encode(mesh, file)
		
		dmesh = self.drc.decode(file)
		# Faces check
		self.assertEqual(1, dmesh.faces_num, 'Number of faces')
		self.assertEqual(3, dmesh.faces_len,'Length of faces array precalculated')
		self.assertEqual([0, 1, 2], dmesh.faces, 'Face Array')
		# Vertices check
		self.assertEqual(3, dmesh.vertices_num, 'Number of vertices')
		self.assertEqual(9, dmesh.vertices_len,'Length of vertices array precalculated')
		self.assertEqual([0, 0, 0, 1, 1, 1, 2, 2, 2], dmesh.vertices, 'Vertex Array')

		file_del(file)

	def test_decode_and_encode_stoormtrup_drc(self):
		# Step1: decode
		mesh = self.drc.decode(os.path.join(dir_path, 'res/stormtrooper.drc'))
		# Step2: encode
		file = os.path.join(dir_path, 'res/stormtrooper_copy.drc')
		file_del(file)
		self.drc.encode(mesh, file)
		# Step3: re-decode and test
		dmesh = self.drc.decode(file)
		#    Faces check
		self.assertEqual(6518, dmesh.faces_num, 'Number of faces')
		self.assertEqual(19554, dmesh.faces_len,'Length of faces array precalculated')
		self.assertEqual(19554, len(dmesh.faces),'Length of faces array by len')
		#    Vertices check
		self.assertEqual(5176, dmesh.vertices_num, 'Number of vertices')
		self.assertEqual(15528, dmesh.vertices_len,'Length of vertices array precalculated')
		self.assertEqual(15528, len(dmesh.vertices),'Length of vertices array by len')
		#    Normals check
		self.assertEqual(5176, dmesh.normals_num, 'Number of normals')
		self.assertEqual(15528, dmesh.normals_len, 'Length of normals array precalculated')
		self.assertEqual(15528, len(dmesh.normals),'Length of normals array by len')

		file_del(file)