Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

I try to do this:

bpy.ops.object.select_all()
bpy.ops.object.join()

in my script. It selects all the stuff like CtrlA but fails on join() (CtrlJ) with the following error message:

Traceback (most recent call last): File "", line 1,
in File "C:\Program Files\Blender
Foundation\Blender\2.71\scripts\modules\bpy\ops.py", line 188, in
__ call __
ret = op_call(self.idname_py(), None, kw) RuntimeError: Operator bpy.ops.object.join.poll() failed, context is incorrect

the message is from console.

How can I make this work? CtrlA, CtrlJ actually works.

share|improve this question
1  
TO join an object you must have one object as the active object. If there is no active object selected it will not work. Try setting one object as the selected object. –  Vader Jul 12 at 13:07
    
Works nice, thank you. Would you put this as an answer so I can flag it? –  Zéiksz Jul 12 at 13:15

3 Answers 3

up vote 3 down vote accepted

To join an object you must have one object as the active object. If there is no active object selected it will not work. Try setting one object as the selected object.

An example would be

bpy.context.scene.objects.active = bpy.data.objects["Cube"]

share|improve this answer

If you don't want to change the real selection states, you may wanna use an override:

import bpy

scene = bpy.context.scene

obs = []
for ob in scene.objects:
    # whatever objects you want to join...
    if ob.type == 'MESH':
        obs.append(ob)

ctx = bpy.context.copy()

# one of the objects to join
ctx['active_object'] = obs[0]

ctx['selected_objects'] = obs

# we need the scene bases as well for joining
ctx['selected_editable_bases'] = [scene.object_bases[ob.name] for ob in obs]

bpy.ops.object.join(ctx)
share|improve this answer

A full script for later reference:

import bpy

for ob in bpy.context.scene.objects:
    if ob.type == 'MESH':
        ob.select = True
        bpy.context.scene.objects.active = ob
    else:
        ob.select = False
bpy.ops.object.join()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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