Esempio n. 1
0
    def __init__(self,
                 obj,
                 from_objects,
                 width=1024,
                 height=1024,
                 texture_dir=None):
        """
            obj: ベイク先オブジェクト
            width: テクスチャ幅
            height: テクスチャ高さ
        """
        self.obj = obj
        self.from_objects = from_objects
        self.width = width
        self.height = height

        fjw_id = fjw.id(self.obj)
        # self.texture_dir = os.path.dirname(bpy.data.filepath) + os.sep + "textures" + os.sep + fjw_id + os.sep
        # if not os.path.exists(self.texture_dir):
        #     os.makedirs(self.texture_dir)
        if texture_dir:
            self.texture_dir = texture_dir
        else:
            dirname = os.path.dirname(bpy.data.filepath)
            self.texture_dir = dirname + os.sep

        uvu = fjw.UVUtils(self.obj)
        if uvu.is_empty():
            fjw.activate(self.obj)
            fjw.mode("EDIT")
            bpy.ops.mesh.select_all(action='SELECT')
            # bpy.ops.uv.smart_project()
            bpy.ops.uv.lightmap_pack()
            fjw.mode("OBJECT")
    def return_to_basefile(cls):
        json = cls.get_json()
        image_paths = cls.get_exported_image_paths()
        basepath = json.val("basepath")
        fjw_id = json.val("fjw_id")

        #テクスチャ画像を本来あるべき場所にコピー
        texture_export_dir = json.val("texture_export_dir")
        texture_export_dir = bpy.path.abspath(texture_export_dir)

        copied_list = []
        for image_path in image_paths:
            if os.path.exists(image_path):
                copied = shutil.copy(image_path, texture_export_dir)
                copied_list.append(copied)

        #ベースパスを開く
        bpy.ops.wm.open_mainfile(filepath=bpy.path.abspath(basepath))

        #対象オブジェクトを取得
        obj = fjw.id(fjw_id)
        print("return_to_basefile:obj:%s" % str(obj))

        #テクスチャ割当
        tbaker = TextureBaker(obj)
        for copied in copied_list:
            tbaker.assign_image(copied)
Esempio n. 3
0
    def get_bakemat(self):
        """
            アクティブマテリアルをベイク用マテリアルとして取得する。なければ作成する。
        """
        matu = fjw.MaterialUtils(self.obj)
        mat = matu.get_active_mat()
        if not mat:
            mat = matu.mat(fjw.id(self.obj))

        mat.use_transparency = True
        mat.alpha = 0
        return mat
    def get_exist_filepath(cls, obj, exportname):
        fjw_id = fjw.id(obj)
        basefilepath = bpy.data.filepath
        dirname, name, ext = fjw.splitpath(basefilepath)
        fjw_dir = dirname + os.sep + "fjw" + os.sep + name + os.sep + fjw_id
        if ".blend" not in exportname:
            exportname = exportname + ".blend"
        expname, expext = os.path.splitext(exportname)
        fjw_path = fjw_dir + os.sep + exportname

        if os.path.exists(fjw_path):
            return fjw_path
        return None
    def get_exported_image_paths(cls):
        """生成されたテクスチャの絶対パスのリストを返す"""
        dirname, name, ext = fjw.splitpath(bpy.data.filepath)

        image_paths = []
        # nameと同じ名前のオブジェクト
        if name in bpy.context.scene.objects:
            obj = bpy.context.scene.objects[name]
            fjw_id = fjw.id(obj)

            tex_dir = dirname + os.sep + "textures" + os.sep + fjw_id
            if not os.path.exists(tex_dir):
                print("!no tex dir.")
                return None

            files = os.listdir(tex_dir)
            for file in files:
                image_paths.append(tex_dir + os.sep + file)

            return image_paths
        return None
    def make_texture_workingfile(cls, obj, exportname):
        """
            テクスチャ作業用の作業ファイルを作る。
            
        arguments:
            オブジェクト
            
        outputs:
            最終的にどうなるのか
                fjw_work/元blendファイル名/ユニークID/作業タイプ.blend
                metadata.json
                    textures/元blendファイル/ID/作業タイプ
                    テクスチャ出力先
            この後
                選択オブジェクトとターゲット以外削除なりなんなりする
            
        """

        # 前準備
        #     別ファイルに移動するから現状を保存する
        bpy.ops.wm.save_as_mainfile()
        # 必要な情報
        #     元ファイルパス
        basefilepath = bpy.data.filepath
        dirname, name, ext = fjw.splitpath(basefilepath)

        fjw_id = fjw.id(obj)

        fjw_dir = dirname + os.sep + "fjw" + os.sep + name + os.sep + fjw_id
        if ".blend" not in exportname:
            exportname = exportname + ".blend"
        expname, expext = os.path.splitext(exportname)
        fjw_path = fjw_dir + os.sep + exportname
        jsonpath = fjw_dir + os.sep + expname + ".json"

        bpy.context.scene["texture_working_obj"] = fjw_id

        #まずはそのまま保存
        bpy.ops.wm.save_as_mainfile()

        # if os.path.exists(fjw_path):
        #     # 既に存在する場合は破棄すべき!作り直し。
        #     shutil.rmtree(fjw_dir)
        # なんもしなければそのまま新規になるのでは??

        if not os.path.exists(fjw_dir):
            os.makedirs(fjw_dir)
        texture_export_dir = dirname + os.sep + "textures" + os.sep + name + os.sep + fjw_id
        if not os.path.exists(texture_export_dir):
            os.makedirs(texture_export_dir)

        #作業ファイル保存
        bpy.ops.wm.save_as_mainfile(filepath=fjw_path)

        #json保存
        #相対パス化
        json = JsonTools()
        json.val("fjw_id", fjw_id)
        json.val("basepath", bpy.path.relpath(basefilepath))
        json.val("texture_export_dir", bpy.path.relpath(texture_export_dir))
        json.save(jsonpath)
        return json