def sync_update(rpr_context: RPRContext, obj: bpy.types.Object,
                is_updated_geometry, is_updated_transform, **kwargs):
    """ Update existing mesh from obj.data: bpy.types.Mesh or create a new mesh """

    mesh = obj.data
    log("sync_update", obj, mesh)

    obj_key = object.key(obj)
    rpr_shape = rpr_context.objects.get(obj_key, None)
    if rpr_shape:
        if is_updated_geometry:
            rpr_context.remove_object(obj_key)
            sync(rpr_context, obj)
            return True

        if is_updated_transform:
            rpr_shape.set_transform(object.get_transform(obj))

        indirect_only = kwargs.get("indirect_only", False)
        material_override = kwargs.get("material_override", None)

        sync_visibility(rpr_context,
                        obj,
                        rpr_shape,
                        indirect_only=indirect_only)
        assign_materials(rpr_context, rpr_shape, obj, material_override)
        return True

    sync(rpr_context, obj, **kwargs)
    return True
Esempio n. 2
0
def sync_update(rpr_context: RPRContext, obj: bpy.types.Object,
                is_updated_geometry, is_updated_transform) -> bool:
    """ Update existing light from obj.data: bpy.types.Light or create a new light """

    light = obj.data
    log("sync_update", light, obj, is_updated_geometry, is_updated_transform)

    light_key = object.key(obj)
    rpr_light = rpr_context.objects.get(light_key, None)

    if not rpr_light:
        # no such light => creating light
        sync(rpr_context, obj)
        return True

    if is_updated_geometry:
        # light exists, but its settings were changed => recreating light
        rpr_context.remove_object(light_key)
        sync(rpr_context, obj)
        # TODO: Better to set only changed parameters without recreating.
        #  But this idea has to be applied to other objects also with refactoring.
        return True

    if is_updated_transform:
        if light.type == 'AREA' and light.rpr.intensity_normalization:
            # the normalized are light should be recreated to apply scale correctly
            rpr_context.remove_object(light_key)
            sync(rpr_context, obj)
        else:
            # updating only light transform
            rpr_light.set_transform(object.get_transform(obj))
        return True

    return False
def sync(rpr_context: RPRContext, obj: bpy.types.Object, **kwargs):
    """ Creates pyrpr.Shape from obj.data:bpy.types.Mesh """

    mesh = kwargs.get("mesh", obj.data)
    material_override = kwargs.get("material_override", None)
    indirect_only = kwargs.get("indirect_only", False)
    log("sync", mesh, obj, "IndirectOnly" if indirect_only else "")

    obj_key = object.key(obj)
    data = MeshData.init_from_mesh(mesh, obj=obj)
    if not data:
        rpr_context.create_empty_object(obj_key)
        return

    rpr_shape = rpr_context.create_mesh(obj_key, data.vertices, data.normals,
                                        data.uvs, data.vertex_indices,
                                        data.normal_indices, data.uv_indices,
                                        data.num_face_vertices)
    rpr_shape.set_name(obj.name)
    rpr_shape.set_id(obj.pass_index)

    if data.vertex_colors is not None:
        rpr_shape.set_vertex_colors(data.vertex_colors)

    assign_materials(rpr_context, rpr_shape, obj, material_override)

    rpr_context.scene.attach(rpr_shape)
    rpr_shape.set_transform(object.get_transform(obj))

    sync_visibility(rpr_context, obj, rpr_shape, indirect_only=indirect_only)
def sync(rpr_context: RPRContext, material: bpy.types.Material, input_socket_key='Surface', *,
         obj: bpy.types.Object = None):
    """
    If material exists: returns existing material unless force_update is used
    In other cases: returns None
    """

    log(f"sync {material} '{input_socket_key}'; obj {obj}")

    if obj and obj.type=='MESH' and has_uv_map_node(material):
        # only Mesh objects have UV mapping and RPR data field
        mat_key = key((material.name, obj.data.rpr.uv_sets_names), input_socket_key=input_socket_key)
    else:
        mat_key = key(material.name, input_socket_key=input_socket_key)

    rpr_material = rpr_context.materials.get(mat_key, None)
    if rpr_material:
        return rpr_material

    output_node = get_material_output_node(material)
    if not output_node:
        log("No output node", material)
        return None

    data = {'material_key': mat_key, 'object': obj}
    node_parser = ShaderNodeOutputMaterial(rpr_context, material, output_node, None, data=data)
    rpr_material = node_parser.final_export(input_socket_key)

    if rpr_material:
        rpr_context.set_material_node_as_material(mat_key, rpr_material)

    return rpr_material
def sync(rpr_context: RPRContext,
         material: bpy.types.Material,
         input_socket_key='Surface',
         *,
         obj: bpy.types.Object = None):
    """
    If material exists: returns existing material unless force_update is used
    In other cases: returns None
    """

    log(f"sync {material} '{input_socket_key}'; obj {obj}")

    mat_key = key(material, obj, input_socket_key)
    rpr_material = rpr_context.materials.get(mat_key, None)
    if rpr_material:
        return rpr_material

    output_node = get_material_output_node(material)
    if not output_node:
        log("No output node", material)
        return None

    data = {'material_key': mat_key, 'object': obj}
    node_parser = ShaderNodeOutputMaterial(rpr_context,
                                           material,
                                           output_node,
                                           None,
                                           data=data)
    rpr_material = node_parser.final_export(input_socket_key)

    if rpr_material:
        rpr_context.set_material_node_as_material(mat_key, rpr_material)

    return rpr_material
def sync_update(rpr_context: RPRContext,
                material: bpy.types.Material,
                input_socket_key='Surface',
                obj: bpy.types.Object = None):
    """ Recreates existing material """

    log("sync_update", material)

    mat_key = key(material, obj, input_socket_key)
    if mat_key in rpr_context.materials:
        rpr_context.remove_material(mat_key)

    sync(rpr_context, material, obj=obj)

    displacement_key = key(material, obj, 'Displacement')
    if displacement_key in rpr_context.materials:
        rpr_context.remove_material(displacement_key)

    sync(rpr_context, material, input_socket_key='Displacement')

    return True
def sync_update(rpr_context: RPRContext, material: bpy.types.Material, obj: bpy.types.Object = None):
    """ Recreates existing material """

    log("sync_update", material)

    if obj and obj.type=='MESH' and has_uv_map_node(material):
        # only Mesh objects have UV mapping and RPR data field
        mat_key = key((material.name, obj.data.rpr.uv_sets_names),)
    else:
        mat_key = key(material.name)

    if mat_key in rpr_context.materials:
        rpr_context.remove_material(mat_key)

    sync(rpr_context, material, obj=obj)

    displacement_key = key(mat_key, input_socket_key='Displacement')
    if displacement_key in rpr_context.materials:
        rpr_context.remove_material(displacement_key)

    sync(rpr_context, material, input_socket_key='Displacement')

    return True
def sync(rpr_context: RPRContext, obj: bpy.types.Object):
    """ Creates pyrpr.Camera from obj.data: bpy.types.Camera. Created camera sets to scene as default """

    camera = obj.data
    log("sync", camera)

    rpr_camera = rpr_context.create_camera(object.key(obj))
    rpr_camera.set_name(camera.name)

    settings = CameraData.init_from_camera(
        camera, obj.matrix_world, rpr_context.width / rpr_context.height)
    settings.export(rpr_camera)

    # set scene's camera
    rpr_context.scene.set_camera(rpr_camera)
