问题
I would like to open a .blend file from a python script and parse it (get objects, animations and materials). The documentation I have read so far about how to do this from blender API (running the script as a blender add-on), but I would like to run this script from the command line without opening blender.
I appreciate all the help you can give me.
回答1:
I realized I don't need to open the binary blender file and parse it in order to use the objects. Blender has its own python installation, so I put a python script inside the folder path_to_blender/version/scripts/addons , I can execute it in the command line as follows:
blender.exe --background --python ./version/scripts/addons/superScript.py
Next, if you have a .blend file you want to read from your script, put it after the background parameter as follows:
blender.exe --background myFile.blend --python ./version/scripts/addons/superScript.py
And inside your python script do the following:
import bpy
import os
for ob in bpy.context.scene.objects:
print("object name: ", ob.data.name)
In this example I'm printing all the objects inside the scene in the .blend file
回答2:
As you answered, the easiest way is to use blender, there is also a script that is included with a blender install called blend_render_info.py that extracts the start and end frame settings without using the blender binary, I'm not sure how easy it would be to expand on that but it does work with python 2.x and 3.x
To get more detailed info there is an old project called blender-aid that was created to read and alter blend file data, someone recently had luck using it to extract blend file data using python 2.7 as documented in this question
来源:https://stackoverflow.com/questions/33447680/blender-open-and-parse-a-blend-file-from-python-script