1
$\begingroup$

I have the following blender python script:

import bpy
from random import randint

#how many cubes you want to add
count = 10

for c in range(0,count):
    x = randint(-10,10)
    y = randint(-10,10)
    z = randint(-10,10)
    bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
    print(f'Cube added at coordinates: {x}{y}{z}')
    
bpy.ops.export_mesh.stl(filepath="C://Users//diego//OneDrive//CFD//output.stl")

And it works perfectly when called by blender --python script.py but fails when called by blender -b --python script.py, I get the following error:

Error: Object does not have geometry data
Error: Object does not have geometry data

Blender quit
$\endgroup$
1
$\begingroup$

This does not happen if you don't have the default cube/light/camera in your startup file (I don't know why). So putting this at the start of your script to remove all objects

while bpy.data.objects:
    bpy.data.objects.remove(bpy.data.objects[0], do_unlink=True)

appears to fix it.

$\endgroup$
1
  • $\begingroup$ It worked, thanks! It makes sense that those were the two objects without geometry, but the fact that it worked with the ui really misled me $\endgroup$ – dgeneratioN Apr 19 at 4:28
1
$\begingroup$

Quoting from https://docs.blender.org/api/242PythonDoc/API_related-module.html :

Background mode examples:
# Open Blender in background mode with file 'myfile.blend'
# and run the script 'script.py':

blender -b myfile.blend -P script.py

# Note: a .blend file is always required.  'script.py' can be a file
# in the file system or a Blender Text stored in 'myfile.blend'.

I have tested it but you can try adding a file in your command line options

$\endgroup$
2
  • $\begingroup$ Those docs are from 2.42, released in 2006. A .blend is definitely not required in modern Blender. But it doesn't hurt to try I guess... $\endgroup$ – scurest Apr 18 at 21:42
  • $\begingroup$ I've tried using a .blend file (saved the default cube scene, and even deleting the cube) but unfortunately it didn't seem to matter $\endgroup$ – dgeneratioN Apr 18 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.