I am totally a newbie to Blender and python scripting in Blender.
What I am trying to do is write a script for one of my models in the Crate.blend file in the scene to extract its vertices and face information and printing it to the console
I use the Blender text editor to write a simple script like below and run the script. This runs fine on the console
import bpy
for item in bpy.data.objects:
if item.name == 'Crate':
if item.type == 'MESH':
for face in item.data.polygons:
verts_in_face = face.vertices[:]
print("face index ", face.index)
print("normal ", face.normal)
for vert in verts_in_face:
print("vertex coords ", item.data.vertices[vert].co)
What I want is to execute this script when I type the following line in the console
blender --background crate.blend --python export-model.py -- Crate
How do I go about achieving this? I think I might need to create an exporter script for the same? I found a promising post for creating an exporter script here
I will paste the script just in case
# Required Blender information.
bl_info = {
"name": "My Exporter",
"author": "",
"version": (1, 0),
"blender": (2, 65, 0),
"location": "File > Export > Test (.tst)",
"description": "",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Import-Export"
}
# Import the Blender required namespaces.
import sys, getopt
import bpy
from bpy_extras.io_utils import ExportHelper
# The main exporter class.
class MyExporter(bpy.types.Operator, ExportHelper):
bl_idname = "export_scene.my_exporter";
bl_label = "My Exporter";
bl_options = {'PRESET'};
filename_ext = ".tst";
object_count = 0;
def __init__(self):
pass
def execute(self, context):
print("Execute was called.");
self.parse_command_line_options();
if (self.filepath == ""):
print("No sutable filename was provided to save to.");
return {'FINISHED'};
# Get all the mesh objects in the scene.
objList = [object for object in bpy.context.scene.objects if object.type == 'MESH'];
# Now process all the objects that we found.
for gameObject in objList:
self.export_object(gameObject);
# Parse all the objects in the scene.
return {'FINISHED'};
def export_object(self, gameObject):
if (gameObject.type != "MESH"):
print("Object was not of type mesh.");
else:
self.object_count += 1;
return;
def parse_command_line_options(self):
modelFile = "";
myArgs = [];
argsStartPos = 0;
if (("--" in sys.argv) == False):
return;
argsStartPos = sys.argv.index("--");
argsStartPos += 1;
myArgs = sys.argv[argsStartPos:];
try:
opts, args = getopt.getopt(myArgs, 'hm:', ["help", "model-file="]);
except getOpt.GetoptError:
print("Opt Error.");
return;
for opt, arg in opts:
if (opt in ("-h", "--help")):
print("Run this as the following blender command.");
print("\tblender <blend file> --background --python <script file> -- -m <output file>");
elif (opt in ("-m", "--model-file")):
modelFile = arg;
if (modelFile != ""):
self.filepath = modelFile;
# Define a function to create the menu option for exporting.
def create_menu(self, context):
self.layout.operator(MyExporter.bl_idname,text="test (.tst)");
# Define the Blender required registration functions.
def register():
"""
Handles the registration of the Blender Addon.
"""
bpy.utils.register_module(__name__);
bpy.types.INFO_MT_file_export.append(create_menu);
def unregister():
"""
Handles the unregistering of this Blender Addon.
"""
bpy.utils.unregister_module(__name__);
bpy.types.INFO_MT_file_export.remove(create_menu);
# Handle running the script from Blender's text editor.
if (__name__ == "__main__"):
print("Registering.");
register();
print("Executing.");
bpy.ops.export_scene.my_exporter();
I run this script inside Blender text editor and it compiles
Now when I try to run it on the command line with the below command
blender model.blend --background --python myexporter.py
it thrown exception on model.blend as "invalid syntax"
I am completely new to Blender and scripting with python so spare me if I am missing something here.
Thanks
After changing my script as suggested in the second post, I get the following error
My blender file name is "model.blend" and it throws the error as invalid syntax. What am I missing?
blender model.blend --back...
from your system console/terminal. $\endgroup$