Skip to content

Instantly share code, notes, and snippets.

@rondreas
Created February 14, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondreas/d251aeee36ff1b371d747359433005e6 to your computer and use it in GitHub Desktop.
Save rondreas/d251aeee36ff1b371d747359433005e6 to your computer and use it in GitHub Desktop.
import unreal
@unreal.uclass()
class OnAssetPostImportAction(unreal.EditorUtilityObject):
@unreal.ufunction(override=True)
def run(self):
""" Add a callable static function to be called whenever an asset is
imported.
"""
# Get the Import Subsystem,
import_subsytem = unreal.get_editor_subsystem(unreal.ImportSubsystem)
# Add a callable to the OnAssetPostImport delegate,
import_subsytem.on_asset_post_import.add_callable(
OnAssetPostImportAction.log_import_name
)
@unreal.ufunction(static=True, params=[unreal.Factory, unreal.Object], meta=dict(BlueprintEvent=True))
def log_import_name(factory, created_object):
""" Static function that should print asset name whenever a new asset is
imported.
:param factory: the factory class used for import
:type factory: unreal.Factory
:param created_object: the imported asset,
:type created_object: unreal.Object
"""
print("Hello %s" % created_object.get_name())
def main():
# Execute the run function for our Editor Utility Object,
OnAssetPostImportAction().run()
if __name__ == '__main__':
main()
@RyanHz2
Copy link

RyanHz2 commented Jun 2, 2021

Hello,rondreas.i get some help with your pythoncode.thank you.
But now i have one question.
When i import an asset ,it will log a "created_object.get_name()",
but i delete it and import again ,it will not log "created_object.get_name()",import others also
Hope you can solve mt question,thank you very much.
(i think it is the problem of "on_asset_post_import&on_asset_pre_import&on_asset_reimport"?)

@rondreas
Copy link
Author

rondreas commented Jun 2, 2021

This snippet will not survive the garbage collector, guessing Unreal did a cleanup after you deleted the uasset.

@RyanHz2
Copy link

RyanHz2 commented Jun 2, 2021

This snippet will not survive the garbage collector, guessing Unreal did a cleanup after you deleted the uasset.

but i delete one asset and import another asset,it will not log something,
and it will work when i restart unreal.
how can i solve it ,thank you a lot.

@rondreas
Copy link
Author

rondreas commented Jun 2, 2021

It's the garbage collector. Running gc.CollectGarbageEveryFrame 1 then gc.CollectGarbageEveryFrame 0 will have same result.

Not sure why it doesn't work after re-running the script - was sure I could do that back in february at least.

@RyanHz2
Copy link

RyanHz2 commented Jun 3, 2021

Thanks for your suggestion,but it do not work.
Maybe it is my faulty.

import unreal
import gc
@unreal.uclass()
class OnAssetPostImportAction(unreal.EditorUtilityObject):
    @unreal.ufunction(override=True)
    def run(self):

        import_subsystem = unreal.get_editor_subsystem(unreal.ImportSubsystem)
        import_subsystem.on_asset_post_import.add_callable(OnAssetPostImportAction.log_import_name)
    @unreal.ufunction(static=True, params=[unreal.Factory, unreal.Object], meta=dict(BlueprintEvent=True))
    def log_import_name(factory, created_object):
        print("Hello %s" % created_object.get_path_name())

        gc.CollectGarbagEveryFrame = 0

@rondreas
Copy link
Author

rondreas commented Jun 3, 2021

Sorry, I was just saying the problem is due to the garbage collector. Not proposing a solution. gc.CollectGarbageEveryFrame is a command you run in the editor not a module in unreal python.

Don't think the python module gc can do anything to save the script to survive unreals garbage collector. Been told you'd need to add the object to root, unreal documentation but this isn't exposed to blueprints/python so you would have to expose it in C++

@RyanHz2
Copy link

RyanHz2 commented Jun 3, 2021

hahahahhah,ok.it's my fault to misunderstand what your talk.
i may get it.Thank you to answer my question,Have a nice day.
(my first time to say somthing in git , amazing and learn something from your git, Thank you. (^ .^) )

@rondreas
Copy link
Author

rondreas commented Jun 4, 2021

Ok so it seems if you run this in your startup they will persist, pretty much as the documentation promised for startup objects

import unreal
@unreal.uclass()
class OnAssetImports(unreal.EditorUtilityObject):
    @unreal.ufunction(static=True, params=[unreal.Factory, unreal.Object], meta=dict(BlueprintEvent=True))
    def log_import_name(factory, created_object):
        print("Hello %s" % created_object.get_name())

import_subsytem = unreal.get_editor_subsystem(unreal.ImportSubsystem)
import_subsytem.on_asset_post_import.add_callable(
    OnAssetImports.log_import_name
)

saving this to init_unreal.py inside your Content/Python folder should work

@RyanHz2
Copy link

RyanHz2 commented Jun 7, 2021

Oh,myGodness.Thank you sosososososososo much. it solve my “Headache” problem,
but if i can , can i know why it can run . it look just reverve the code , but it make me learn a lot , Thank you
(Sorry for reply your message after two days , beacause i go another place without computer , Sorry)
Ps. i am your fans now , hahah

@RyanHz2
Copy link

RyanHz2 commented Jul 1, 2021

hello, Mr.rondreas. during this time, i learn and do something unreal plugins with python in my relaxlife, is so wonderfull.
but now, i have a question : some python codes start by our clicked and will run for a longtime, but now i dont want it run a long time , iwant to stop it by my exe , as i want to start/stop python code by myself.
is it can do ? Do you have some suggestions?
Thank you for your help , and sorry to occupy your time.

@rondreas
Copy link
Author

rondreas commented Jul 1, 2021

Not really the forum for this, but try looking at scoped slow tasks

@RyanHz2
Copy link

RyanHz2 commented Jul 1, 2021

i know the scoped slow tasks, but i mean
like this forum "StartupObjects/index.html" what we talk ,
if it run ,it will run a long time to "print("Hello %s" % created_object.get_name())" when i import some assets
but with UE running, i want it stop, stop this code running, back to the time i can import assets without this code by my thought
it is the question i think a lot ,when i keep develop this forum.
Sorry to occupy your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment