def mkArray(aType, iList): ''' Makes the alembic-usable c++ typed 2-d arrays Parameters ---------- aType : imath type The type of the output array iList : list or np.array The input iterable. Returns ------- : aType The input list translated into an aType array ''' if isinstance(iList, aType): return iList if np is None: array = aType(len(iList)) for i in xrange(len(iList)): array[i] = tuple(iList[i]) return array elif arrayToNumpy is None: array = aType(len(iList)) for i in xrange(len(iList)): array[i] = tuple(iList[i].tolist()) return array else: iList = np.array(iList) array = aType(len(iList)) memView = arrayToNumpy(array) np.copyto(memView, iList) return array
def getSampleArray(imesh, pBar=None): ''' Get the per-frame vertex positions for a mesh Parameters ---------- imesh : IPolyMesh The input alembic mesh object Returns ------- : np.array or list The per-frame vertex positions ''' meshSchema = imesh.getSchema() posProp = meshSchema.getPositionsProperty() numShapes = len(posProp.samples) if arrayToNumpy is not None: shapes = np.empty((len(posProp.samples), len(posProp.samples[0]), 3)) for i, s in enumerate(posProp.samples): pbPrint(pBar, message="Reading Shape", val=i, maxVal=numShapes) shapes[i] = arrayToNumpy(s) elif np is not None: shapes = [] for i, s in enumerate(posProp.samples): pbPrint(pBar, message="Reading Shape", val=i, maxVal=numShapes) shapes.append(s) else: shapes = [] for i, s in enumerate(posProp.samples): pbPrint(pBar, message="Reading Shape", val=i, maxVal=numShapes) shapes.append(s) pbPrint(pBar, message="Done Reading") return shapes
def getSampleArray(imesh): ''' Get the per-frame vertex positions for a mesh Parameters ---------- imesh : IPolyMesh The input alembic mesh object Returns ------- : np.array or list The per-frame vertex positions ''' meshSchema = imesh.getSchema() posProp = meshSchema.getPositionsProperty() if arrayToNumpy is not None: shapes = np.empty((len(posProp.samples), len(posProp.samples[0]), 3)) for i, s in enumerate(posProp.samples): shapes[i] = arrayToNumpy(s) return shapes elif np is not None: shapes = [] for i, s in enumerate(posProp.samples): shapes.append(s) return np.array(shapes) else: shapes = [] for i, s in enumerate(posProp.samples): shapes.append(s) return shapes
def mkArray(aType, iList): ''' Makes the alembic-usable c++ typed arrays ''' iList = np.array(iList) array = aType(len(iList)) memView = arrayToNumpy(array) np.copyto(memView, iList) return array
def mk1dArray(aType, iList): """ Makes the alembic-usable c++ typed 1-d arrays Parameters ---------- aType : imath type The type of the output array iList : list or np.array The input iterable. Returns ------- : aType The input list translated into an aType array """ if isinstance(iList, aType): return iList if np is None or arrayToNumpy is None or aType is UnsignedIntArray: array = aType(len(iList)) for i in range(len(iList)): array[i] = iList[i] return array else: iList = np.array(iList) array = aType(len(iList)) memView = arrayToNumpy(array) np.copyto(memView, iList) return array
def getSampleArray(imesh): meshSchema = imesh.getSchema() posProp = meshSchema.getPositionsProperty() shapes = np.empty((len(posProp.samples), len(posProp.samples[0]), 3)) for i, s in enumerate(posProp.samples): shapes[i] = arrayToNumpy(s) return shapes
def extract_mesh_frame_data(polymesh_sample): """ This function tries to extract the vertex and face indices information from a PolyMesh frame (sample) This function assumes that :param polymesh_sample: an IPolyMeshSchameSample that contains the vertex and face indices of the frame :raises ValueError: when the mesh faces do not contain the same number of indices :return: Tuple[ np.ndarray(#num_vertices, #dim[xyz]), np.ndarray(#num_faces, #num_verts_in_face)]: a tuple containing the frame mesh vertices and face indices """ # extract all the vertex positions from the frame (#num_vertices, #dim[xyz]) mesh_verts = arrayToNumpy(polymesh_sample.getPositions()) # extract the mesh face counts from the frame # NOTE: PyAlembic represents the face indices as a flat array (1 dimension array) # and in order to let the programmer know which indices belong to each face, the "faceCounts" array is utilized # the faceCounts array is a flat array that tells the programmer how much indices each face contains # so suggest we have the following: # face_indices = [0 3 4 2 1 4 5 3 2] # face_counts = [3 3 3] # so using the face_counts array we know we have 3 faces (len(face_counts)), each of them containing 3 vertices # and the faces will be: # face_0 = [0 3 4] # face_1 = [2 1 4] # face_2 = [5 3 2] mesh_face_counts = arrayToNumpy(polymesh_sample.getFaceCounts()) # check that all the faces have the same number of vertices # since we try to reshape the face_indices array to be in a shape of (#num_faces, #num_vertices_in_face), # each face must contain the same number of vertices if np.unique(mesh_face_counts).size > 1: raise ValueError("Mesh faces are not homogeneous, i.e not all the mesh faces contain the same No. of vertices") # create the face indices tensor (#num_faces, #num_vertices_in_face) mesh_face_indices = arrayToNumpy(polymesh_sample.getFaceIndices()).reshape((-1, mesh_face_counts[0])) # return the frame vertices and face indices return mesh_verts, mesh_face_indices
def getStaticMeshArrays(imesh): ''' Get all the generally non-changing data for a mesh as numpy arrays Parameters ---------- imesh : IPolyMesh The input alembic mesh object Returns ------- : np.array or list A flat of vertex indices for the faces as np.array if possible : np.array or list The number of vertices per face as np.array if possible ''' faces, counts = getStaticMeshData(imesh) if arrayToNumpy is not None: faces = arrayToNumpy(faces).copy() counts = arrayToNumpy(counts).copy() elif np is not None: faces, counts = np.array(faces), np.array(counts) else: faces, counts = list(faces), list(counts) return faces, counts
def getSampleArray(imesh): meshSchema = imesh.getSchema() posProp = meshSchema.getPositionsProperty() if arrayToNumpy is not None: shapes = np.empty((len(posProp.samples), len(posProp.samples[0]), 3)) for i, s in enumerate(posProp.samples): shapes[i] = arrayToNumpy(s) return shapes elif np is not None: shapes = [] for i, s in enumerate(posProp.samples): shapes.append(s) return np.array(shapes) else: shapes = [] for i, s in enumerate(posProp.samples): shapes.append(s) return shapes
def loadSmpx(iarch): ''' Load the json and shape data from a .smpx file ''' top = iarch.getTop() par = top.children[0] par = IXform(top, par.getName()) abcMesh = par.children[0] abcMesh = IPolyMesh(par, abcMesh.getName()) meshSchema = abcMesh.getSchema() posProp = meshSchema.getPositionsProperty() print "Loading Verts" shapes = np.empty((len(posProp.samples), len(posProp.samples[0]), 3)) for i, s in enumerate(posProp.samples): shapes[i] = arrayToNumpy(s) print "Done Loading" return shapes
def mkArray(aType, iList): ''' Makes the alembic-usable c++ typed arrays ''' if isinstance(iList, aType): return iList if np is None: array = aType(len(iList)) for i in xrange(len(iList)): array[i] = tuple(iList[i]) return array elif arrayToNumpy is None: array = aType(len(iList)) for i in xrange(len(iList)): array[i] = tuple(iList[i].tolist()) return array else: iList = np.array(iList) array = aType(len(iList)) memView = arrayToNumpy(array) np.copyto(memView, iList) return array
def loadSMPX(cls, smpx): iarch = IArchive(str(smpx)) # because alembic hates unicode try: top = iarch.getTop() par = top.children[0] par = IXform(top, par.getName()) abcMesh = par.children[0] abcMesh = IPolyMesh(par, abcMesh.getName()) systemSchema = par.getSchema() props = systemSchema.getUserProperties() prop = props.getProperty('simplex') jsString = prop.getValue() js = json.loads(jsString) system = cls.loadJSON(js) system._smpx = smpx meshSchema = abcMesh.getSchema() rawFaces = meshSchema.getFaceIndicesProperty().samples[0] rawCounts = meshSchema.getFaceCountsProperty().samples[0] system._faces = [i for i in rawFaces] system._counts = [i for i in rawCounts] print "Loading Verts" posProp = meshSchema.getPositionsProperty() allVerts = np.empty((len(posProp.samples), len(posProp.samples[0]), 3)) for i, s in enumerate(posProp.samples): allVerts[i] = arrayToNumpy(s) for shape, sample in zip(system.shapes, allVerts): shape.loadSMPX(sample) finally: del iarch return system
#!/bin/env python import numpy import imathnumpy from imath import FloatArray, IntArray import PyOpenColorIO as OCIO length = 10 a = numpy.random.uniform(1, 5, length) b = numpy.random.uniform(1, 5, length) fdest = FloatArray(length) dest = imathnumpy.arrayToNumpy(fdest) dest[:] = a * b results = fdest - a * b print("a: {}".format(a)) print("b: {}".format(b)) print("dest: {}".format(fdest)) print("diff: {}".format(results)) config = OCIO.GetCurrentConfig() print("OCIO config: {}".format(config))