[Unreal Engine]複数のカメラのスクリーンショットを自動的に撮るPython

複数のカメラのスクリーンショットを自動的に撮るPython

Log Python: Automation Library: Failed to convert parameter ‘camera’ when calling function ‘AutomationBlueprintFunctionLibrary.TakeHighResScreenshot’ on ‘Default__AutomationBlueprintFunctionLibrary’ TypeError: NativizeProperty: Cannot nativize ‘SceneCaptureActor’ as ‘Camera’ (ObjectProperty) TypeError: NativizeObject: Cannot nativize ‘SceneCaptureActor’ as ‘Object’ (allowed Class type: ‘CameraActor’)

IS FIX Code

import unreal
import time

class OnTick(object):
  """ Register a function and run it on tick, """
  def __init__(self):
    # make a generator function so we can call next on it,
    
    EditorLevelLibrary = unreal.EditorLevelLibrary()
    all_level_actors=EditorLevelLibrary.get_all_level_actors()
    CameraActors=[]
    for actor in all_level_actors:
        #CameraBool=actor.find_camera_component_when_view_target
        CameraComponent=actor.get_component_by_class(unreal.CameraComponent)
        #print("CameraComponent="+str(CameraComponent))
        #className=CameraComponent.get_class() 
        #print("CameraComponent.get_class() ="+str(className))
        if(str(CameraComponent)=="None"):
            pass
        else:
            #if(str(CameraComponent) in "CineCameraComponent"):
            #    pass
            #else: 
            CameraActors.append(actor)
    
    self.CameraActors=CameraActors
    #self.actors = (actor for actor in unreal.EditorLevelLibrary.get_selected_level_actors())
    self.actors = iter(CameraActors)
    
    self.tickcount=0
    self.CapturedCount=0
    # register a callback on tick
    self.on_tick = unreal.register_slate_post_tick_callback(self.__tick__)
    
    
  def __tick__(self, deltatime):
    """ Function we will call every tick, """
    try:
      print(deltatime) # Print the deltatime just for sanity check so we
      # know a tick was made,
        
      # Get the next actor from our generator function,
      actor = next(self.actors)
      """
      actor_location = actor.get_actor_location()
      actor_rotation = actor.get_actor_rotation() 
      # Solve the camera position, and rotation
      position = actor_location + unreal.Vector(0.0, 0.0, 0.0)

      # roll(z), pitch(tate), yaw(yoko)
      rotation = unreal.Rotator(actor_rotation.roll+0.0, actor_rotation.pitch+5.0,actor_rotation.yaw+0.0)
      
      unreal.EditorLevelLibrary.set_level_viewport_camera_info(
        position,
        rotation
      )
      """
      errorBool=False
      try:
          CameraComponent=actor.get_component_by_class(unreal.CameraComponent)
          print("actor= "+str(actor))
          print("CameraComponent= "+str(CameraComponent))
          unreal.AutomationLibrary.take_high_res_screenshot(
            1920,
            1080,
            str(self.CapturedCount)+"_"+actor.get_name() + ".hdr",
            camera=actor,
            capture_hdr=True,
            comparison_tolerance=unreal.ComparisonTolerance.HIGH
          )
          errorBool=False
          
      except Exception as error:
          errorBool=True
          pass
      
      if(errorBool==False):
          # Count
          self.CapturedCount=self.CapturedCount+1
      """
      unreal.EditorLevelLibrary.pilot_level_actor(actor)
      unreal.EditorLevelLibrary.editor_set_game_view(True)
      # Take the screenshot,
      unreal.AutomationLibrary.take_high_res_screenshot(
        1920,
        1080,
        actor.get_name() + ".hdr",
        camera=actor,
        capture_hdr=True,
        comparison_tolerance=unreal.ComparisonTolerance.HIGH,
        delay=0.25
      )
      
      unreal.EditorLevelLibrary.eject_pilot_level_actor()
      """
      # Count
      self.tickcount=self.tickcount+1
      
      CameraActors=self.CameraActors
      CameraActorsLen=len(CameraActors)
      endActor=CameraActors[CameraActorsLen-1]
      print("self.tickcount="+str(self.tickcount)+"  :CameraActorsLen="+str(CameraActorsLen))
      if(str(self.tickcount)==str(CameraActorsLen)):
          print("finish")
          unreal.unregister_slate_post_tick_callback(self.on_tick)
      
    except Exception as error:
      #pass
      print(error)
      try:
          unreal.unregister_slate_post_tick_callback(self.on_tick)
      except Exception as error:
          pass
#if __name__ == '__main__':
instance = OnTick()

関連URL

https://forums.unrealengine.com/t/python-api-highrescreenshot/132783/10

https://docs.unrealengine.com/5.1/en-US/PythonAPI/class/EditorLevelLibrary.html

https://docs.unrealengine.com/5.0/en-US/PythonAPI/class/AutomationLibrary.html

https://docs.unrealengine.com/5.1/en-US/taking-screenshots-in-unreal-engine/

https://docs.unrealengine.com/4.26/en-US/BlueprintAPI/Utilities/Struct/MakeAutomationScreenshotOptions/

コメントを残す

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