Example #1
0
def getColorAttributesFromModel(model):
    
    # Calculate the net transformation
    transform = model.getNetTransform()
    transformMat = transform.getMat()
    
    areas = []
    rgbColors = []
    textures = []
    transparencies = []
    for nodePath in model.findAllMatches('**/+GeomNode'):
        geomNode = nodePath.node()
        
        for n in range(geomNode.getNumGeoms()):
            state = geomNode.getGeomState(n)
        
            geom = geomNode.getGeom(n)
            area = getSurfaceAreaFromGeom(geom, transformMat)
        
            if state.hasAttrib(TextureAttrib.getClassType()):
                # Get color from texture
                texAttr = state.getAttrib(TextureAttrib.getClassType())
                tex = texAttr.getTexture()
                
                # Load texture image from file and compute average color
                texFilename = str(tex.getFullpath())
                img = scipy.ndimage.imread(texFilename)

                texture = os.path.splitext(os.path.basename(texFilename))[0]
                
                #TODO: handle black-and-white and RGBA texture
                assert img.dtype == np.uint8
                assert img.ndim == 3 and img.shape[-1] == 3
                
                rgbColor = (np.mean(img, axis=(0,1)) / 255.0).tolist()

                rgbColors.append(rgbColor)
                transparencies.append(False)
                areas.append(area)
                textures.append(texture)

            elif state.hasAttrib(ColorAttrib.getClassType()):
                colorAttr = state.getAttrib(ColorAttrib.getClassType())
                
                if (colorAttr.getColorType() == ColorAttrib.TFlat or colorAttr.getColorType() == ColorAttrib.TOff):
                    # Get flat color
                    color = colorAttr.getColor()
                    
                    isTransparent = False
                    if isinstance(color, LVecBase4f):
                        rgbColor= [color[0], color[1], color[2]]
                        alpha = color[3]
                        
                        if state.hasAttrib(TransparencyAttrib.getClassType()):
                            transAttr = state.getAttrib(TransparencyAttrib.getClassType())
                            if transAttr.getMode() != TransparencyAttrib.MNone and alpha < 1.0:
                                isTransparent = True
                        elif alpha < 1.0:
                            isTransparent = True
                            
                    elif isinstance(color, LVecBase3f):
                        rgbColor= [color[0], color[1], color[2]]
                    else:
                        raise Exception('Unsupported color class type: %s' % (color.__class__.__name__))
                
                    rgbColors.append(rgbColor)
                    transparencies.append(isTransparent)
                    areas.append(area)
                    textures.append(None)
                
                else:
                    # Get colors from vertex data
                    verAreas, verRgbColors, vertransparencies = getColorAttributesFromVertexData(geom, transformMat)
                    areas.extend(verAreas)
                    rgbColors.extend(verRgbColors)
                    transparencies.extend(vertransparencies)
                    textures.extend([None,]*len(vertransparencies))
            
    areas = np.array(areas)
    areas /= np.sum(areas)
            
    return areas, rgbColors, transparencies, textures