<commit_before>from .rman_translator import RmanTranslator
from ..rman_sg_nodes.rman_sg_mesh import RmanSgMesh
from ..rman_utils import object_utils
from ..rman_utils import string_utils
from ..rman_utils import property_utils

import bpy
import math
import numpy as np

def _get_mats_faces_(nverts, material_ids):

    mats = {}
    for face_id, num_verts in enumerate(nverts):
        mat_id = material_ids[face_id]
        if mat_id not in mats:
            mats[mat_id] = []
        mats[mat_id].append(face_id)
    return mats

def _is_multi_material_(ob, mesh):
    if type(mesh) != bpy.types.Mesh or len(ob.data.materials) < 2 \
            or len(mesh.polygons) == 0:
        return False
    first_mat = mesh.polygons[0].material_index
    for p in mesh.polygons:
        if p.material_index != first_mat:
            return True
    return False

# requires facevertex interpolation
def _get_mesh_uv_(mesh, name=""):
    uvs = []
    if not name:
        uv_loop_layer = mesh.uv_layers.active
    else:
        # assuming uv loop layers and uv textures share identical indices
        idx = mesh.uv_textures.keys().index(name)
        uv_loop_layer = mesh.uv_layers[idx]

    if uv_loop_layer is None:
        return None

    uv_count = len(uv_loop_layer.data)
    fastuvs = np.zeros(uv_count * 2)
    uv_loop_layer.data.foreach_get("uv", fastuvs)
    fastuvs.reshape(uv_count, 2)    
    uvs = fastuvs.tolist()

    return uvs

def _get_mesh_vcol_(mesh, name=""):
    vcol_layer = mesh.vertex_colors[name] if name != "" \
        else mesh.vertex_colors.active
    cols = []

    if vcol_layer is None:
        return None

    vcol_count = len(vcol_layer.data)
    fastvcols = np.zeros(vcol_count * 4)
    vcol_layer.data.foreach_get("color", fastvcols)
    fastvcols.reshape(vcol_count, 4)    
    cols = fastvcols.tolist()        

    return cols    

def _get_mesh_vgroup_(ob, mesh, name=""):
    vgroup = ob.vertex_groups[name] if name != "" else ob.vertex_groups.active
    weights = []

    if vgroup is None:
        return None

    for v in mesh.vertices:
        if len(v.groups) == 0:
            weights.append(0.0)
        else:
            weights.extend([g.weight for g in v.groups
                            if g.group == vgroup.index])

    return weights

def _get_material_ids(ob, geo):
        
    material_ids = string_utils.convert_val([p.material_index for p in geo.polygons])
    return material_ids

def _get_primvars_(ob, geo, rixparams, interpolation=""):

    rm = ob.data.renderman

    interpolation = 'facevarying' if not interpolation else interpolation

    if rm.export_default_uv:
        uvs = _get_mesh_uv_(geo)
        if uvs and len(uvs) > 0:
            rixparams.SetFloatArrayDetail("st", uvs, 2, interpolation)

    if rm.export_default_vcol:
        vcols = _get_mesh_vcol_(geo)
        if vcols and len(vcols) > 0:
            rixparams.SetColorDetail("Cs", string_utils.convert_val(vcols, type_hint="color"), interpolation)
    
    # custom prim vars

    for p in rm.prim_vars:
        if p.data_source == 'VERTEX_COLOR':
            vcols = _get_mesh_vcol_(geo, p.data_name)
            
            if vcols and len(vcols) > 0:
                rixparams.SetColorDetail(p.name, string_utils.convert_val(vcols, type_hint="color"), interpolation)<commit_msg>Force the interpolation on the vertex color primvars to be vertex, for meshes. Windows seems to be more sensitive to this and was causing a crash.<commit_after>from .rman_translator import RmanTranslator
from ..rman_sg_nodes.rman_sg_mesh import RmanSgMesh
from ..rman_utils import object_utils
from ..rman_utils import string_utils
from ..rman_utils import property_utils

import bpy
import math
import numpy as np

def _get_mats_faces_(nverts, material_ids):

    mats = {}
    for face_id, num_verts in enumerate(nverts):
        mat_id = material_ids[face_id]
        if mat_id not in mats:
            mats[mat_id] = []
        mats[mat_id].append(face_id)
    return mats

def _is_multi_material_(ob, mesh):
    if type(mesh) != bpy.types.Mesh or len(ob.data.materials) < 2 \
            or len(mesh.polygons) == 0:
        return False
    first_mat = mesh.polygons[0].material_index
    for p in mesh.polygons:
        if p.material_index != first_mat:
            return True
    return False

# requires facevertex interpolation
def _get_mesh_uv_(mesh, name=""):
    uvs = []
    if not name:
        uv_loop_layer = mesh.uv_layers.active
    else:
        # assuming uv loop layers and uv textures share identical indices
        idx = mesh.uv_textures.keys().index(name)
        uv_loop_layer = mesh.uv_layers[idx]

    if uv_loop_layer is None:
        return None

    uv_count = len(uv_loop_layer.data)
    fastuvs = np.zeros(uv_count * 2)
    uv_loop_layer.data.foreach_get("uv", fastuvs)
    fastuvs.reshape(uv_count, 2)    
    uvs = fastuvs.tolist()

    return uvs

def _get_mesh_vcol_(mesh, name=""):
    vcol_layer = mesh.vertex_colors[name] if name != "" \
        else mesh.vertex_colors.active
    cols = []

    if vcol_layer is None:
        return None

    vcol_count = len(vcol_layer.data)
    fastvcols = np.zeros(vcol_count * 4)
    vcol_layer.data.foreach_get("color", fastvcols)
    fastvcols.reshape(vcol_count, 4)    
    cols = fastvcols.tolist()        

    return cols    

def _get_mesh_vgroup_(ob, mesh, name=""):
    vgroup = ob.vertex_groups[name] if name != "" else ob.vertex_groups.active
    weights = []

    if vgroup is None:
        return None

    for v in mesh.vertices:
        if len(v.groups) == 0:
            weights.append(0.0)
        else:
            weights.extend([g.weight for g in v.groups
                            if g.group == vgroup.index])

    return weights

def _get_material_ids(ob, geo):
        
    material_ids = string_utils.convert_val([p.material_index for p in geo.polygons])
    return material_ids

def _get_primvars_(ob, geo, rixparams, interpolation=""):

    rm = ob.data.renderman

    interpolation = 'facevarying' if not interpolation else interpolation

    if rm.export_default_uv:
        uvs = _get_mesh_uv_(geo)
        if uvs and len(uvs) > 0:
            rixparams.SetFloatArrayDetail("st", uvs, 2, interpolation)

    if rm.export_default_vcol:
        vcols = _get_mesh_vcol_(geo)
        if vcols and len(vcols) > 0:
            rixparams.SetColorDetail("Cs", string_utils.convert_val(vcols, type_hint="color"), "vertex")
    
    # custom prim vars

    for p in rm.prim_vars:
        if p.data_source == 'VERTEX_COLOR':
            vcols = _get_mesh_vcol_(geo, p.data_name)
            
            if vcols and len(vcols) > 0:
                rixparams.SetColorDetail(p.name, string_utils.convert_val(vcols, type_hint="color"), "vertex")
{'fraction_non_alphanumeric': 0.09418896939228154, 'fraction_numerical': 0.0041401744787816054, 'mean_word_length': 2.63265306122449, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3940750', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2230, 'n_words': 542}