Esempio n. 1
0
def get_combined_ver_str():
    """Returns combined version string from Blender version and Blender Tools version.
    :return: combined version string
    :rtype: str
    """
    from io_scs_tools import get_tools_version

    (version, build) = get_blender_version()
    return "Blender " + version + build + ", SCS Blender Tools: " + get_tools_version()
Esempio n. 2
0
def initialise_scs_dict(scene):
    """Parts and Variants data initialisation (persistent).

    Things which this function does:
    1. copies all the settings to current world
    2. checks object identities
    3. updates shaders presets path and reloads them

    Cases when it should be run:
    1. Blender startup -> SCS tools needs to configured
    2. Opening .blend file -> because all the configs needs to be moved to current world
    3. addon reloading and enable/disable -> for SCS tools this is the same as opening Blender

    :param scene: Current Blender Scene
    :type scene: bpy.types.Scene
    """

    # SCREEN CHECK...
    if bpy.context.screen:
        lprint("I Initialization of SCS scene, BT version: " +
               get_tools_version())

        # NOTE: covers: start-up, reload, enable/disable and it should be immediately removed
        # from handlers as soon as it's executed for the first time
        if initialise_scs_dict in bpy.app.handlers.scene_update_post:
            bpy.app.handlers.scene_update_post.remove(initialise_scs_dict)

        # INITIALIZE CUSTOM CONNECTIONS DRAWING SYSTEM
        _connections_group_wrapper.init()

        # USE SETTINGS FROM CONFIG...
        # NOTE: Reapplying the settings from config file to the currently opened Blender file datablock.
        # The thing is, that every Blend file holds its own copy of SCS Global Settings from the machine on which it got saved.
        # The SCS Global Settings needs to be overwritten upon each file load to reflect the settings from local config file,
        # but also upon every SCS Project Base Path change.
        _config_container.apply_settings()

        # GLOBAL PATH CHECK...
        if _get_scs_globals().scs_project_path != "":
            if not os.path.isdir(_get_scs_globals().scs_project_path):
                lprint(
                    "\nW The Project Path %r is NOT VALID!\n\tPLEASE SELECT A VALID PATH TO THE PROJECT BASE FOLDER.\n",
                    (_get_scs_globals().scs_project_path, ))

        # CREATE PREVIEW MODEL LIBRARY
        _preview_models.init()

        # ADD DRAW HANDLERS
        _open_gl_callback.enable(mode=_get_scs_globals().drawing_mode)
Esempio n. 3
0
def initialise_scs_dict(scene):
    """Parts and Variants data initialisation (persistent).

    Things which this function does:
    1. copies all the settings to current world
    2. checks object identities
    3. updates shaders presets path and reloads them

    Cases when it should be run:
    1. Blender startup -> SCS tools needs to configured
    2. Opening .blend file -> because all the configs needs to be moved to current world
    3. addon reloading and enable/disable -> for SCS tools this is the same as opening Blender

    :param scene: Current Blender Scene
    :type scene: bpy.types.Scene
    """

    # SCREEN CHECK...
    if bpy.context.screen:
        lprint("I Initialization of SCS scene, BT version: " + get_tools_version())

        # NOTE: covers: start-up, reload, enable/disable and it should be immediately removed
        # from handlers as soon as it's executed for the first time
        if initialise_scs_dict in bpy.app.handlers.scene_update_post:
            bpy.app.handlers.scene_update_post.remove(initialise_scs_dict)

        # INITIALIZE CUSTOM CONNECTIONS DRAWING SYSTEM
        _connections_group_wrapper.init()

        # USE SETTINGS FROM CONFIG...
        # NOTE: Reapplying the settings from config file to the currently opened Blender file datablock.
        # The thing is, that every Blend file holds its own copy of SCS Global Settings from the machine on which it got saved.
        # The SCS Global Settings needs to be overwritten upon each file load to reflect the settings from local config file,
        # but also upon every SCS Project Base Path change.
        _config_container.apply_settings()

        # GLOBAL PATH CHECK...
        if _get_scs_globals().scs_project_path != "":
            if not os.path.isdir(_get_scs_globals().scs_project_path):
                lprint("\nW The Project Path %r is NOT VALID!\n\tPLEASE SELECT A VALID PATH TO THE PROJECT BASE FOLDER.\n",
                       (_get_scs_globals().scs_project_path,))

        # CREATE PREVIEW MODEL LIBRARY
        _preview_models.init()

        # ADD DRAW HANDLERS
        _open_gl_callback.enable(mode=_get_scs_globals().drawing_mode)
