Exemple #1
0
        def invoke(self, context, event):
            """Invoke a file path selector."""
            filepath = getattr(bpy.context.active_object.active_material.scs_props, self.shader_texture)
            if filepath.startswith(str(os.sep + os.sep)) and os.path.isdir(_get_scs_globals().scs_project_path):
                self.filepath = os.path.join(_get_scs_globals().scs_project_path, _path_utils.strip_sep(filepath))
            elif os.path.isfile(filepath):
                self.filepath = filepath
            else:
                self.filepath = _get_scs_globals().scs_project_path

            context.window_manager.fileselect_add(self)
            return {'RUNNING_MODAL'}
Exemple #2
0
        def invoke(self, context, event):
            """Invoke a file path selector."""
            filepath = getattr(bpy.context.active_object.active_material.scs_props, self.shader_texture)
            if filepath.startswith("//") and os.path.isdir(_get_scs_globals().scs_project_path):
                self.filepath = os.path.join(_get_scs_globals().scs_project_path, _path_utils.strip_sep(filepath))
            elif os.path.isfile(filepath):
                self.filepath = filepath
            else:
                self.filepath = _get_scs_globals().scs_project_path

            context.window_manager.fileselect_add(self)
            return {'RUNNING_MODAL'}
Exemple #3
0
def get_texture_path_from_material(material, texture_type, export_path):
    """Get's relative path for Texture section of tobj from given texture_type.
    If tobj is not yet created it also creates tobj for it.

    :param material: Blender material
    :type material: bpy.types.Material
    :param texture_type: type of texture which should be readed from material (example "texture_base")
    :type texture_type: str
    :return: relative path for Texture section data of PIT material
    :rtype: str
    """

    # overwrite tobj value directly if specified
    if getattr(material.scs_props, "shader_" + texture_type + "_use_imported",
               False):
        return getattr(material.scs_props,
                       "shader_" + texture_type + "_imported_tobj", "")

    # use tobj value from shader preset if texture is locked and has default value
    if "scs_shader_attributes" in material and "textures" in material[
            "scs_shader_attributes"]:
        for tex_entry in material["scs_shader_attributes"]["textures"].values(
        ):
            if "Tag" in tex_entry and texture_type in tex_entry["Tag"]:
                if "Lock" in tex_entry and tex_entry["Lock"] == "True":
                    if "Value" in tex_entry and tex_entry["Value"] != "":
                        return tex_entry["Value"]

    # CALCULATING TOBJ AND TEXTURE PATHS
    texture_raw_path = getattr(material.scs_props, "shader_" + texture_type,
                               "NO PATH")
    tobj_rel_filepath = tobj_abs_filepath = texture_abs_filepath = ""
    scs_project_path = _get_scs_globals().scs_project_path.rstrip("\\").rstrip(
        "/")

    extensions, texture_raw_path = _path_utils.get_texture_extens_and_strip_path(
        texture_raw_path)

    for ext in extensions:
        if texture_raw_path.startswith("//"):  # relative

            # search for relative path inside current scs project base and
            # possible dlc/mod parent folders; use first found
            for infix in ("", "../base/", "../../base/"):

                curr_path = os.path.join(scs_project_path,
                                         infix + texture_raw_path[2:] + ext)

                if os.path.isfile(curr_path):

                    tobj_rel_filepath = texture_raw_path.replace("//", "/")

                    # if tobj is used by user then get texture path from tobj
                    # otherwise get tobj path from texture path
                    if ext == ".tobj":
                        tobj_abs_filepath = curr_path
                        texture_abs_filepath = _path_utils.get_texture_path_from_tobj(
                            curr_path)
                    else:
                        tobj_abs_filepath = _path_utils.get_tobj_path_from_shader_texture(
                            curr_path, check_existance=False)
                        texture_abs_filepath = curr_path
                    break

            # break searching for texture if texture was found
            if tobj_rel_filepath != "":
                break

        elif ext != ".tobj" and os.path.isfile(texture_raw_path +
                                               ext):  # absolute

            texture_raw_path_with_ext = texture_raw_path + ext

            # if we are exporting somewhere into SCS Project Base Path texture still can be saved
            if scs_project_path != "" and _path_utils.startswith(
                    export_path, scs_project_path):

                tex_dir, tex_filename = os.path.split(
                    texture_raw_path_with_ext)
                tobj_filename = tex_filename + ".tobj"

                # copy texture beside exported files
                try:
                    shutil.copy2(texture_raw_path_with_ext,
                                 os.path.join(export_path, tex_filename))
                except OSError as e:
                    # ignore copying the same file
                    # NOTE: happens if absolute texture paths are used
                    # even if they are referring to texture inside scs project path
                    if type(e).__name__ != "SameFileError":
                        raise e

                # copy also TOBJ if exists
                texture_raw_tobj_path = str(tex_dir) + os.sep + tobj_filename
                if os.path.isfile(texture_raw_tobj_path):
                    shutil.copy2(texture_raw_tobj_path,
                                 os.path.join(export_path, tobj_filename))

                # get copied TOBJ relative path to current scs project path
                tobj_rel_filepath = ""
                if export_path != scs_project_path:
                    tobj_rel_filepath = os.sep + os.path.relpath(
                        export_path, scs_project_path)

                tobj_rel_filepath = tobj_rel_filepath + os.sep + tobj_filename[:
                                                                               -5]
                tobj_abs_filepath = os.path.join(export_path, tobj_filename)
                texture_abs_filepath = texture_raw_path_with_ext
                break

            else:
                lprint(
                    "E Can not properly export texture %r from material %r!\n\t   "
                    +
                    "Make sure you are exporting somewhere into Project Base Path and texture is properly set!",
                    (texture_raw_path, material.name))
                return ""

    else:
        lprint(
            "E Texture file %r from material %r doesn't exists inside current Project Base Path.\n\t   "
            +
            "TOBJ  won't be exported and reference will remain empty, expect problems!",
            (texture_raw_path, material.name))
        return ""

    # CREATE TOBJ FILE
    if not os.path.isfile(tobj_abs_filepath):  # only if it does not exists yet

        # export tobj only if file of texture exists
        if os.path.isfile(texture_abs_filepath):
            texture_name = os.path.basename(
                _path_utils.strip_sep(texture_abs_filepath))
            _tobj.export(tobj_abs_filepath, texture_name, set())
        else:
            lprint(
                "E Texture file %r from material %r doesn't exists, TOBJ can not be exported!",
                (texture_raw_path, material.name))

    # make sure that Windows users will export proper paths
    tobj_rel_filepath = tobj_rel_filepath.replace("\\", "/")

    return tobj_rel_filepath
Exemple #4
0
def _get_texture_path_from_material(material, texture_type, export_path):
    """Get's relative path for Texture section of tobj from given texture_type.
    If tobj is not yet created it also creates tobj for it.

    :param material: Blender material
    :type material: bpy.types.Material
    :param texture_type: type of texture which should be readed from material (example "texture_base")
    :type texture_type: str
    :return: relative path for Texture section data of PIT material
    :rtype: str
    """

    # overwrite tobj value directly if specified
    if getattr(material.scs_props, "shader_" + texture_type + "_use_imported",
               False):
        return getattr(material.scs_props,
                       "shader_" + texture_type + "_imported_tobj", "")

    # use tobj value from shader preset if texture is locked and has default value
    if "scs_shader_attributes" in material and "textures" in material[
            "scs_shader_attributes"]:
        for tex_entry in material["scs_shader_attributes"]["textures"].values(
        ):
            if "Tag" in tex_entry and texture_type in tex_entry["Tag"]:
                if "Lock" in tex_entry and tex_entry["Lock"] == "True":
                    if "Value" in tex_entry and tex_entry["Value"] != "":
                        return tex_entry["Value"]

    # TEXTURE PATH
    texture_raw_path = getattr(material.scs_props, "shader_" + texture_type,
                               "NO PATH")
    texture_abs_filepath = _path_utils.get_abs_path(texture_raw_path)

    # TOBJ PATH
    scs_project_path = _get_scs_globals().scs_project_path.rstrip("\\").rstrip(
        "/")
    if os.path.isfile(scs_project_path + os.sep +
                      texture_raw_path):  # relative within base

        tobj_rel_filepath = os.path.splitext(texture_raw_path)[0][1:]
        tobj_abs_filepath = str(
            os.path.splitext(texture_abs_filepath)[0] + ".tobj")

    elif os.path.isfile(texture_raw_path):  # absolute

        # if we are exporting somewhere into SCS Project Base Path texture still can be saved
        if scs_project_path != "" and export_path.startswith(scs_project_path):

            tex_dir, tex_filename = os.path.split(texture_raw_path)
            tobj_filename = tex_filename.rsplit(".", 1)[0] + ".tobj"

            # copy texture beside exported files
            shutil.copy2(texture_raw_path,
                         os.path.join(export_path, tex_filename))

            # copy also TOBJ if exists
            texture_raw_tobj_path = tex_dir + os.sep + tobj_filename
            if os.path.isfile(texture_raw_tobj_path):
                shutil.copy2(texture_raw_tobj_path,
                             os.path.join(export_path, tobj_filename))

            tobj_rel_filepath = ""
            if export_path != scs_project_path:
                tobj_rel_filepath = os.sep + os.path.relpath(
                    export_path, scs_project_path)

            tobj_rel_filepath = tobj_rel_filepath + os.sep + tobj_filename[:-5]
            tobj_abs_filepath = os.path.join(export_path, tobj_filename)

        else:
            lprint(
                "E Can not properly export texture %r from material %r!\n\t   "
                +
                "Make sure you are exporting somewhere into Project Base Path and texture is properly set!",
                (texture_raw_path, material.name))
            return ""

    else:
        lprint(
            "E Texture file %r from material %r doesn't exists inside current Project Base Path, TOBJ can not be exported!",
            (texture_raw_path, material.name))
        return ""

    # CREATE TOBJ FILE
    if not os.path.isfile(tobj_abs_filepath):  # only if it does not exists yet

        # export tobj only if file of texture exists
        if os.path.isfile(texture_abs_filepath):
            settings = getattr(material.scs_props,
                               "shader_" + texture_type + "_settings", set())
            texture_name = os.path.basename(
                _path_utils.strip_sep(texture_raw_path))
            _tobj.export(tobj_abs_filepath, texture_name, settings)
        else:
            lprint(
                "E Texture file %r from material %r doesn't exists, TOBJ can not be exported!",
                (texture_raw_path, material.name))

    # make sure that Windows users will export proper paths
    tobj_rel_filepath = tobj_rel_filepath.replace("\\", "/")

    return tobj_rel_filepath
