Add geometry to existing mesh


How do I add geometry to an existing mesh ?

I tried[‘Cube’].from_pydata( … )
but blender crashed :frowning:

(I’m porting an xml import script from 2.4)


I use this (acc. to example) to create an object:

def mesh_create(name, obj_loc, verts, edges, faces):
    # create a new mesh
    mesh ='_mesh')
    # create a new object
    object =, mesh)
    # set the object location
    object.location = obj_loc
    # link the object to the actual scene
    # create the mesh with the above definitions
    mesh.from_pydata(verts, edges, faces)
    # too see the edges the mesh has to be updated
    # return the object to the caller
    return object

Now I would like to have a function for example:

def draw_line(mesh, start, end)
draw_line('Mesh_Name', start, end)

that adds a line to this mesh.

Just got the idea: Should I create a new mesh and then link it to the object ?

Post a link to the original xml importer, with an xml example file. I enjoy writing importers.

Thank you for this offer, but since I’m doing this not only to get it done, but especially to learn something, this doesn’t really help me out…

I’ll continue trying, but it would be great if you, or someone could give me a hint concerning the above question.

Hm. Am I understanding this right: It’s not possible anymore (because of the new bmesh system) to simply append geometry to an existing mesh ? I have to say that I’m a little bit shocked by that.

This is the original piece of code from the 2.4 script:

def draw_dashed_line(mesh, start, end):
    w = 0.04
    step = w * (end - start).normalize()
    n = len(mesh.verts)
    for i in range(int(1 + 0.5 * (end - start).length / w)):
        a = start + 2 * i * step
        b = a + step
        if (b - end).length < step.length:
            b = end
        mesh.verts.extend([a, b])
        mesh.edges.extend([n + 2 * i, n + 2 * i + 1])

If I understand it correctly it simply creates a dashed line, appended to an existing mesh.
I can’t believe that it’s not possible anymore to do such an, I would say elementary, operation?!

I found that I can add single or multiple vertices by:[‘Cube’].vertices.add() for instance, but I didn’t found a good way to manipulate these.

I mean let’s say I want to make some drawing functions for instance

def draw_line():

def draw_circle():

and now I want to call these from different instances that make use of these, for creating different objects. That’s what the old script does. I mean that must be feasible ?!

One more thing: The fact that blender crashes after[‘Cube’].from_pydata( … ) with an internal error, is this a bug ? What’s the purpose of this function ? I mean it’s there, but it causes a crash, so that’s a bug, isn’t it ?

Sorry about all these complaints in my third post. I use blender for quite a long time now and I really love it, but I’m new to the scripting.
Ok, I’ll have a look at that script, thanks.

Ah, okay. I think I got it. I have to use the bmesh module to manipulate the mesh. I wasn’t aware of this module at all !! That was it, thanks ! There are all the functions I was missing :spin:

BLENDER_YASIM_IMPORT ? :slight_smile: very nice.

You got it right ! :eek:

I’ve already some simple elements(Items) working. Now I can advance to some more complicated ones. I might simplify some things because some of the matrix transformations seem a bit too complicated for me… I’ll see.

Another off topic question I have is: Is it possible to add tooltips to my own buttons ? At the moment it only displays the bpy function. Didn’t found something about it yet, but it’s not that important.

In the class where you define the operator, you should have the following:

class YasimImport(bpy.types.Operator):
    '''Import Yasim'''                   # Internal documentation (note triple quotes)
    bl_idname = "my_operators.unique_id" # Whatever you're calling it internally
    bl_label = "Click this button if you want to import." # Tooltip

That makes perfectly sense to me, but I just found in the api quickstart doc example it’s the following:

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

And it’s working! So, the tooltip in the triple quotes. That’s indeed a little bit strange, but anyways, thanks.

And I found that bl_label shows up on the button if no text=‘Foo’ is defined in the panel

class UIPanel(bpy.types.Panel):
    bl_label = "Yasim XML Importer"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    def draw(self, context):
        self.layout.operator("yasim_xml.load", <b>text='Load XML'</b>)

I made a button in the UIPanel for now, so it might be different in the menu or somewhere else… don’t think so, but who knows…

Quickstart API example :