0
$\begingroup$

I keep running into a poll context error for when trying to parent_clear-

  File "c:\Users\Willard\Desktop\blender\2.82\scripts\modules\bpy\ops.py", line 199, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.object.parent_clear.poll() failed, context is incorrect

My script works fine and avoids poll errors when running blender with the interface.

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        for region in area.regions:
            if region.type == 'WINDOW':
                override = {'area': area, 'region': region, 'edit_object': bpy.context.edit_object}

for s in bpy.context.scene.objects:
    if s.type == 'MESH' and s.parent_type == 'OBJECT': 
         s.select_set(True)
         bpy.ops.object.parent_clear(override, type='CLEAR_KEEP_TRANSFORM')
         bpy.ops.object.select_all(action='DESELECT')
    else:
        pass


for o in bpy.context.scene.objects:
    if o.type == 'MESH':
         o.select_set(False)
    
    else:
        o.select_set(True)
        
       

bpy.ops.object.delete()

While running the script in background mode I receive the failed context error. I'm not sure how to override that context that I believe is failing because the script is running off the screen

$\endgroup$

1 Answer 1

1
$\begingroup$

Don't use the operator.

It is pretty simple to clear parent (set to None) and keep transform with simple API calls.

Python console code., clear parent on context object.

>>> mw = C.object.matrix_world.copy()
>>> C.object.parent = None
>>> C.object.matrix_world = mw
>>> 

Similarly, via API calls, to remove non meshes from scene

>>> non_meshes = [o for o in C.scene.objects if o.type != 'MESH']
>>> non_meshes
[bpy.data.objects['Empty'], bpy.data.objects['Camera'], bpy.data.objects['Sun']]

>>> while non_meshes:
...     D.objects.remove(non_meshes.pop())
...     
>>> non_meshes = [o for o in C.scene.objects if o.type != 'MESH']
>>> non_meshes
[]
$\endgroup$

You must log in to answer this question.

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