0
$\begingroup$

I'm doing a scritp with several functions. All of them have been tested and work fine if you execute them from a py file. The problem is when I put them on an operator and call them when pressing the button they stop working. It seems as if blender is not able to get the current active object to do whatever I'm telling it to do. Because is work related I can't really share the code but one of the parts where it crashes looks like:

ob=bpy.context.active_object
for i in range(0,len(bpy.context.object.data.polygons)):
        ob.data.polygons[1].select=True
        bpy.ops.object.mode_set(mode='EDIT')

The loop is longer, but the error comes up in that line. Menawhile, executing the same function from a py file works fine.

The error is

AttribueError: 'NoneType' object has no attribute 'data

Any idea?

$\endgroup$
2
  • $\begingroup$ What are you trying to achieve? $\endgroup$
    – Karan
    Jun 20 at 5:40
  • $\begingroup$ In this part I'm separating some faces. But that's not the problem. As I said, the code works. When I put it on a operator in the properties panel suddenly the script cannot access the selected object even though you can see in the viewport that it has in fact been selected. $\endgroup$
    – AntGarGal
    Jun 21 at 6:49

2 Answers 2

0
$\begingroup$
import bpy


bpy.ops.object.mode_set(mode='EDIT')


for i in range(0, len(bpy.context.object.data.polygons)):
    bpy.context.object.data.polygons[i].select = True
$\endgroup$
2
  • 1
    $\begingroup$ Try switching to Object mode prior to selecting. $\endgroup$ Jun 20 at 9:03
  • $\begingroup$ Not what I'm trying to do. Also, you need to switch to Edit mode after doing the selection so that the mesh updates, not before. As I said, my problem is that when I put my code (that works fine) on an operator in a panel, it's no longer able to see the selected object. And this does happen in several functions. $\endgroup$
    – AntGarGal
    Jun 21 at 6:57
0
$\begingroup$

I found a workaround though I'm not sure is the best solution. Apparently when executing the operator from the porperties panel it's not able to run operators from the 3D viewport. Digging around I found that adding this loop before the lines does work, though it's a bit confusing to which ones they have to be added to as not all 3D viweport operators fail on my code.

for window in bpy.context.window_manager.windows:
   with bpy.context.temp_override(window=window):
       'Do whatever'
$\endgroup$

You must log in to answer this question.

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