Page MenuHome

bpy.ops.wm.save_as_mainfile does not work as expected on MacOS when run from script
Open, Waiting for Developer to ReproducePublic

Description

System Information
Operating system: MacOS

Blender Version
Blender 2.8 Candidate Release (didn't try 2.79 but I suspect the same problem)

Short description of error
On MacOS the operator bpy.ops.wm.save_as_mainfile(copy=True, filepath=path, relative_remap=True, compress=True) saves the current blend scene as a copy but does find external assets when run from a script however saving through the interface using "File > Save Copy... > <same output location as script>" does work as intended (I was under the impression that the interface is just running this operator under the hood which is why I am surprised to see different results)

Exact steps for others to reproduce the error

  • open a new unsaved blender file
  • add a texture to the default cube to act as an external asset
  • run the following script from blenders text editor
import bpy

# feel free to change path (I am saving to a temporary directory)
path='/var/folders/94/tdrqyvln2_bfkdxctp3ktdr40000gn/T/gm-temp-dir-6fzcf_08/file_saved_through_script.blend'

bpy.ops.wm.save_as_mainfile(copy=True, filepath=path, relative_remap=True, compress=True)

You should see a similar messages in the system console:

Info: Total files 1 | Changed 1 | Failed 0`
Path /private/Users/<user name>/Pictures/test_texture.jpg does not exist for <BlockUsage name=b'IMartsoc5.jpg' type='Image' field='name[1024]' asset=BlendPath(b'//../../../../../../Users/<user name>/Pictures/test_texture.jpg')>

However if you save a copy through the interface you just get:

Info: Total files 1 | Changed 1 | Failed 0

(no weird does not exist error)

Note - I have tested on Windows and Linux and it works as expected in both these environments. I encountered this issue as I am trying to write a blender add-on that requires packing the current blender scene before upload. I am using Blender Asset Tracer (BAT) to do this and it works well. I save a copy of the current scene to a temporary blend file and pack that file, however on MacOS this packed file never contains external assets and it appears to be because bpy.ops.wm.save_as_mainfile is not working correctly.

Details

Type
Bug

Event Timeline

Philipp Oeser (lichtwerk) triaged this task as Needs Information from User priority.

Still unsure if this is related to BAT or plain blender?

That message you see is from BAT...

If you dont use BAT but just run the script you posted, if you then open the file just saved through the script, are the images showing up? (so the paths are still correct?)
If this is the case, then we might have to dig in BAT instead, no?

btw., T62612 might be related... (still not sure though why these would actually report as "not existing"...)

Still unsure if this is related to BAT or plain blender?
That message you see is from BAT...
If you dont use BAT but just run the script you posted, if you then open the file just saved through the script, are the images showing up? (so the paths are still correct?)
If this is the case, then we might have to dig in BAT instead, no?

Sorry I must have been running BAT trace after saving, however I still believe the issue is plain blender.

I just tried running the script with a variety of save locations (No BAT) and here's the result:

  • Saved to /var/folders/94/tdrqyvln2_bfkdxctp3ktdr40000gn/T/file_saved_through_script.blend and when opened in blender the cube was missing it's texture (showed as pink)
  • Saved to /var/folders/94/tdrqyvln2_bfkdxctp3ktdr40000gn/T/gm-temp-dir-6fzcf_08/file_saved_through_script.blend (pink cube again)
  • Saved to /tmp/file_saved_through_script.blend (pink cube again)
  • Saved to /Applications/file_saved_through_script.blend This time the cube had it's texture
  • Saved to /Users/<user name>/file_saved_through_script.blend cube had it's texture
  • Saved to /Users/<user name>/Desktop/file_saved_through_script.blend cube had it's texture

The cube texture was located on the desktop so it looks to me like the paths are correctly re-mapped so long as your saving the file to a location with the same root as the texture. I'm guessing the /Applications folder maps to the /Users/<user name>/Applications folder? I'm not used to MacOS so I may be wrong on that.

I need to be able to save the current blender scene (that may contain relative and absolute paths) as a copy to to some arbitary location in a way that BAT will then be able to pack that copy for distribution.

Demeter Dzadik (Mets) raised the priority of this task from Needs Information from User to Waiting for Developer to Reproduce.

Looks like this no longer needs information.

I tested it but... Blender actually just closed for me when running the script, with no message in the console. Is that... what's supposed to happen? It also does not save a file in the path I specified.

Win10 v1607, GTX 1070, rB79ce2054d436

@Demeter Dzadik (Mets) - Blender shouldn't close, how are you running the script and which line of the script is causing blender to close for you? I was just using blender's internal text editor to run it btw, I didn't try running the script as a command line argument or anything.

Welp, today it works fine... Same build, same file, same path. I don't know why it was doing that yesterday... Oh well, I guess what matters is I can't reproduce.

Windoze, mentioned in my first comment.

@Demeter Dzadik (Mets) Ah I see, I only get the bug on Mac so that makes sense.