Esempio n. 4
0
def post_load(scene):
    # get Blender Tools version from last blend file load
    last_load_bt_ver = _get_scs_globals().last_load_bt_version

    scs_roots = None
    """
    Applies fixes for v0.6 or less:
    1. fixes reflection textures tga's for tobjs as TOBJ load is now supported and unlock that textures
    2. calls update on all set textures to correct paths for them
    3. tries to fix active shader preset name for materials, because of new flavor system
    """
    if _info_utils.cmp_ver_str(last_load_bt_ver, "0.6") <= 0:

        print("INFO\t-  Applying fixes for version <= 0.6")

        for material in bpy.data.materials:

            # ignore materials not related to blender tools
            if material.scs_props.mat_effect_name == "":
                continue

            for tex_type in material.scs_props.get_texture_types().keys():

                texture_attr_str = "shader_texture_" + tex_type
                if texture_attr_str in material.scs_props.keys():

                    # 1. fix reflection textures
                    if tex_type == "reflection":

                        is_building_ref = material.scs_props[
                            texture_attr_str].endswith("/bulding_ref.tga")
                        is_generic_s = material.scs_props[
                            texture_attr_str].endswith(
                                "material/environment/generic_s.tga")
                        is_glass_interior = material.scs_props.active_shader_preset_name == "glass - interior"
                        is_dif_spec_weight_add_env = material.scs_props.active_shader_preset_name == "dif.spec.weight.add.env"
                        is_truckpaint = material.scs_props.active_shader_preset_name.startswith(
                            "truckpaint")

                        # fix paths
                        if is_building_ref:
                            material.scs_props[
                                texture_attr_str] = material.scs_props[
                                    texture_attr_str][:-4]
                            material.scs_props[texture_attr_str +
                                               "_locked"] = False
                        elif is_generic_s:
                            if is_glass_interior:
                                material.scs_props[
                                    texture_attr_str] = "//material/environment/interior_reflection"
                            elif is_dif_spec_weight_add_env:
                                material.scs_props[
                                    texture_attr_str] = "//material/environment/generic_reflection"
                            else:
                                material.scs_props[
                                    texture_attr_str] = "//material/environment/vehicle_reflection"

                            # unlock reflection textures everywhere except on truckpaint shader
                            if not is_truckpaint:
                                material.scs_props[texture_attr_str +
                                                   "_locked"] = False

                        # acquire roots on demand only once
                        scs_roots = _object_utils.gather_scs_roots(
                            bpy.data.objects) if not scs_roots else scs_roots

                        # propagate reflection texture change on all of the looks.
                        # NOTE: We can afford write through because old BT had all reflection textures locked
                        # meaning user had to use same texture on all looks
                        # NOTE#2: Printouts like:
                        # "Look with ID: X doesn't have entry for material 'X' in SCS Root 'X',
                        #  property 'shader_texture_reflection' won't be updated!"
                        # are expected here, because we don't use any safety check,
                        # if material is used on the mesh objects inside scs root
                        for scs_root in scs_roots:
                            _looks.write_through(scs_root, material,
                                                 texture_attr_str)

                    # 2. trigger update function for path reload and reload of possible missing textures
                    update_func = getattr(material.scs_props,
                                          "update_" + texture_attr_str, None)
                    if update_func:
                        update_func(material)

            # ignore already properly set materials
            if material.scs_props.active_shader_preset_name in _get_shader_presets_inventory(
            ):
                continue

            # 3. try to recover "active_shader_preset_name" from none flavor times Blender Tools
            material_textures = {}
            if "scs_shader_attributes" in material and "textures" in material[
                    "scs_shader_attributes"]:
                for texture in material["scs_shader_attributes"][
                        "textures"].values():
                    tex_id = texture["Tag"].split(":")[1]
                    tex_value = texture["Value"]
                    material_textures[tex_id] = tex_value

            (preset_name, preset_section) = _material_utils.find_preset(
                material.scs_props.mat_effect_name, material_textures)
            if preset_name:
                material.scs_props.active_shader_preset_name = preset_name

                # acquire roots on demand only once
                scs_roots = _object_utils.gather_scs_roots(
                    bpy.data.objects) if not scs_roots else scs_roots

                # make sure to fix active preset shader name in all looks
                # NOTE: Printouts like:
                # "Look with ID: X doesn't have entry for material 'X' in SCS Root 'X',
                #  property 'active_shader_preset_name' won't be updated!"
                # are expected here, because we don't use any safety check,
                # if material is used on the mesh objects inside scs root
                for scs_root in scs_roots:
                    _looks.write_through(scs_root, material,
                                         "active_shader_preset_name")

    # as last update "last load" Blender Tools version to current
    _get_scs_globals().last_load_bt_version = get_tools_version()
