Ozeki Posted January 24, 2017 Share Posted January 24, 2017 Theory I recently wanted to try and use/adapt the official Blender exporter addon for Babylon to allow for parsing a directory of source models (FBX) and have Blender batch export them to the Babylon Json format. I know that there is a C++ CLI tool to do exactly this but (correct me if I'm wrong) it seems like it will only run on Windows (no OSX or Linux). So I wanted to be add this functionality via Python scripting in Blender to be able to do this no matter the platform. Disclaimer, Python is not my native language. I taken some inspiration from a few existing scripts and adapted a simple one that : Looks for files ending with FBX in the input path. Imports them (one by one) via the Blender FBX operator : bpy.ops.import_scene.fbx(filepath=file) Exports them (one by one) via the BabylonJS operator : bpy.ops.bjs.main(filepath=file) Problem Sadly here I am getting an error. Perhaps I'm being naive here by thinking that I can access a non-standard issue addon via scripting ? That is what the error seems to indicate at least : Traceback (most recent call last): File "/BatchConvert.py", line 54, in <module> File "/BatchConvert.py", line 44, in convert_recursive File "/Users/ozeki/Library/Application Support/Steam/steamapps/common/Blender/blender.app/Contents/MacOS/../Resources/2.78/scripts/modules/bpy/ops.py", line 189, in __call__ ret = op_call(self.idname_py(), None, kw) AttributeError: Calling operator "bpy.ops.bjs.main" error, could not be found location: <unknown location>:-1 Solution ? Is this doable with the way the Babylon Blender addon is setup and if so what am I doing wrong? I have attached my current code to this post. I've also added a list of references for the scripts that I have looked at or adapted from originally. References https://github.com/BabylonJS/Babylon.js/blob/master/Exporters/Blender/io_export_babylon.py#L202 https://github.com/BabylonJS/Babylon.js/blob/master/Exporters/Blender/src/babylon-js/__init__.py#L66 http://blender.stackexchange.com/a/47000 http://blender.stackexchange.com/a/34539 https://github.com/pjezek/blender/blob/master/unity_tools/__init__.py https://github.com/RH2/TD_B3D-UDK/blob/master/main.py#L152 BatchConvert.py HeadClot 1 Quote Link to comment Share on other sites More sharing options...
JCPalmer Posted January 24, 2017 Share Posted January 24, 2017 This has no been tried up till now. I am not hopeful. First, I assume exporter is permanently saved as enabled. I also tried calling a MakeHuman add-in from my code. I remember having to add it onto my "path". I do not do it anymore, but I remembered on the MakeHuman forum I pasted code which did. You will have to convert this for us: # add import_runtime_mhx2 to sys.path, so can be imported mhxOnPath = False def putMhxOnPath(): global mhxOnPath if not mhxOnPath: import os import sys addon_paths = bpy.utils.script_paths('addons') for path in addon_paths: lib_path = os.path.join(path, 'import_runtime_mhx2') if os.path.exists(lib_path): print('mhx2 found @ ' + lib_path) sys.path.append(lib_path) mhxOnPath = True break return mhxOnPath Finally, the exporter will bring up UI, something called exporthelper. Do not know if you can set filepath like that. HeadClot and Ozeki 2 Quote Link to comment Share on other sites More sharing options...
Ozeki Posted January 25, 2017 Author Share Posted January 25, 2017 Hey again, Thanks for your input, the path was a good clue to keep me going and helped me solve the problem. I was able to make this work with the 4.6.1 version of the Babylon Export as it exposes the bpy.ops.scene.babylon Blender Operator when you import io_export_babylon of that version of the Exporter. I might try to make this work with the newer version later on but I don't have a lot of time right now. I'm willing to share my findings but fair warning, this hasn't been thoroughly tested so use it at your own risks. That being said when we solve a problem in an open-source community it is the least we can do to give something back so here is my contribution. Blender version and usage information is available in the file's header. One could easily adapt this script to also have an OUTPUT_DIR next to the CONVERT_DIR in order to allow for outputting the files elsewhere than in the same folder but it did not bother me for my workflow. I believe one could also adapt this for use with almost any other two formats compatible with Blender. Either way cheers and thank you again! Blender - Batch Export from Fbx to BabylonJShttps://gist.github.com/osmanzeki/13ad20cca2bbffa7a600a3648888da83 HeadClot and JCPalmer 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.