Export OBJ script

Hi all,

So…I’ve got a high poly file that I need to export. Trouble is, my 16GB of ram isn’t enough apparently.
I know you might ask why I have such a high poly model - simple answer is: it’s a detail sculpting pass for a project I’m working on.

But anyway…I can’t export the file as an OBJ without blender crashing due to running out of RAM. I need to export the file to bake normals in xNormal; blender I find isn’t great with baking normals, hence the external program.

So what I’d like to do, which I think would work, is export the file without using the Blender UI via the command line, and/or python script. I have, however, no idea how to do this.

I have searched a fair amount online (which is why I assume it’s possible), and found several scripts, which I can’t get to work, basically because I lack the basic understandings of Python (I assume that’s why).

Can anyone help me with this?

Pretty low-tech solution to this, since OBJ is text-based and does not contain explicit indices, you could just export in separate manageable pieces and then combine everything together in a text editor?

Maybe it would be better to split the mesh in smaller parts and either load them in xNormal together or bake the parts individually? Reducing the memory footprint of the problem will be preferable even though it might be the .obj exporter that is poorly optimized. The solution is pretty much what LDAsh suggested, but merging sounds unnecessary and should be avoided as it will get messy without a script.

I’ve tried splitting the model, and although it does work, it leaves seams when baking the normals. I can’t apply the multires and then split it either, as blender just closes. That’s why I’m hoping there is someway to do it without having to actually physically load the model, therefore saving the 5 GB or ram it uses just to load it into Blender…I can of course lower the multires by a level, but I lose a lot of sharpness of the sculpting.

Ok, i’ve found a simple script for you to use from:

I’ve made a simple python file for you to download (see steps below):

export_obj.py (248 Bytes)

!!! Warning the following steps are for Windows, other operating system may work differently, parameters to blender should be the same.

So to get to the problem, running from terminal is not complicated but is not straight forward. I’ll try to explain steps in two different solutions:

If blender is available as a console command (open console and just type blender, if blender opens this should work, for me it’s not available):

  1. Download the script i linked above and put it in the same directory as the .blend file that you are trying to export.

  2. Open console in the file directory (the file is the .blend to be exported). If you know how to do this, great, orthewise for windows 7+ (google it for other OS) this can be done by:

    [left-click] then [hold-shift, right-click] and select the console option in the drop down something like ‘open command-window’ or ‘Open PowerShell window here’. (Make sure to not click any files and click on empty space in the file window)

  3. Try opening the file from the console by typing (make sure it opens otherwise look at option 2):
    blender .\<file>.blend

  4. When the blender file opens, close it, then type (note that entire <file> statement should be replaced with the file name):
    blender -b .\<file>.blend --python .\export_obj.py

  5. Pray that you have enough RAM, i beleive this would be a miracle if it works without still overflowing RAM!

  6. This exports the entire scene as an .obj to a file called ‘export.obj’, which may not be exactly what you want, but it’s a step in the right direction.

Option 2:
If typing blender in console doesn’t work step 4 needs to be replaced with (and a similar replacement need to be done for redundant test step 3):

<directory_to_blender_exe>\blender.exe -b .\<file>.blend --python .\export_obj.py

where <directory_to_blender_exe> is either a relative or absolute directory path to your blender executable (http://desktop.arcgis.com/en/arcmap/10.3/tools/supplement/pathnames-explained-absolute-relative-unc-and-url.htm might help to explain)

Also, for reference and info, from the manual:

Command line intro
Rendering on the command line
Command line arguments

OBJ export

Ah! Computer says no!

Thanks anyway. I did come across that code, but wasn’t sure how to use it. You’re instructions got my head around it. But alas, it still says no. Oh well…At-last I’ve learned something new. :smiley:

Many thanks!

Yeah i was concerned it wouldn’t really help, it only saved me ~200 MB when i did a simple test (blender is still running normally it’s just reducing some overhead related to the UI). I think you might have painted yourself into a corner, the easy solution is just to find a system with more RAM that can handle the file. There might exist a great solution for the problem, but it will save everyone time if the easy solution is available (and there is a catch in trying to to solve it, if i have enough RAM it’s might no longer be a problem!). I think it’s a good lesson to be carefull to not exceed system limitations, at least for the next project :wink:

Why not export with other formats then convert to .obj in an external program? Some of the exporters are way more efficient when it comes to memory management. The issue with the obj exporter is that it is pure python.

Also paste the error output of the export process if you can. The issue might not be just memory issue.

I don’t think the problem is related to .obj file export, as he mentions he couldn’t create the highly detailed mesh from the multi-res modifier, i have assumed (perhaps wrongly, or i misunderstood what he ment) that Blender is unable to do so due to RAM. If it’s possible to attain more information from that process, it would be most helpful. But if OP has access to a system with more RAM testing exporting it there might straight up solve the problem. If the problem persists after that, i’m unsure how to solve it properly without getting hold of the file.

That is why I recommend other exporters like Fbx or Alembic, they can convert that data into mesh during export more efficiently. In fact running the same Python obj exporter in the command line wont make much of a difference if the raw mesh data is the issue.

Oh, i was under the assumption they use the same functions (c++ or otherwise) to apply modifiers first, creating raw primitives that can be exported (since modifiers are not straight forward to to import/export). So if the modifier can’t be applied within RAM limits, no exporters would solve it. But it is definitively a possibility, especially if they can export modifiers as is.

A few months back, I did send the file to someone with 32GB of RAM (As opposed to my 16GB) and they were able to successfully export. So it is a confirmed RAM issue. Also, on the occasion when Blender doesn’t simply crap the bed and close, it goes back to the 3D view, and says that it ran out of memory.

It was like a 2-3gb file though, so uploading and sending the file back was a bit awkward and time consuming.

A few months back, I did try saving as an stl, and that was successful. Don’t think I tried fbx or Alembic, but i’ll be willing to give it a go. How does one go about converting a 3D file into a different file type?

Obj exporter still makes calls to certain depsgraph functions to convert the mesh via the API but how that is handled might not be as efficient as the other exporters,

I know the obj exporter needs to be implemented natively. The ui and everything will be in Python but it can be rewritten in C++ to manage memory better, also it will be much faster. Obj exporter is quite slow atm.

@Magnavis you can use Meshlab to convert meshes but I never tried it with fbx myself.

There are several open source applications, that could do the conversion job. One app, that immediately comes to my mind is Meshlab (http://www.meshlab.net/). This application is quite optimized for handling large meshes like 3d scans.
Another thing that comes to my mind is AssImp, the open asset import library. It’s mainly a programming library, but they could have an example program, that’s converting from one format to another, but I haven’t checked that. You can find it here: http://www.assimp.org/

Usually back in the days when working with big models we had to split models with overlaps, considering the UV seams as well. Doesn’t really solve the issue of exporting it as one, but would take care of the baking artifacts.