UE4 ひとつなぎのMayaファイルからスケルトンメッシュとアニメーションフレームを指定して読み込み python

アニメーションフレームを指定して読み込み python

python

元々読み込み可能なスケルタルメッシュを推奨します。

# coding: UTF-8
import unreal
import sys
import os
import re
import datetime
import subprocess
import uuid
unreal.log_warning("--------------chara_anim_split_importer.py--start000-----------------")
unreal.log_warning("    ")

class CharaAnimSplitImporter:

    def mainWay(self):
        unreal.log_warning("mainWay 010")
        self.AssetRegistryLoaded=False
        #----------------------------------------------------------
        unreal.log_warning("mainWay 011")
        self.tickhandle = None
        unreal.log_warning("mainWay 012")

        unreal.log_warning("mainWay 0121 10s")
        self.tickhandle = unreal.register_slate_pre_tick_callback(self.testRegistry)
        unreal.log_warning("mainWay 013 tickhandle="+str(self.tickhandle))
        #self.main_way_start2()
        #self.timeloop()
        
    def testRegistry(self,deltaTime):
        unreal.log_warning("testRegistry ticking...mainWay")
        asset_registry = unreal.AssetRegistryHelpers.get_asset_registry()
        if asset_registry.is_loading_assets():
            unreal.log_warning("still loading...mainWay")
        else:
            unreal.log_warning("ready!.....mainWay")
            if(self.AssetRegistryLoaded==False):
                self.AssetRegistryLoaded=True
                self.main_way_start2()
                
            unreal.unregister_slate_pre_tick_callback(self.tickhandle)
            unreal.unregister_slate_pre_tick_callback(self.tickhandle)


    def getUE4EXT(self,file_path):
        basename_without_ext = os.path.splitext(os.path.basename(file_path))[0]
        underBarArr=basename_without_ext.split("_")
        UE4EXT=underBarArr[0]
        return UE4EXT

    def Add_Env_Variable(self):
        #pythonDir=os.path.dirname(sys.argv[0])
        #MyDir=os.path.dirname(__file__)
        MyDir=os.path.dirname(self.get__file__())
        MyDir=os.path.abspath(MyDir)
        MyDir=re.sub(r'\\', '/', MyDir)
        #MyDir=MyDir+"/"
        MyDir=MyDir
        print("MyDir="+MyDir)
        self.MyDir=MyDir
        #K:/Test_Env_Enlighten/Tool/Environment/UnUpResourceData/
        #K:/Test_Env_Enlighten/Tool/Environment/UnUpResourceData/
        
        print("------------sys.path-------------")
        for path in sys.path:
            print("path="+path)
        print("------------sys.path-------------")
        for path in sys.path:
            path=os.path.abspath(path)
            path=re.sub(r'\\', '/', path)
            #print("path ="+path)
            #print("MyDir="+MyDir)
            if(path==MyDir or path+"/"==MyDir):
                print("Hit")
                sys.path.pop()
        print("-----------DELETED------------")
        for path in sys.path:
            print("path="+path)    
        print("-----------DELETED--------------")
        
        sys.path.append(MyDir)
        print("-----------ADDED------------")
        for path in sys.path:
            print("path="+path)    
        print("-----------ADDED--------------")


    def setting_txt_to_dict(self,myDir):
        print(" setting.txt file Open!!")
        test_data = open(myDir+'/setting.txt', "r")

        # 行ごとにすべて読み込んでリストデータにする
        lines = test_data.readlines()
        
        dict = {}
        # 一行ずつ表示する
        for line in lines:
            print(line)
            if(line.find("#")>-1):
                #print("comment line= "+line)
                pass
            elif(line=="\n"):
                pass
            else:
                lines=line.split("=")
                lines[1]=lines[1].replace('\n','')
                lines[1]=lines[1].replace('"','')
                lines[1]=lines[1].replace(' ','')
                lines[0]=lines[0].replace(' ','')
                dict[lines[0]] = lines[1]
           
        for k, v in dict.items(): # for/if文では文末のコロン「:」を忘れないように
            print("key= "+k+" ,value= "+v)
        # ファイルをクローズする
        test_data.close()
        return dict 

    def main_way_start2(self):
        self.Add_Env_Variable()
        
        myDir=self.getmyDir()
        
        myCharaPath=myDir+"Female_A_V3_GameJoint.fbx"
        
        # アニメーションフレーム範囲を読み込む
        self.loadAnimChara(myCharaPath,12,100,"Female_A_V3_GameJoint_Anim_01")
        self.loadAnimChara(myCharaPath,120,180,"Female_A_V3_GameJoint_Anim_02")
        
    def loadAnimChara(self,myCharaPath,startFrame,endFrame,AnimName):
        # FBXインポート時の設定
        mesh_data = unreal.EditorAssetLibrary.find_asset_data("/Game/Mannequin/Character/Mesh/SK_Mannequin")
        #mesh_data = unreal.EditorAssetLibrary.find_asset_data("/Game/UE4_Female_A/Female_A_V3_GameJoint.Female_A_V3_GameJoint")

        mesh = mesh_data.get_asset()
        mesh.skeleton
        
        op = unreal.FbxImportUI()
        #op.import_materials = True # マテリアルもインポート
        op.import_animations=True
        op.set_editor_property("automated_import_should_detect_type", False)
        op.set_editor_property("create_physics_asset", False)
        op.set_editor_property("import_as_skeletal", False)
        op.set_editor_property("import_materials", False)
        op.set_editor_property("import_mesh", False)
        op.set_editor_property("import_rigid_mesh", False)
        op.set_editor_property("import_textures", False)
        op.set_editor_property("is_obj_import", False)
        op.set_editor_property("mesh_type_to_import", unreal.FBXImportType.FBXIT_ANIMATION)

        #op.static_mesh_import_data.combine_meshes = True # メッシュを1つにまとめる
        op.skeleton= mesh.skeleton
        #op.set_editor_property("anim_start_frame", 12)#[Read-Only]
        #op.set_editor_property("anim_end_frame", 100)#[Read-Only]
        #anim_sequence_import_data=op.set_editor_property("anim_sequence_import_data", 100)
        anim_sequence_import_data=op.get_editor_property("anim_sequence_import_data")
        anim_sequence_import_data.set_editor_property("animation_length", unreal.FBXAnimationLengthImportType.FBXALIT_SET_RANGE)
        startEnd=unreal.Int32Interval()
        #startEnd.min=12
        #startEnd.max=100
        startEnd.set_editor_property("min", startFrame)
        startEnd.set_editor_property("max", endFrame)
        #anim_sequence_import_data.frame_import_range  = startEnd
        anim_sequence_import_data.set_editor_property("frame_import_range", startEnd)
        
        
        # FBXインポートのタスクを生成
        task = unreal.AssetImportTask()
        task.automated = True
        #task.destination_path = '/Game/UE4_Female_A_Auto/' # アセットを保存するフォルダ
        task.destination_path = '/Game/UE4_Female_A/Pose/' # アセットを保存するフォルダ
        task.destination_name = 'Female_A_V3_GameJoint' # UE4上のアセット名
        task.destination_name = AnimName # UE4上のアセット名
        task.filename = "E:/download/Sculpt_forger_Girl17_Model_Download_UE4_Checked_EyeLash/Sculpt_forger_Girl17_Model_Download_UE4_Checked_EyeLash/Female_A/Female_A_V3_GameJoint.fbx" # 読み込みたいFBXファイル名を指定する
        task.options = op

        tasks = [task]

        # タスクを実行
        # FBXとマテリアルがインポートされる
        atool = unreal.AssetToolsHelpers.get_asset_tools()
        atool.import_asset_tasks(tasks)
    
    def get__file__(self):
        # 他のファイルから呼べば__file__で取れる。
        return "E:\download\Sculpt_forger_Girl17_Model_Download_UE4_Checked_EyeLash\Sculpt_forger_Girl17_Model_Download_UE4_Checked_EyeLash\Female_A\chara_anim_split_importer.py"
    
    def getmyDir(self):
        #myDir=os.path.dirname(__file__)
        myDir=os.path.dirname(self.get__file__())
        myDir=os.path.abspath(myDir)
        myDir=re.sub(r'\\', '/', myDir)
        myDir=myDir+"/"
        print("myDir="+myDir)
        self.myDir=myDir
        return myDir
        

    def debug_Write(self,s):
        #s= self.debug_data+"\n"
        dt_now = datetime.datetime.now()
        dt_format=dt_now.strftime('%Y_%m_%d__%H_%M_%S')
        #path_w=self.getmyDir()+"/debug_un_up_resource_data_log.csv"
        path_w=self.getmyDir()+"/chara_anim_split_importer_"+dt_format+".csv"
        with open(path_w, mode='w') as f:
            f.write(s)
        
        with open(path_w) as f:
            print(f.read())
        
        
unreal.log("---------------chara_anim_split_importer.py--start class_ins Make-------------------------------")
classIns=CharaAnimSplitImporter()
unreal.log("---------------chara_anim_split_importer.py--start class_ins MainWay()-------------------------------")
classIns.mainWay()

unreal.log_warning("--------------chara_anim_split_importer.py--end000-----------------")
unreal.log_warning("  END PYTHON. ")
unreal.log_warning("  END PYTHON. ")

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です