Untitled

 avatar
unknown
python
a month ago
4.2 kB
7
Indexable
# ----------------------------------------------------------------------------
# Requirements
# ----------------------------------------------------------------------------

# Plugins: 
#   Scripting -
#       Python
#       Editor Scripting Utilities
#       Python Foundation Packages
#   Geometry -
#       Geometry
#       Geometry Processing
#       Geometry Script

# ----------------------------------------------------------------------------
# Import from /includes
# ----------------------------------------------------------------------------
import unreal
import os

# ----------------------------------------------------------------------------
# Unreal Library Classes
# ----------------------------------------------------------------------------
ur = unreal
ur_edit_asset_lib = unreal.EditorAssetLibrary()
ur_asset_helper = unreal.AssetToolsHelpers.get_asset_tools()  
ur_subsystem: unreal.SubobjectDataSubsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)

physics_actor: unreal.Name = unreal.Name("PhysicsActor")
block_all: unreal.Name = unreal.Name("BlockAll")

# ----------------------------------------------------------------------------
# Import from /includes
# ----------------------------------------------------------------------------
#================================================
# * find_asset_outliner()
# ?
# @ input string
# @ return actor that was found
#================================================
def find_asset_outliner(input:str) -> unreal.Actor:
    dyn_mesh = unreal.DynamicMesh()
    
    actor_subsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
    actorsList = actor_subsystem.get_all_level_actors()
    geomesh_transform = unreal.GeometryScript_MeshTransforms
    actor:unreal.Actor
    for actor in actorsList:
        actorLabel = actor.get_actor_label()
        actorLocation = actor.get_actor_location()
        geomesh_transform.translate_pivot_to_location        

        input_actor = os.path.basename(os.path.normpath(input))

        if (actorLabel==input) or (actorLabel==input_actor):
            print(f'Actor label found')
            return actor
                
    return None

#===========================================================================
# * set_pivot_center_bottom()
# * 
# * Purpose: 
#===========================================================================                           
def set_pivot_center_bottom(asset_path:str):
   
    print('set_pivot_center_bottom() -- Start')            
    asset_options = unreal.GeometryScriptCopyMeshFromAssetOptions()
    requested_lod = unreal.GeometryScriptMeshReadLOD()
    
    input_sm:unreal.StaticMesh = ur_edit_asset_lib.load_asset(asset_path)
    
    dm = unreal.DynamicMesh()
    print(f'{unreal.GeometryScript_AssetUtils.copy_mesh_from_static_mesh(input_sm,dm,asset_options,requested_lod)}')
                
    mesh_bounds = input_sm.get_bounds()
    
    center = mesh_bounds.origin         # we need to keep this somewhere as we lose world positions when pivot is bottom center.
    extent = mesh_bounds.box_extent
    
    bottom_center = unreal.Vector(center.x, center.y, center.z )#- extent.z)    

    print(f'DynamicMesh = {dm}','[SPCB]')
    print(f'Location = {center}','[SPCB]')
    print(f'Extent = {extent}','[SPCB]')
    print(f'Bottom Center = {bottom_center}','[SPCB]')

    # move the pivot
    dm.translate_pivot_to_location(bottom_center)
    
    # move the object back to where it was.
    asset_outliner = find_asset_outliner(asset_path)    
    asset_outliner.set_actor_location(center,False,False)

    options = unreal.GeometryScriptCopyMeshToAssetOptions()

    target_lod = unreal.GeometryScriptMeshWriteLOD()
    unreal.GeometryScript_AssetUtils.copy_mesh_to_static_mesh(dm,input_sm,options,target_lod)
    #unreal.EditorAssetLibrary.save_asset(input_sm)
    print('set_pivot_center_bottom() -- End')
    
print('Pivot Fixer by T.J.Roughton')

# example usage

#set_pivot_center_bottom('/Game/Mesh/0x1C995_0_002')
#set_pivot_center_bottom('/Game/Mesh/0x1C995_1_002')
#set_pivot_center_bottom('/Game/Mesh/0x1C995_2_002')
Leave a Comment