convertViewVec: called in an invalid context

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
den_d
Posts: 0
Joined: Tue Apr 16, 2013 4:00 pm
Location: Brussels/Belgium

convertViewVec: called in an invalid context

Post by den_d » Wed Jun 12, 2013 3:22 pm

Hello,

Finding the appropriate context for handling geometries in Blender/Python seems to be such a recurrent challenge for a rookie like me.

My purpose is to create a function that adds a basic box which is easier to handle than bpy.ops.mesh.primitive_cube_add(). I mean a 1*1*1 box with its center at the center of the bottom face. (I'm working on procedures for architectural composition so I always have to start thinking from the ground.)

What I'm currently doing is primitive_cube_add(), then I enter the edit geometry mode so as to scale the 2*2*2 cube down of a half, and I move all the vertices of a half unit along the z axis.

Here's the function :

Code: Select all

import bpy

def Add_my_Box(X_Dim, Y_Dim, Z_Dim, X_Box, Y_Box, Z_Box):
    bpy.ops.mesh.primitive_cube_add() 
    bpy.ops.object.editmode_toggle() 
    bpy.ops.transform.resize(value=(0.5, 0.5, 0.5)) 
    bpy.ops.transform.translate(value=(0, 0, 0.5))
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.transform.resize(value=(X_Dim, Y_Dim, Z_Dim))
    bpy.ops.transform.translate(value=(X_Box, Y_Box, Z_Box))

Add_my_Box(1, 1, 5, 0, 0, 0)
This function was working smoothly on Blender 2.62, but since I shifted to 2.66a, I got twice this message in the terminal : convertViewVec: called in an invalid context.
Even if the function seems to work properly (I get the result I expect), I wanna make it clean.

I figured out that both of the bpy.ops.transform.translate() are the problem because they're called in an inappropriate context. After looking up on forums, I've come up with this:

Code: Select all

import bpy
from mathutils import Vector

def Add_my_Box(X_Dim, Y_Dim, Z_Dim, X_Box, Y_Box, Z_Box):     
    bpy.ops.mesh.primitive_cube_add() 
    bpy.ops.object.editmode_toggle() 
    bpy.ops.transform.resize(value=(0.5, 0.5, 0.5))    
    bpy.ops.transform.translate(value=(0, 0, 0.5))    
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.transform.resize(value=(X_Dim, Y_Dim, Z_Dim))   
    context = bpy.context 
    obj = context.active_object
    obj.location += Vector([X_Box, Y_Box, Z_Box])

Add_my_Box(1, 1, 5, 0, 0, 0)
One convertViewVec: called in an invalid context down, I still gotta engage the second one, while editing vertices. And that's where I'm stuck. I've found several methods for entering in the edit mode and then handling vertices, but I just can't make them work.

Can someone give me a hand?
Thanks a lot.
den.

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Fri Jun 14, 2013 1:23 pm

operators are built for UI usage, so they rely on certain contexts. Some do work if used in a script (poll succeeds) but throw the above warning if there's something missing in the context. But that is usually something not necessarily needed for the operator to work in your script. If you wanna avoid it, you may switch to bmesh module.
I'm sitting, waiting, wishing, building Blender in superstition...

den_d
Posts: 0
Joined: Tue Apr 16, 2013 4:00 pm
Location: Brussels/Belgium

Post by den_d » Wed Jun 26, 2013 2:40 pm

CoDEmanX wrote:operators are built for UI usage, so they rely on certain contexts. Some do work if used in a script (poll succeeds) but throw the above warning if there's something missing in the context. But that is usually something not necessarily needed for the operator to work in your script. If you wanna avoid it, you may switch to bmesh module.
Thanks and sorry for late reply.
Indeed, I figured out that trying to handle geometries by scripting the same way you handle them in the 3D editor doesn't really make sense.

I'll try to play with the bmesh module.

Post Reply