Announcement

Collapse
No announcement yet.

Replace Material Python Script??

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Replace Material Python Script??

    I'm trying to re-create the script that's seen in this video at the 0:57 mark, but not having any luck. Nothing seems to happen and I figure I might have the syntax incorrect.

    Any suggestions?

    https://www.youtube.com/watch?v=sj53pLUdLyk

    import unreal

    def replace_material(original, replacement):
    original_asset =
    unreal.EditorAssetLibrary.load_asset(original)
    replacement_asset =
    unreal.EditorAssetLibrary.load_asset(replacement)
    unreal.EditorAssetLibrary.consolidate_assets(replacement_asset, [original_asset])

    #replace_material(original_name, replacement_name)
    replace_material("/Materials/M_01", "/Materials/B_001")

    #2
    Hello again,

    You are right, there is a little problem in your syntax: the paths should start with '/Game'

    So, if your asset location is: Content/Materials/M_01
    The right path to use is: /Game/Materials/M_01


    + Additional info:
    The consolidate_assets function does not really ''Replace Materials", but replaces the references. So everything that uses the original material will now use the new one.
    https://api.unrealengine.com/INT/Pyt...olidate_assets

    If it is not what you are looking for, you can take a look at the set_material function to only set a material to a specific mesh asset.
    https://api.unrealengine.com/INT/Pyt...h.set_material
    Have An Awesome And Productive Day
    Alex, Youtube Tutorials

    Comment


      #3
      Hey, thanks Alex. The script still isn't working even after I corrected those paths. Wondering if maybe I've got some other syntax problem? It was hard to know if there were spaces after some words or not in the original video...

      Here's the new lines:

      #replace_material(original_name, replacement_name)
      replace_material("/Game/Materials/M_01", "/Game/Materials/B_001"

      Attaching a screenshot of the folder structure. The platform in the sceen as the M_01 white material.



      Comment


        #4
        Oh ok, now I think I understand what you did, here is the right indentation.
        I though the indents where wrong because of the copy paste from your code to here ^^'

        Code:
        import unreal
        
        def replace_material(original, replacement):
            original_asset = unreal.EditorAssetLibrary.load_asset(original)
            replacement_asset = unreal.EditorAssetLibrary.load_asset(replacement)
            unreal.EditorAssetLibrary.consolidate_assets(replacement_asset, [original_asset])
        
        replace_material("/Game/Materials/M_01", "/Game/Materials/B_001")
        Have An Awesome And Productive Day
        Alex, Youtube Tutorials

        Comment


          #5
          Okay, I got it working...I think! It might be a case of i'm just too new at this to understand what's going on but once I run the script the original material (M_01) vanishes from the Content Browser. When I go to exit/open a new level/etc I get a message that says 'the M_01 asset failed to save (because it thinks it's missing?). If I just click on 'continue', the M_01 material asset magically reappears in the Content Browser/Materials folder! Well, it appears only if I go back one folder and then open up materials. In Windows Explorer, the M_01 material is still there so this must be some kind of odd Content Browser thing?

          Wondering if this is acceptable/normal behavior?

          Tnx again Alex for your help on this. Much appreciated.
          Last edited by landrvr1; 12-16-2018, 06:23 PM.

          Comment


            #6
            Yeah, it's a good start! :P
            Don't worry, you will understand more and more every day.

            For the failed to save message, I don't think it's the expected behavior and I don't know why it happens.
            (But I suspect the Redirectors)

            If your assets are now all using the new material and the old one is not used anymore, I suggest to:
            - Ignore the message
            - Save everything
            - Restart Unreal
            - Fix the redirectors

            Personnaly, I'm not a fan of functions like consolidate_assets, because the engine does a lot of things and I'm loosing a little bit too much control on what happens. I prefer to do every steps myself. (Assign new material, double check that nothing is still using the old material, then delete it)
            Have An Awesome And Productive Day
            Alex, Youtube Tutorials

            Comment


              #7
              Alex, I went through your steps and the interesting thing is that after a restart of Unreal, the material magically reappears and no redirectors were left to fix (though one was certainly there after I did the material swap). This appears to work the same as my initial workflow of:

              1. Run the script.
              2. The material that you are replacing will seem to vanish from the Content Browser, but don’t worry. This is a glitch.
              3. Go to File/Open Level then select the same Level you’ve been working on.
              4. Do a Save Selected, with all items checked as content to save.
              5. You will get a message that says ‘______________ failed to save’.
              6. Press the Continue button.
              7. You will get a notice that the ‘packages failed to save’. Press Okay.
              8. The Level will reopen. If you are in the same folder as the original material, get out of that folder and then back in....the material will reappear!

              Hopefully that little bug will be taken care of in the future (where should I go to log that? I believe Python is still beta, correct?). I'm going to circle back to that original thread and post a quick follow-up. Tnx much, Alex!


              Comment

              Working...
              X