Esempio n. 5
0
def post_load(scene):
    # get Blender Tools version from last blend file load
    last_load_bt_ver = _get_scs_globals().last_load_bt_version

    scs_roots = None

    """
    Applies fixes for v0.6 or less:
    1. fixes reflection textures tga's for tobjs as TOBJ load is now supported and unlock that textures
    2. calls update on all set textures to correct paths for them
    3. tries to fix active shader preset name for materials, because of new flavor system
    """
    if _info_utils.cmp_ver_str(last_load_bt_ver, "0.6") <= 0:

        print("INFO\t-  Applying fixes for version <= 0.6")

        for material in bpy.data.materials:

            # ignore materials not related to blender tools
            if material.scs_props.mat_effect_name == "":
                continue

            for tex_type in material.scs_props.get_texture_types().keys():

                texture_attr_str = "shader_texture_" + tex_type
                if texture_attr_str in material.scs_props.keys():

                    # 1. fix reflection textures
                    if tex_type == "reflection":

                        is_building_ref = material.scs_props[texture_attr_str].endswith("/bulding_ref.tga")
                        is_generic_s = material.scs_props[texture_attr_str].endswith("material/environment/generic_s.tga")
                        is_glass_interior = material.scs_props.active_shader_preset_name == "glass - interior"
                        is_dif_spec_weight_add_env = material.scs_props.active_shader_preset_name == "dif.spec.weight.add.env"
                        is_truckpaint = material.scs_props.active_shader_preset_name.startswith("truckpaint")

                        # fix paths
                        if is_building_ref:
                            material.scs_props[texture_attr_str] = material.scs_props[texture_attr_str][:-4]
                            material.scs_props[texture_attr_str + "_locked"] = False
                        elif is_generic_s:
                            if is_glass_interior:
                                material.scs_props[texture_attr_str] = "//material/environment/interior_reflection"
                            elif is_dif_spec_weight_add_env:
                                material.scs_props[texture_attr_str] = "//material/environment/generic_reflection"
                            else:
                                material.scs_props[texture_attr_str] = "//material/environment/vehicle_reflection"

                            # unlock reflection textures everywhere except on truckpaint shader
                            if not is_truckpaint:
                                material.scs_props[texture_attr_str + "_locked"] = False

                        # acquire roots on demand only once
                        scs_roots = _object_utils.gather_scs_roots(bpy.data.objects) if not scs_roots else scs_roots

                        # propagate reflection texture change on all of the looks.
                        # NOTE: We can afford write through because old BT had all reflection textures locked
                        # meaning user had to use same texture on all looks
                        # NOTE#2: Printouts like:
                        # "Look with ID: X doesn't have entry for material 'X' in SCS Root 'X',
                        #  property 'shader_texture_reflection' won't be updated!"
                        # are expected here, because we don't use any safety check,
                        # if material is used on the mesh objects inside scs root
                        for scs_root in scs_roots:
                            _looks.write_through(scs_root, material, texture_attr_str)

                    # 2. trigger update function for path reload and reload of possible missing textures
                    update_func = getattr(material.scs_props, "update_" + texture_attr_str, None)
                    if update_func:
                        update_func(material)

            # ignore already properly set materials
            if material.scs_props.active_shader_preset_name in _get_shader_presets_inventory():
                continue

            # 3. try to recover "active_shader_preset_name" from none flavor times Blender Tools
            material_textures = {}
            if "scs_shader_attributes" in material and "textures" in material["scs_shader_attributes"]:
                for texture in material["scs_shader_attributes"]["textures"].values():
                    tex_id = texture["Tag"].split(":")[1]
                    tex_value = texture["Value"]
                    material_textures[tex_id] = tex_value

            (preset_name, preset_section) = _material_utils.find_preset(material.scs_props.mat_effect_name, material_textures)
            if preset_name:
                material.scs_props.active_shader_preset_name = preset_name

                # acquire roots on demand only once
                scs_roots = _object_utils.gather_scs_roots(bpy.data.objects) if not scs_roots else scs_roots

                # make sure to fix active preset shader name in all looks
                # NOTE: Printouts like:
                # "Look with ID: X doesn't have entry for material 'X' in SCS Root 'X',
                #  property 'active_shader_preset_name' won't be updated!"
                # are expected here, because we don't use any safety check,
                # if material is used on the mesh objects inside scs root
                for scs_root in scs_roots:
                    _looks.write_through(scs_root, material, "active_shader_preset_name")

    # as last update "last load" Blender Tools version to current
    _get_scs_globals().last_load_bt_version = get_tools_version()