2
$\begingroup$

I'm making a simple script that adds a cube and moves it, and then I want to shift some of the faces, in order to change the cube's width height or depth. When I run it, the cube is created and moved, but altering de cube's dimensions gives an error:

convertViewVec: called in an invalid context

I'm using the code that's shown in the Info window when I select a face and drag it along one of the axes. I'm new to Blender and I can't figure out what's missing here..

import bpy

bpy.ops.mesh.primitive_cube_add()
bpy.ops.transform.translate(value=(0, 6.04281, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)
bpy.ops.transform.translate(value=(0, 0, -5.22667), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)
bpy.ops.transform.translate(value=(0, 2.9328, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)
bpy.ops.object.editmode_toggle()
bpy.ops.transform.translate(value=(0, 1.03954, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)

Edit - Solved it by creating a custom geometry, as shown here: How to create a mesh programmatically, without bmesh? (Pointed out by @poor)

The following code now creates a flat cube:

import bpy  

verts = [
(2.0, 1.0, -1.0), 
(2.0, 0.99, -1.0), 
(-1.0, 0.99, -1.0), 
(-1.0, 1.0, -1.0), 
(2.0, 1.0, 1.0), 
(2.0, 0.99, 1.0), 
(-1.0, 0.99, 1.0), 
(-1.0, 1.0, 1.0)]

faces = [
(0, 1, 2, 3), 
(4, 7, 6, 5), 
(0, 4, 5, 1), 
(1, 5, 6, 2), 
(2, 6, 7, 3), 
(4, 0, 3, 7)]

mesh_data = bpy.data.meshes.new("cube_mesh_data")  
mesh_data.from_pydata(verts, [], faces)  
mesh_data.update()

obj = bpy.data.objects.new("My_Object", mesh_data)  

scene = bpy.context.scene    
scene.objects.link(obj)    
obj.select = True  
$\endgroup$
6
  • 1
    $\begingroup$ I think this answer has the solution you are after. $\endgroup$
    – sambler
    Mar 20, 2015 at 6:25
  • $\begingroup$ Welcome dcvsu :) sambler's answer linked in the comment above should solves your issue, if not feel free to comment here. To position your object see this answers: blender.stackexchange.com/questions/26852/… Anyway, Enjoy the site! $\endgroup$
    – p2or
    Mar 20, 2015 at 11:16
  • $\begingroup$ Hi thanks @poor. The other answer also adds new faces, and that's not what I want to do. In Blender (manually) I select a face and drag it along one of the axes, changing the cube's width height or depth. (Maybe 'extrude' isn't the right word here). $\endgroup$
    – dcvsu
    Mar 20, 2015 at 14:11
  • $\begingroup$ Ahh - You want to shift the faces, right? If yes, edit your question to reopen it. In general there a 3 possibilities: create a custom geometry via python, shifting the selected faces or scale the object. Number 2 could be expensive, because you have to create an algorithm to get the right face. For 1 see this question: blender.stackexchange.com/questions/2407/…. $\endgroup$
    – p2or
    Mar 20, 2015 at 14:35
  • $\begingroup$ I edited the question, but it's solved now; creating a custom geometry via python did the job. Thanks a lot @poor! $\endgroup$
    – dcvsu
    Mar 20, 2015 at 16:11

0

Browse other questions tagged .