1
$\begingroup$

I found a lot of solutions for deleting unused data. Most of them are not practical for my case.
I have a very long script and during this, I want to purge all the unused data from the memory.

I'm trying to run this script but I keep getting errors:

import bpy
bpy.types.SpaceOutliner.display_mode = "ORPHAN_DATA"
bpy.ops.outliner.orphans_purge()

The error:

RuntimeError: Operator bpy.ops.outliner.orphans_purge.poll() failed, context is incorrect

Any suggestions?
Note: I can't reload blender; it's a long and continuous operation and can't be interrupted and if I ignored purging data the memory will leak.

$\endgroup$
3
  • $\begingroup$ The problem indeed is about accessing the right UI in blender. How we can access a certain menu in the UI? $\endgroup$ Commented Dec 19, 2018 at 11:25
  • 1
    $\begingroup$ I believe what you are after is to override the context. blender.stackexchange.com/questions/15118/… $\endgroup$
    – morph3us
    Commented Dec 19, 2018 at 12:43
  • $\begingroup$ I tried this: bpy.context.area.type = 'OUTLINER' bpy.context.space_data.display_mode = 'ORPHAN_DATA' bpy.ops.outliner.orphans_purge() bpy.context.area.type = 'TEXT_EDITOR' But still I have error right now that the type doesn't exist! so can I specify a certain area of UI other than the current one (context)? $\endgroup$ Commented Dec 19, 2018 at 13:19

2 Answers 2

1
$\begingroup$
override = bpy.context.copy()
override["area.type"] = ['OUTLINER']
override["display_mode"] = ['ORPHAN_DATA']
bpy.ops.outliner.orphans_purge(override) 

you can call every operator with a dictionary in its call in first place (if there are more than one attributes to pass). this dictionary holds the information you want to override from the normal context. you dont have to name the override like this, any other name will do, too.

the thing is, i cannot help you a hundred percent, because I dont really know which parts of the context you have to overwrite. at least in 2.8, "area.type" and "display_mode" are not part of the original context, so you are trying to override something which isnt there, so the context still stays the same like without overriding at all.

i also dont know what you do have to overwrite, because right now i really dont have time to figure that out, sry, I am myself only learning all of this. you can however see whats part of the original context if you type following into the python console:

override = bpy.context.copy()
override

it will list you everything that is in the original context, maybe you find out what you want to overwrite.

$\endgroup$
3
  • $\begingroup$ I'm not really expert in Blender's python.. but this should work.. The same error still appears but I'll try to fix it up.. if u can refine it I will be really thankful. $\endgroup$ Commented Dec 19, 2018 at 14:28
  • $\begingroup$ I just edited my comment, hope it helps! $\endgroup$
    – morph3us
    Commented Dec 19, 2018 at 17:04
  • $\begingroup$ It's not working at all I've tried to change it and fit it but seems useless in my case.. :( $\endgroup$ Commented Jan 3, 2019 at 11:59
0
$\begingroup$
def purge() :
  # print(C.area.spaces[0])
  type_orig, C.area.type = C.area.type, 'OUTLINER'
  # print(C.area.type)
  # print(C.area.spaces[0])
  C.area.spaces[0].display_mode = 'ORPHAN_DATA'
  Op.outliner.orphans_purge()
  C.area.type = type_orig

This is the closest I could come up with, considering that I could not find a way to create an Area instance using blender python. So I modify the current context and restore it to the previous value.

$\endgroup$

You must log in to answer this question.

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