def create_blueprint_from_mesh(self): new_blueprint = ue.find_asset(self.path_to_output_asset + '/main_mesh') if new_blueprint is None: ue.log("blueprint class doesn't exists") new_blueprint = ue.create_blueprint( Character, self.path_to_output_asset + '/main_mesh') else: ue.log("blueprint class exists") new_blueprint = ue.find_asset(self.path_to_output_asset + '/main_mesh') # new_blueprint.GeneratedClass.get_cdo().Mesh.RelativeLocation = FVector(0, -200, 150) # new_blueprint.GeneratedClass.get_cdo().Mesh.RelativeRotation = FRotator(0, 0, 0) # add empty static mesh component to blueprint class new_blueprint.GeneratedClass.get_cdo( ).CapsuleComponent.CapsuleHalfHeight = 150 new_blueprint.GeneratedClass.get_cdo( ).CapsuleComponent.CapsuleRadius = 50 st_component = ue.add_component_to_blueprint(new_blueprint, StaticMeshComponent, 'dicom_mesh') ue.log("self.mesh.get_name() = " + self.mesh.get_name()) st_component.StaticMesh = ue.load_object( StaticMesh, self.path_to_output_asset + "/test_test") ue.compile_blueprint(new_blueprint) # saves uasset on the hard disk new_blueprint.save_package() world = ue.get_editor_world() new_actor = world.actor_spawn(new_blueprint.GeneratedClass, FVector(0, 0, 150))
def Spawn(cls, world=None, select=False, nuke=False): '''General purpose spawn function - spawns an actor and returns it. If no world is provided, finds one using GetWorld. cls can be: - the name of the class as a string, in which case it will be imported from unreal_engine.classes - a class previously imported from unreal_engine.classes - a Python class created via the fm.subclassing module If the current world is the editor world and select=True, then the newly spawned actor will be selected before returning. ''' world = world or GetWorld() if isinstance(cls, str): import unreal_engine.classes as c cls = getattr(c, cls) if nuke: for obj in ue.get_editor_world().all_objects()[:]: try: if obj.is_a(cls) and obj.is_valid(): obj.actor_destroy() except: pass #logTB() # TODO: grr, there is some mystery object that is in an invalid state that logs an error - it'd be nice to find it and handle it better if select: ue.editor_deselect_actors() try: ue.allow_actor_script_execution_in_editor(True) newObj = world.actor_spawn(cls) finally: ue.allow_actor_script_execution_in_editor(False) if select: ue.editor_select_actor(newObj) return newObj
def stereo_setup_level_blurprint(video_asset_dir, project_name, shot_name, video_dir): sc_num = len(os.listdir(video_asset_dir)) world = ue.get_editor_world() level_bp = world.CurrentLevel.get_level_script_blueprint() uber_page = level_bp.UberGraphPages[0] from unreal_engine.classes import KismetMathLibrary, K2Node_Timeline, K2Node_ExecutionSequence, K2Node_IfThenElse n = K2Node_ExecutionSequence() y, x = uber_page.graph_get_good_place_for_new_node() node_sequence = uber_page.graph_add_node(n, x, y) t = K2Node_Timeline() y, x = uber_page.graph_get_good_place_for_new_node() node_timeline = uber_page.graph_add_node(t, x, y) pin1 = node_timeline.get_update_pin() pin2 = node_sequence.node_find_pin("execute") pin1.make_link_to(pin2) # 开始程序化操作 for idx, num in enumerate(sc_num): if len(os.listdir(video_dir))>1: stereo = 1 else:
def test_camera_cut_track(self): world = ue.get_editor_world() camera_cut_track = self.asset.sequencer_add_camera_cut_track() camera1 = camera_cut_track.sequencer_track_add_section() cine_camera = world.actor_spawn(CineCameraActor) camera_guid = self.asset.sequencer_add_possessable(cine_camera) camera1.CameraGuid = ue.string_to_guid(camera_guid) print(self.asset.properties()) self.assertEqual(camera_cut_track, self.asset.MovieScene.CameraCutTrack) self.assertEqual(camera_cut_track.sequencer_track_sections(), [camera1])
def test_spawnable(self): world = ue.get_editor_world() character = world.actor_spawn(Character) spawnable_guid = self.asset.sequencer_make_new_spawnable(character) found_spawnable = self.asset.sequencer_find_spawnable(spawnable_guid) guid = ue.string_to_guid(spawnable_guid) self.assertEqual(guid.A, found_spawnable.Guid.A) self.assertEqual(guid.B, found_spawnable.Guid.B) self.assertEqual(guid.C, found_spawnable.Guid.C) self.assertEqual(guid.D, found_spawnable.Guid.D) self.assertEqual(guid, found_spawnable.Guid)
# link variables intensity_node.node_find_pin('intensity').make_link_to( directional_light_set_intensity.node_find_pin('NewIntensity')) directional_light_node.node_find_pin('Directional_light').make_link_to( directional_light_set_intensity.node_find_pin('self')) # a commodity function for finding an event node def get_event_node(event_name): for node in bp.UberGraphPages[0].Nodes: if node.is_a(K2Node_Event): if node.EventReference.MemberName == event_name: return node # get the ReceiveBeginPlay event node begin_play_node = get_event_node('ReceiveBeginPlay') # link BeginPlay to SetIntensity begin_play_node.node_find_pin('then').make_link_to( directional_light_set_intensity.node_find_pin('execute')) # compile the blueprint ue.compile_blueprint(bp) # open related editor ue.open_editor_for_asset(bp) # spawn it ue.get_editor_world().actor_spawn(bp.GeneratedClass)
def stereo_setup_level_bp_variable(player_sets=[]): from unreal_engine.classes import KismetMathLibrary, K2Node_Timeline, K2Node_ExecutionSequence, K2Node_IfThenElse world = ue.get_editor_world() level_bp = world.CurrentLevel.get_level_script_blueprint() left_channel = player_sets[0] if len(player_sets==4): right_channel = player_sets[1] else: right_channel = None pin = EdGraphPinType(PinCategory='object', PinSubCategoryObject=MediaPlayer) left_media = ue.blueprint_add_member_variable(level_bp, left_channel.get_name(), pin, None, left_channel.get_path_name()) right_media = ue.blueprint_add_member_variable(level_bp, right_channel.get_name(), pin, None, right_channel.get_path_name()) uber_page = level_bp.UberGraphPages[0] # 添加media player节点 # ---------------------------------------------- y, x = uber_page.graph_get_good_place_for_new_node() node_left_media = uber_page.graph_add_node_variable_get(left_channel.get_name(), None, x, y) y, x = uber_page.graph_get_good_place_for_new_node() node_right_media = uber_page.graph_add_node_variable_get(right_channel.get_name(), None, x, y) # 添加open source节点 # ---------------------------------------------- y, x = uber_page.graph_get_good_place_for_new_node() node_open_source_L = uber_page.graph_add_node_call_function(MediaPlayer.OpenSource, x, y) y, x = uber_page.graph_get_good_place_for_new_node() node_open_source_R = uber_page.graph_add_node_call_function(MediaPlayer.OpenSource, x, y) # 连接media source,没办法单独的设置资源,只能通过变量来设置 # ---------------------------------------------- pin = EdGraphPinType(PinCategory='object', PinSubCategoryObject=FileMediaSource) ue.blueprint_add_member_variable(level_bp, player_sets[2].get_name(), pin, None, player_sets[2].get_path_name()) pin = EdGraphPinType(PinCategory='object', PinSubCategoryObject=FileMediaSource) ue.blueprint_add_member_variable(level_bp, player_sets[3].get_name(), pin, None, player_sets[3].get_path_name()) c = uber_page.graph_add_node_variable_get(player_sets[2].get_name(), None, x, y) d = uber_page.graph_add_node_variable_get(player_sets[3].get_name(), None, x, y) pin1=c.node_find_pin(player_sets[2].get_name()) pin2=node_open_source_L.node_find_pin("MediaSource") pin1.make_link_to(pin2) pin1=d.node_find_pin(player_sets[3].get_name()) pin2=node_open_source_R.node_find_pin("MediaSource") pin1.make_link_to(pin2) # 添加>=节点, call_function的函数名可以大写也可以小写,直接按照C++文档的来就可以 # ---------------------------------------------- y, x = uber_page.graph_get_good_place_for_new_node() node_greater = uber_page.graph_add_node_call_function(KismetMathLibrary.EqualEqual_FloatFloat, x, y) # 添加branch节点 # ---------------------------------------------- i = K2Node_IfThenElse() y, x = uber_page.graph_get_good_place_for_new_node() node_branch = uber_page.graph_add_node(i, x, y) # 连接节点 # ---------------------------------------------- pin1 = node_greater.node_find_pin("ReturnValue") pin2 = node_branch.node_find_pin("Condition") pin1.make_link_to(pin2) pin1 = node_left_media.node_find_pin(left_channel.get_name()) pin2 = node_open_source_L.node_find_pin('self') pin1.make_link_to(pin2) pin1 = node_right_media.node_find_pin(right_channel.get_name()) pin2 = node_open_source_R.node_find_pin('self') pin1.make_link_to(pin2) pin1 = node_open_source_L.node_find_pin("then") pin2 = node_open_source_R.node_find_pin("execute") pin1.make_link_to(pin2) pin1 = node_branch.node_find_pin("Then") pin2 = node_open_source_L.node_find_pin("execute") pin1.make_link_to(pin2) # compile the blueprint ue.compile_blueprint(level_bp) # open related editor ue.open_editor_for_asset(level_bp) return node_branch.node_find_pin, node_greater.node_find_pin("A")
def setUp(self): self.world = ue.get_editor_world() self.random_string = str(int(time.time()))
class Dicom_Mesh_PyActor: path_to_output_asset = '/Game/DicomMeshAssets/1' fbx_factory = FbxFactory() mesh = None world = ue.get_editor_world() dilimiter = '#' # this is called on game start def begin_play(self): ue.log('Begin Play on Dicom_Mesh class') # found_object = ue.find_object(self.path_to_output_asset + 'test') # ue.log(found_object) # this is called at every 'tick' def tick(self, delta_time): pass def runProcess(self, command): p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while (True): # returns None while subprocess is running retcode = p.poll() line = p.stdout.readline() yield line if retcode is not None: break def dicoms_to_mesh(self, args): # TODO add checkers for file paths # TODO consider migrating to subprocess instead of os args_lst = args.split(self.dilimiter) (invesalius_path, dicoms_folder_path, output_stl_path) = (args_lst[0], args_lst[1], args_lst[2]) invesalius_path = get_full_path(invesalius_path) dicoms_folder_path = get_full_path(dicoms_folder_path) output_stl_path = get_full_path(output_stl_path) ue.log("invesalius path = " + invesalius_path) ue.log("dicoms_folder_path = " + dicoms_folder_path) ue.log("stl_path = " + output_stl_path) ue.log( "calling for invisalius via command line for mesh to stl conversion" ) runProcess("python " + invesalius_path + "/app.py --no-gui -i " + dicoms_folder_path + " -t 200,3033 -e " + output_stl_path) ue.log("finished conversion from dicom files to stl") def stl_to_fbx(self, args): path_to_stl_to_fbx_script = "C:/integrationcourseenv/VRoscopy/VRoscopy/Content/Scripts/stl_to_fbx.py" args_lst = args.split(self.dilimiter) (path_to_stl, fbx_export_path) = (args_lst[0], args_lst[1]) path_to_stl = get_full_path(path_to_stl) fbx_export_path = get_full_path(fbx_export_path) ue.log("path_to_stl = " + path_to_stl) ue.log("fbx_export_path = " + fbx_export_path) ue.log("calling command line for stl to fbx conversion") # blender --background --python C:/integrationcourseenv/VRoscopy/VRoscopy/Content/Scripts/stl_to_fbx.py -- "C:/Users/DAN/Desktop/test.stl" "C:/Users/DAN/Desktop/test.fbx" runProcess("blender --background --python " + path_to_stl_to_fbx_script + " -- " + "\"" + path_to_stl + "\"" + " \"" + fbx_export_path) # subprocess.call("(blender --background --python " + path_to_stl_to_fbx_script + " -- " + "\"" + path_to_stl # + "\"" + " \"" + fbx_export_path + "\")", shell=True) ue.log("finished conversion from stl to fbx") def run_fbx_to_ueasset(self, path_to_fbx=''): if path_to_fbx is None: ue.log("provide correct path to file and proper fbx name") else: path_to_fbx = get_full_path(path_to_fbx) ue.log("path_to_fbx = " + path_to_fbx) # configure the factory self.fbx_factory.ImportUI.bCreatePhysicsAsset = False self.fbx_factory.ImportUI.bImportMaterials = False self.fbx_factory.ImportUI.bImportTextures = False self.fbx_factory.ImportUI.bImportAnimations = False self.fbx_factory.ImportUI.bImportAsSkeletal = True self.fbx_factory.ImportUI.SkeletalMeshImportData.ImportUniformScale = 0.1 # import the mesh ue.log("starting to import fbx to unreal engine") ###### asset = factory.factory_import_object(filename, asset_name) # self.mesh = self.fbx_factory.factory_import_object(path_to_fbx, self.path_to_output_asset) self.mesh = self.fbx_factory.factory_import_object( path_to_fbx, self.path_to_output_asset) self.mesh = ue.find_asset(self.path_to_output_asset + '/test_test') ue.log("finished to import fbx to unreal engine ") self.mesh.save_package() ue.log("save uasset as package") ue.add_on_screen_debug_message(1, 30, self.mesh) def create_blueprint_from_mesh(self): new_blueprint = ue.find_asset(self.path_to_output_asset + '/main_mesh') if new_blueprint is None: ue.log("blueprint class doesn't exists") new_blueprint = ue.create_blueprint( Character, self.path_to_output_asset + '/main_mesh') else: ue.log("blueprint class exists") new_blueprint = ue.find_asset(self.path_to_output_asset + '/main_mesh') # new_blueprint.GeneratedClass.get_cdo().Mesh.RelativeLocation = FVector(0, -200, 150) # new_blueprint.GeneratedClass.get_cdo().Mesh.RelativeRotation = FRotator(0, 0, 0) # add empty static mesh component to blueprint class new_blueprint.GeneratedClass.get_cdo( ).CapsuleComponent.CapsuleHalfHeight = 150 new_blueprint.GeneratedClass.get_cdo( ).CapsuleComponent.CapsuleRadius = 50 st_component = ue.add_component_to_blueprint(new_blueprint, StaticMeshComponent, 'dicom_mesh') ue.log("self.mesh.get_name() = " + self.mesh.get_name()) st_component.StaticMesh = ue.load_object( StaticMesh, self.path_to_output_asset + "/test_test") ue.compile_blueprint(new_blueprint) # saves uasset on the hard disk new_blueprint.save_package() world = ue.get_editor_world() new_actor = world.actor_spawn(new_blueprint.GeneratedClass, FVector(0, 0, 150)) # anim_bp = ue.find_asset(self.path_to_output_asset+'.test') # bpFactory = BlueprintFactory() # new_blueprint = bpFactory.factory_create_new(self.path_to_output_asset + '/Kaiju_BP') # tmp_static_mesh = new_blueprint.add_component_to_blueprint(new_blueprint, StaticMesh, self.mesh) # ue.get_editor_world().actor_spawn(new_blueprint.GeneratedClass) # ue.editor_save_all() # if anim_bp: # ue.delete_asset(anim_bp.get_path_name()) # # anim_bp_factory = AnimBlueprintFactory() # # anim_bp_factory.TargetSkeleton = self.mesh.Skeleton # # anim_bp = anim_bp_factory.factory_create_new(self.path_to_output_asset+'testAnimBP') def add_mesh_to_level(self): dicom_mesh_actor = self.world.actor_spawn(StaticMeshActor) dicom_mesh_actor.StaticMeshComponent.StaticMesh = ue.load_object( StaticMesh, '/Game/DicomMeshAssets/1/test') dicom_mesh_actor.set_actor_label('Dicom Mesh Actor') # mesh = ue.load_object(StaticMesh, '/Game/DicomMeshAssets/1/test') # obj = ue.get_editor_world().actor_spawn(StaticMeshActor) # obj.StaticMeshComponent.StaticMesh = self.mesh pass
def test_add_possessable(self): world = ue.get_editor_world() character = world.actor_spawn(Character) guid = self.asset.sequencer_add_possessable(character) self.assertTrue(guid in self.asset.sequencer_possessables_guid())
def setUp(self): self.world = ue.get_editor_world()
from unreal_engine.classes import MovieSceneAudioTrack, LevelSequenceFactoryNew, MovieSceneSkeletalAnimationTrack, Character, SkeletalMesh, MovieScene3DTransformTrack, CineCameraActor import time from unreal_engine.structs import FloatRange, FloatRangeBound from unreal_engine import FTransform, FVector # create a new level sequence asset factory = LevelSequenceFactoryNew() seq = factory.factory_create_new('/Game/MovieMaster' + str(int(time.time()))) # add an audio track (without sound section ;) to the sequence audio = seq.sequencer_add_master_track(MovieSceneAudioTrack) # get a reference to the editor world (to spawn actors) world = ue.get_editor_world() # spawn a new character and modify it (post_edit_change will allow the editor/sequencer to be notified of actor updates) character = world.actor_spawn(Character) # notify modifications are about to happen... character.modify() character.Mesh.SkeletalMesh = ue.load_object(SkeletalMesh, '/Game/InfinityBladeAdversaries/Enemy/Enemy_Bear/Enemy_Bear.Enemy_Bear') # finalize the actor character.post_edit_change() # add to the sequencer as a possessable (shortcut method returning the guid as string) guid = seq.sequencer_add_actor(character) # add an animation track mapped to the just added actor anim = seq.sequencer_add_track(MovieSceneSkeletalAnimationTrack, guid)
def setUp(self): ue.allow_actor_script_execution_in_editor(True) ue.begin_transaction('test') self.world = ue.get_editor_world() self.blueprint = ue.load_object( Blueprint, '/Game/PlotterPlatforms.PlotterPlatforms')
def setUp(self): ue.allow_actor_script_execution_in_editor(True) ue.begin_transaction('test') self.world = ue.get_editor_world() self.blueprint = ue.load_object(Blueprint, '/Game/PlotterPlatforms.PlotterPlatforms')
from unreal_engine import FVector, FRotator from unreal_engine.classes import StaticMeshActor, StaticMesh import json, os, glob, math geo_folder = geo_directory + "\\" + geo_map + "\\" geo_mtl_file = geo_directory + "\\" + geo_map + "\\" + geo_map + ".mtl" xmodels_directory = geo_directory + r"\xmodels" xmodels_json = geo_directory + "\\" + geo_map + "\\" + geo_map + "_xmodels.json" with open(xmodels_json, 'r') as file: entities = json.load(file) cube_placeholder = ue.load_object(StaticMesh, '/Engine/BasicShapes/Cube.Cube') props_cube = ue.get_editor_world().actor_spawn(StaticMeshActor, FVector(0, 0, 0), FRotator(0, 0, 0)) props_cube.StaticMeshComponent.StaticMesh = cube_placeholder props_cube.set_actor_label('props') mesh_number = 0 last_mesh = '' for actor in entities: if 'Name' in actor: if actor['Name'].endswith("."): actor['Name'] = actor['Name'][:actor['Name'].rfind(".")] #Set all the coordinates, scale and rotations.
def CreateSequencer(): import os.path import time import configparser import unreal_engine as ue from unreal_engine.classes import MovieSceneCameraCutTrack, MovieScene3DTransformSection, MovieScene3DTransformTrack, MovieSceneAudioTrack, CineCameraActor, LevelSequenceFactoryNew if ue.ENGINE_MINOR_VERSION >= 20: from unreal_engine.structs import FloatRange, FloatRangeBound, MovieSceneObjectBindingID, FrameRate else: from unreal_engine.structs import FloatRange, FloatRangeBound, MovieSceneObjectBindingID from unreal_engine import FTransform, FRotator, FVector, FColor from unreal_engine.enums import EMovieSceneObjectBindingSpace from unreal_engine.structs import MovieSceneObjectBindingID seqPath = r'/Game/ImportedFbx/Sequencer' seqName = r'MySequence' seqTempName = r'MySequence'+str(time.time()) mustBeReplace = False startFrame = 1 endFrame = 251 frameRateDenominator = 1.0 frameRateNumerator = 60 secureCrop = 0.0001 #add end crop for avoid section overlay def AddSequencerSectionTransformKeysByIniFile(SequencerSection, SectionFileName, FileLoc): Config = configparser.ConfigParser() Config.read(FileLoc) for option in Config.options(SectionFileName): frame = float(option)/float(frameRateNumerator) #FrameRate list = Config.get(SectionFileName, option) list = list.split(',') transform = FTransform(FVector(float(list[0]), float(list[1]), float(list[2])), FRotator(float(list[3]), float(list[4]), float(list[5]))) SequencerSection.sequencer_section_add_key(frame,transform) def AddSequencerSectionFloatKeysByIniFile(SequencerSection, SectionFileName, FileLoc): Config = configparser.ConfigParser() Config.read(FileLoc) for option in Config.options(SectionFileName): frame = float(option)/float(frameRateNumerator) #FrameRate value = float(Config.get(SectionFileName, option)) SequencerSection.sequencer_section_add_key(frame,value) def AddSequencerSectionBoolKeysByIniFile(SequencerSection, SectionFileName, FileLoc): Config = configparser.ConfigParser() Config.read(FileLoc) for option in Config.options(SectionFileName): frame = float(option)/float(frameRateNumerator) #FrameRate value = Config.getboolean(SectionFileName, option) SequencerSection.sequencer_section_add_key(frame,value) if ue.find_asset(seqPath+'/'+seqName): print("Warning this file already exists") factory = LevelSequenceFactoryNew() seq = factory.factory_create_new(seqPath+'/'+seqTempName.replace('.','')) mustBeReplace = True else: factory = LevelSequenceFactoryNew() seq = factory.factory_create_new(seqPath+'/'+seqName.replace('.','')) if seq is None: return 'Error /!\ level sequencer factory_create fail' print("Sequencer reference created") print(seq) ImportedCamera = [] #(CameraName, CameraGuid) print("========================= Import started ! =========================") #Set frame rate if ue.ENGINE_MINOR_VERSION >= 20: myFFrameRate = FrameRate() myFFrameRate.Denominator = frameRateDenominator myFFrameRate.Numerator = frameRateNumerator seq.MovieScene.DisplayRate = myFFrameRate else: seq.MovieScene.FixedFrameInterval = frameRateDenominator/frameRateNumerator #Set playback range seq.sequencer_set_playback_range(startFrame/frameRateNumerator, (endFrame-secureCrop)/frameRateNumerator) camera_cut_track = seq.sequencer_add_camera_cut_track() world = ue.get_editor_world() #Import camera cut section camera_cut_section = camera_cut_track.sequencer_track_add_section() for camera in ImportedCamera: if camera[0] == 'Camera': camera_cut_section.CameraBindingID = MovieSceneObjectBindingID( Guid=ue.string_to_guid( camera[1] ), Space=EMovieSceneObjectBindingSpace.Local ) camera_cut_section.sequencer_set_section_range(1/frameRateNumerator, (251-secureCrop)/frameRateNumerator) if mustBeReplace == True: OldSeq = seqPath+'/'+seqName.replace('.','')+'.'+seqName.replace('.','') NewSeq = seqPath+'/'+seqTempName.replace('.','')+'.'+seqTempName.replace('.','') print(OldSeq) print(NewSeq) print("LevelSequence'"+OldSeq+"'") print('========================= Imports completed ! =========================') for cam in ImportedCamera: print(cam[0]) print('=========================') #Select and open seq in content browser seq.sequencer_changed(True) return 'Sequencer created with success !'
import unreal_engine as ue from unreal_engine.classes import Actor, Character from unreal_engine import FVector, FRotator #use ue.exec('WorldActor.py') world = ue.get_editor_world() actor000 = world.actor_spawn(Actor, FVector(0, 0, 0), FRotator(0, 0, 0)) character000 = world.actor_spawn(Character, FVector(100, 100, 100), FRotator(0, 0, 0)) # select an actor unreal_engine.editor_select_actor(actor000) ''' # get access to the editor world editor = unreal_engine.get_editor_world() # get the list of selected actors selected_actors = unreal_engine.editor_get_selected_actors() # deselect actors unreal_engine.editor_deselect_actors() # import an asset new_asset = unreal_engine.import_asset(filename, package[, factory_class]) # get a factory class factory = ue.find_class('TextureFactory')