Esempio n. 9
0
def sync(rpr_context: RPRContext, obj: bpy.types.Object, **kwargs):
    """ Creates pyrpr.Shape from obj.data:bpy.types.Mesh """

    mesh = kwargs.get("mesh", obj.data)
    material_override = kwargs.get("material_override", None)
    indirect_only = kwargs.get("indirect_only", False)
    log("sync", mesh, obj, "IndirectOnly" if indirect_only else "")

    obj_key = object.key(obj)
    data = MeshData.init_from_mesh(mesh, obj=obj)
    if not data:
        rpr_context.create_empty_object(obj_key)
        return

    deformation_data = rpr_context.deformation_cache.get(obj_key)
    if deformation_data and np.any(data.vertices != deformation_data.vertices) and \
            np.any(data.normals != deformation_data.normals):
        vertices = np.concatenate((data.vertices, deformation_data.vertices))
        normals = np.concatenate((data.normals, deformation_data.normals))
        rpr_shape = rpr_context.create_mesh(obj_key,
                                            np.ascontiguousarray(vertices),
                                            np.ascontiguousarray(normals),
                                            data.uvs, data.vertex_indices,
                                            data.normal_indices,
                                            data.uv_indices,
                                            data.num_face_vertices,
                                            {pyrpr.MESH_MOTION_DIMENSION: 2})
    else:
        rpr_shape = rpr_context.create_mesh(obj_key, data.vertices,
                                            data.normals, data.uvs,
                                            data.vertex_indices,
                                            data.normal_indices,
                                            data.uv_indices,
                                            data.num_face_vertices)

    rpr_shape.set_name(obj.name)
    rpr_shape.set_id(obj.pass_index)
    rpr_context.set_aov_index_lookup(obj.pass_index, obj.pass_index,
                                     obj.pass_index, obj.pass_index, 1.0)

    if data.vertex_colors is not None:
        rpr_shape.set_vertex_colors(data.vertex_colors)

    assign_materials(rpr_context, rpr_shape, obj, material_override)

    rpr_context.scene.attach(rpr_shape)

    transform = object.get_transform(obj)
    rpr_shape.set_transform(transform)
    object.export_motion_blur(rpr_context, obj_key, transform)

    sync_visibility(rpr_context, obj, rpr_shape, indirect_only=indirect_only)
Esempio n. 10
0
def sync(rpr_context: RPRContext, obj: bpy.types.Object, instance_key=None):
    """ Creates pyrpr.Light from obj.data: bpy.types.Light """

    from rprblender.engine.preview_engine import PreviewEngine

    light = obj.data
    rpr = light.rpr
    log("sync", light, obj)

    area = 0.0
    light_key = object.key(obj) if not instance_key else instance_key

    if light.type == 'POINT':
        if light.rpr.ies_file:
            rpr_light = sync_ies_light(rpr_context, light, light_key)
        elif light.shadow_soft_size > 0:
            rpr_light = rpr_context.create_light(light_key, 'sphere')
            rpr_light.set_radius(light.shadow_soft_size)
        else:
            rpr_light = rpr_context.create_light(light_key, 'point')

    elif light.type in (
            'SUN', 'HEMI'):  # just in case old scenes will have outdated Hemi
        rpr_light = rpr_context.create_light(light_key, 'directional')
        rpr_light.set_shadow_softness_angle(light.rpr.shadow_softness_angle)

    elif light.type == 'SPOT':
        rpr_light = rpr_context.create_light(light_key, 'disk')
        rpr_light.set_radius(light.shadow_soft_size)
        oangle = 0.5 * light.spot_size  # half of spot_size
        iangle = oangle * (1.0 - light.spot_blend * light.spot_blend
                           )  # square dependency of spot_blend
        rpr_light.set_cone_shape(iangle, oangle)

    elif light.type == 'AREA':
        data = mesh.MeshData.init_from_shape_type(rpr.shape,
                                                  light.size,
                                                  light.size_y,
                                                  segments=32)
        area = data.area * obj.scale[0] * obj.scale[1]

        rpr_light = rpr_context.create_area_light(light_key, data.vertices,
                                                  data.normals, data.uvs,
                                                  data.vertex_indices,
                                                  data.normal_indices,
                                                  data.uv_indices,
                                                  data.num_face_vertices)

        rpr_light.set_visibility(rpr.visible)
        rpr_light.set_shadow(rpr.visible and rpr.cast_shadows)

        if rpr.color_map:
            rpr_light.set_image(image.sync(rpr_context, rpr.color_map))

    else:
        raise ValueError("Unsupported light type", light, light.type)

    rpr_light.set_name(light.name)

    power = get_radiant_power(light, area)

    # Material Previews are overly bright, that's why
    # decreasing light intensity for material preview by 10 times
    if rpr_context.engine_type == PreviewEngine.TYPE:
        power /= 10.0

    rpr_light.set_radiant_power(*power)
    rpr_light.set_transform(object.get_transform(obj))
    rpr_light.set_group_id(int(light.rpr.group))

    rpr_context.scene.attach(rpr_light)