Exemple #5
0
def _get_texture_path_from_material(material, texture_type, export_path):
    """Get's relative path for Texture section of tobj from given texture_type.
    If tobj is not yet created it also creates tobj for it.

    :param material: Blender material
    :type material: bpy.types.Material
    :param texture_type: type of texture which should be readed from material (example "texture_base")
    :type texture_type: str
    :return: relative path for Texture section data of PIT material
    :rtype: str
    """

    # overwrite tobj value directly if specified
    if getattr(material.scs_props, "shader_" + texture_type + "_use_imported", False):
        return getattr(material.scs_props, "shader_" + texture_type + "_imported_tobj", "")

    # use tobj value from shader preset if texture is locked and has default value
    if "scs_shader_attributes" in material and "textures" in material["scs_shader_attributes"]:
        for tex_entry in material["scs_shader_attributes"]["textures"].values():
            if "Tag" in tex_entry and texture_type in tex_entry["Tag"]:
                if "Lock" in tex_entry and tex_entry["Lock"] == "True":
                    if "Value" in tex_entry and tex_entry["Value"] != "":
                        return tex_entry["Value"]

    # TEXTURE PATH
    texture_raw_path = getattr(material.scs_props, "shader_" + texture_type, "NO PATH")
    texture_abs_filepath = _path_utils.get_abs_path(texture_raw_path)

    # TOBJ PATH
    scs_project_path = _get_scs_globals().scs_project_path.rstrip("\\").rstrip("/")
    if os.path.isfile(scs_project_path + os.sep + texture_raw_path):  # relative within base

        tobj_rel_filepath = os.path.splitext(texture_raw_path)[0][1:]
        tobj_abs_filepath = str(os.path.splitext(texture_abs_filepath)[0] + ".tobj")

    elif os.path.isfile(texture_raw_path):  # absolute

        # if we are exporting somewhere into SCS Project Base Path texture still can be saved
        if scs_project_path != "" and export_path.startswith(scs_project_path):

            tex_dir, tex_filename = os.path.split(texture_raw_path)
            tobj_filename = tex_filename.rsplit(".", 1)[0] + ".tobj"

            # copy texture beside exported files
            shutil.copy2(texture_raw_path, os.path.join(export_path, tex_filename))

            # copy also TOBJ if exists
            texture_raw_tobj_path = tex_dir + os.sep + tobj_filename
            if os.path.isfile(texture_raw_tobj_path):
                shutil.copy2(texture_raw_tobj_path, os.path.join(export_path, tobj_filename))

            tobj_rel_filepath = ""
            if export_path != scs_project_path:
                tobj_rel_filepath = os.sep + os.path.relpath(export_path, scs_project_path)

            tobj_rel_filepath = tobj_rel_filepath + os.sep + tobj_filename[:-5]
            tobj_abs_filepath = os.path.join(export_path, tobj_filename)

        else:
            lprint("E Can not properly export texture %r from material %r!\n\t   " +
                   "Make sure you are exporting somewhere into Project Base Path and texture is properly set!",
                   (texture_raw_path, material.name))
            return ""

    else:
        lprint("E Texture file %r from material %r doesn't exists inside current Project Base Path, TOBJ can not be exported!",
               (texture_raw_path, material.name))
        return ""

    # CREATE TOBJ FILE
    if not os.path.isfile(tobj_abs_filepath):  # only if it does not exists yet

        # export tobj only if file of texture exists
        if os.path.isfile(texture_abs_filepath):
            settings = getattr(material.scs_props, "shader_" + texture_type + "_settings", set())
            texture_name = os.path.basename(_path_utils.strip_sep(texture_raw_path))
            _tobj.export(tobj_abs_filepath, texture_name, settings)
        else:
            lprint("E Texture file %r from material %r doesn't exists, TOBJ can not be exported!",
                   (texture_raw_path, material.name))

    # make sure that Windows users will export proper paths
    tobj_rel_filepath = tobj_rel_filepath.replace("\\", "/")

    return tobj_rel_filepath
Exemple #6
0
def _get_texture_path_from_material(material, texture_type, export_path):
    """Get's relative path for Texture section of tobj from given texture_type.
    If tobj is not yet created it also creates tobj for it.

    :param material: Blender material
    :type material: bpy.types.Material
    :param texture_type: type of texture which should be readed from material (example "texture_base")
    :type texture_type: str
    :return: relative path for Texture section data of PIT material
    :rtype: str
    """

    # overwrite tobj value directly if specified
    if getattr(material.scs_props, "shader_" + texture_type + "_use_imported", False):
        return getattr(material.scs_props, "shader_" + texture_type + "_imported_tobj", "")

    # use tobj value from shader preset if texture is locked and has default value
    if "scs_shader_attributes" in material and "textures" in material["scs_shader_attributes"]:
        for tex_entry in material["scs_shader_attributes"]["textures"].values():
            if "Tag" in tex_entry and texture_type in tex_entry["Tag"]:
                if "Lock" in tex_entry and tex_entry["Lock"] == "True":
                    if "Value" in tex_entry and tex_entry["Value"] != "":
                        return tex_entry["Value"]

    # CALCULATING TOBJ AND TEXTURE PATHS
    texture_raw_path = getattr(material.scs_props, "shader_" + texture_type, "NO PATH")
    tobj_rel_filepath = tobj_abs_filepath = texture_abs_filepath = ""
    scs_project_path = _get_scs_globals().scs_project_path.rstrip("\\").rstrip("/")

    extensions, texture_raw_path = _path_utils.get_texture_extens_and_strip_path(texture_raw_path)

    for ext in extensions:
        if texture_raw_path.startswith("//"):  # relative

            # search for relative path inside current scs project base and
            # possible dlc/mod parent folders; use first found
            for infix in ("", "../base/", "../../base/"):

                curr_path = os.path.join(scs_project_path, infix + texture_raw_path[2:] + ext)

                if os.path.isfile(curr_path):

                    tobj_rel_filepath = texture_raw_path.replace("//", "/")

                    # if tobj is used by user then get texture path from tobj
                    # otherwise get tobj path from texture path
                    if ext == ".tobj":
                        tobj_abs_filepath = curr_path
                        texture_abs_filepath = _path_utils.get_texture_path_from_tobj(curr_path)
                    else:
                        tobj_abs_filepath = _path_utils.get_tobj_path_from_shader_texture(curr_path, check_existance=False)
                        texture_abs_filepath = curr_path
                    break

            # break searching for texture if texture was found
            if tobj_rel_filepath != "":
                break

        elif ext != ".tobj" and os.path.isfile(texture_raw_path + ext):  # absolute

            texture_raw_path_with_ext = texture_raw_path + ext

            # if we are exporting somewhere into SCS Project Base Path texture still can be saved
            if scs_project_path != "" and _path_utils.startswith(export_path, scs_project_path):

                tex_dir, tex_filename = os.path.split(texture_raw_path_with_ext)
                tobj_filename = tex_filename + ".tobj"

                # copy texture beside exported files
                try:
                    shutil.copy2(texture_raw_path_with_ext, os.path.join(export_path, tex_filename))
                except OSError as e:
                    # ignore copying the same file
                    # NOTE: happens if absolute texture paths are used
                    # even if they are referring to texture inside scs project path
                    if type(e).__name__ != "SameFileError":
                        raise e

                # copy also TOBJ if exists
                texture_raw_tobj_path = str(tex_dir) + os.sep + tobj_filename
                if os.path.isfile(texture_raw_tobj_path):
                    shutil.copy2(texture_raw_tobj_path, os.path.join(export_path, tobj_filename))

                # get copied TOBJ relative path to current scs project path
                tobj_rel_filepath = ""
                if export_path != scs_project_path:
                    tobj_rel_filepath = os.sep + os.path.relpath(export_path, scs_project_path)

                tobj_rel_filepath = tobj_rel_filepath + os.sep + tobj_filename[:-5]
                tobj_abs_filepath = os.path.join(export_path, tobj_filename)
                texture_abs_filepath = texture_raw_path_with_ext
                break

            else:
                lprint("E Can not properly export texture %r from material %r!\n\t   " +
                       "Make sure you are exporting somewhere into Project Base Path and texture is properly set!",
                       (texture_raw_path, material.name))
                return ""

    else:
        lprint("E Texture file %r from material %r doesn't exists inside current Project Base Path.\n\t   " +
               "TOBJ  won't be exported and reference will remain empty, expect problems!",
               (texture_raw_path, material.name))
        return ""

    # CREATE TOBJ FILE
    if not os.path.isfile(tobj_abs_filepath):  # only if it does not exists yet

        # export tobj only if file of texture exists
        if os.path.isfile(texture_abs_filepath):
            texture_name = os.path.basename(_path_utils.strip_sep(texture_abs_filepath))
            _tobj.export(tobj_abs_filepath, texture_name, set())
        else:
            lprint("E Texture file %r from material %r doesn't exists, TOBJ can not be exported!",
                   (texture_raw_path, material.name))

    # make sure that Windows users will export proper paths
    tobj_rel_filepath = tobj_rel_filepath.replace("\\", "/")

    return tobj_rel_filepath