1
$\begingroup$

I'm using Blender 2.74 and trying to make this part of my code work:

import bpy
from math import *

me = bpy.ops.mesh.primitive_cylinder_add(radius = 0.5, depth = 1)

for v in me.verts:
    v.co[2] =v.co[2]+0.5

But appear the folow error:'set' object has no attribute 'verts'.

What's the problem?

$\endgroup$
1
$\begingroup$

If you print out the content of me in the console, you can see that it does not contain the cylinder's mesh data:

>>> me
{'FINISHED'}

The bpy.ops.mesh.primitive_cylinder_add operator, like most operators in Blender, returns its exit status (in our case {'FINISHED'} - signalling success) rather then the result of whatever it did.

If you want to access the vertices, you can do this through bpy.context.object.data.vertices, since after you add a cylinder primitive, it becomes the active object (context.object):

import bpy
from math import *

bpy.ops.mesh.primitive_cylinder_add(radius = 0.5, depth = 1)
me = bpy.context.object.data

for v in me.vertices:
    v.co[2] =v.co[2]+0.5

A few other things worth noting:

  1. The vertex collection in the mesh object's data is called vertices (For instance: context.object.data.vertices) not verts. The bmesh module does call the vertex collection verts, which is probably why this confused you.
  2. It's considered bad practice to import everything from a module, unless you're actually going to use everything you imported (from math import *). You should only import whatever function you intend to use. For instance:

    from math import degrees, radians
    
  3. Small tip regarding debugging in blender (and in general): The error message you got gave you a nice lead for debugging. It says that whatever it is you're trying to use .verts with doesn't really have any attribute called 'verts'. The next logical thing is to check what the heck is inside the me variable by printing it, to see why it has no verts. This would have showed that me is indeed a set, not a mesh data object, which whould have indicated that the bpy.ops.mesh.primitive_cylinder_add operator returns an exit status, and not the cylinder's data.

  4. [EDIT] And another late one. To get a shorter and more readable version of your scaling action, you can replace this:

    v.co[2] =v.co[2]+0.5
    

    With this:

    v.co.z += 0.5
    
$\endgroup$
  • 1
    $\begingroup$ Nice explanation @TLousky! The tips will help me a lot! Thanks ! :) $\endgroup$ – Jimmy Nov 11 '15 at 18:50
  • $\begingroup$ No problem @Jimmy, added another small tip for good measure ;-) $\endgroup$ – TLousky Nov 11 '15 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.