6
$\begingroup$

I understand that running blender without a GUI does not provide bpy.context for the 3d View. Can anyone help me with a work around. This results in a 'Context' object has no attribute 'object' error.

for scene in bpy.data.scenes:
    for object in scene.objects:
        if bpy.context.mode == 'OBJECT':
            if object.name != 'Lamp':
                if object.name != 'Camera':
                    print("deleting " + object.name)
                    bpy.ops.object.delete(use_global=False)
                    bpy.ops.mesh.primitive_cone_add({'scene': scene, 'active_object': object})
                    print(bpy.context.object)
$\endgroup$
5
  • 1
    $\begingroup$ The script runs fine GUI-less. Use getattr and hasattr or catch and handle the exceptions. $\endgroup$
    – Aldrik
    Jul 21, 2013 at 3:50
  • $\begingroup$ Thanks for the tip. I thought I needed to link the object to the scene and make it active. $\endgroup$
    – Gordon
    Jul 21, 2013 at 4:58
  • $\begingroup$ @Aldrik it is not so immediately clear so I don't think it should be closed as such. Can you form your comment into an answer? Python related questions are allowed to an extent if kept simple and in relation to Blender somewhat. $\endgroup$
    – iKlsR
    Jul 22, 2013 at 13:35
  • $\begingroup$ Sorry my question was not clear. I am trying to understand blender python interface. How would I make python from terminal run import bpy bpy.ops.wm.quit_blender() without causing RuntimeError: Operator bpy.ops.wm.quit_blender.poll() failed, context is incorrect $\endgroup$
    – Gordon
    Jul 23, 2013 at 5:33
  • $\begingroup$ Note: the example loops over data its deleting, since it calls bpy.ops.object.delete it could be deleting many objects at once, suggest you use scene.objects.remove(obj) so you have better control, and make a copy of the objects list before looping over it. eg, for ob in scene.objects[:]: .... $\endgroup$
    – ideasman42
    Jul 23, 2013 at 5:57

1 Answer 1

4
$\begingroup$

Variables are not always available in the context, typically you can access them directly, in the case of the active object you can access it from scene.objects.active (most collections that have an active member work this way).

$\endgroup$
1
  • $\begingroup$ Thank you for providing a response which explains a tib bit of the python blender interface. I wish I could hire you to teach me a good foundation. $\endgroup$
    – Gordon
    Jul 24, 2013 at 2:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .