calling operator in while loop

If I use “bpy.ops.uv.project_from_view()” on a button it works

class TFPPanel(bpy.types.Panel):
    bl_label = "Texture from Projection"
    bl_idname = "TFPPanel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    
    def draw(self, context):
        layout = self.layout
        row = layout.row(align = True)
        row.alignment = 'EXPAND'
        row.operator("start.button", text = "Start")
  
#BUTTON START      
class OBJECT_OT_StartButton(bpy.types.Operator):
    bl_idname = "start.button"
    bl_label = "Start it"
    bl_description = "start script"
 
    def execute(self, context):
        main(context)
        t_current = bpy.context.scene.frame_current 
        t_all = bpy.context.scene.frame_end
        ob = bpy.context.object
        me = ob.data
        
        t_current = bpy.context.scene.frame_current 
        t_all = bpy.context.scene.frame_end

        #set UVs
        me.uv_textures["uvUNW"].active = False
        me.uv_textures["uvPFV"].active = True

        <b>#project from view
        bpy.ops.uv.project_from_view()</b>
        
        #add 1 to t_currrent
        t_current + 1

        #go to next frame
        bpy.context.scene.frame_current += 1
        
        print(t_current)
            
        return{'FINISHED'}


bpy.utils.register_module(__name__)

but if I use it in a while loop it stops working, but I don’t get an error-message in the terminal


import bpy
import os

def main(context):
    ob = bpy.context.object
    me = ob.data

    #get current frame
    t_current = bpy.context.scene.frame_current

    #get scenelength
    t_all = bpy.context.scene.frame_end


#LAYOUT
class TFPPanel(bpy.types.Panel):
    bl_label = "Texture from Projection"
    bl_idname = "TFPPanel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    
    def draw(self, context):
        layout = self.layout
        row = layout.row(align = True)
        row.alignment = 'EXPAND'
        row.operator("start.button", text = "Start")
  
#BUTTON START      
class OBJECT_OT_StartButton(bpy.types.Operator):
    bl_idname = "start.button"
    bl_label = "Start it"
    bl_description = "start script"
 
    def execute(self, context):
        main(context)
        t_current = bpy.context.scene.frame_current 
        t_all = bpy.context.scene.frame_end
        ob = bpy.context.object
        me = ob.data
        
        while (t_current &lt;= t_all) :

            t_current = bpy.context.scene.frame_current 
            t_all = bpy.context.scene.frame_end
    
            #set UVs
            me.uv_textures["uvUNW"].active = False
            me.uv_textures["uvPFV"].active = True
    
<b>            #project from view
            bpy.ops.uv.project_from_view()</b>
            
            #add 1 to t_currrent
            t_current + 1

            #go to next frame
            bpy.context.scene.frame_current += 1
            
            print(t_current)
            
        return{'FINISHED'}

Can you tell me what I’m doing wrong? I’m relative new to python scripting.

I had a similar problem a couple of days ago… I think the problem might be that the mesh data isn’t updated correctly when you change the active uv-layer.

me.uv_textures["uvUNW"].active = False
me.uv_textures["uvPFV"].active = True

First of all, i don’t think you need to set anything to false here. It’s done automatically.

Try changing the above to:

me.uv_textures["uvPFV"].active = True
me.update()

And se how it goes

For the record;
I’ve submitted a bug report related to this, but I’m not entirely sure it’s really a bug any more. Perhaps it might give a little insight any way…

teppic may be on to something. When you change frames a scene.update() occurs behind the scene. Your me= ob.data may be invalid at that point. Try placing me = ob.data inside the loop…?

you’re right the False is really needless. The mesh-update makes absolutely sense. Unfortunately it doesn’t work in my case.

yeah it really seems, the mesh doesn’t update. That could be the reason for not getting an error-message. The project-from-view works it just uses the old mesh.
Unfortunately me = ob.data inside the loop doesn’t seem to solve the problem.

Have you read the Gotchas?
http://www.blender.org/documentation/blender_python_api_2_62_0/info_gotcha.html?highlight=gotchas

No, but now I read it. And I guess it helped me to understand the problem, thank you.
I looked at the object_animrenderbake.py too and found the solution: adding bpy.context.scene.frame_set(t_current)

import bpy
import os

def main(context):
    ob = bpy.context.object
    me = ob.data

    #get current frame
    t_current = bpy.context.scene.frame_current

    #get scenelength
    t_all = bpy.context.scene.frame_end


#LAYOUT
class TFPPanel(bpy.types.Panel):
    bl_label = "Texture from Projection"
    bl_idname = "TFPPanel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    
    def draw(self, context):
        layout = self.layout
        row = layout.row(align = True)
        row.alignment = 'EXPAND'
        row.operator("start.button", text = "Start")
  
#BUTTON START      
class OBJECT_OT_StartButton(bpy.types.Operator):
    bl_idname = "start.button"
    bl_label = "Start it"
    bl_description = "start script"
 
    def execute(self, context):
        main(context)
        t_current = bpy.context.scene.frame_current 
        t_all = bpy.context.scene.frame_end
        #ob = bpy.context.object
        #me = ob.data
        
        while (t_current &lt;= t_all) :
            
            t_current = bpy.context.scene.frame_current 
            t_all = bpy.context.scene.frame_end
            ob = bpy.context.object
            me = ob.data
            
<b>            #update scene
            bpy.context.scene.frame_set(t_current)</b>
            
            #set UVs
            me.uv_textures["uvPFV"].active = True
            me.update()
            
            #project from view
            bpy.ops.uv.project_from_view(orthographic=False, correct_aspect=False, clip_to_bounds=False, scale_to_bounds=False)
            
            #add 1 to t_currrent
            t_current + 1

            #go to next frame
            bpy.context.scene.frame_current += 1
            
            print(t_current)
            
        return{'FINISHED'}


bpy.utils.register_module(__name__)

Thank you both for helping me.