Take the 2-minute tour ×
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It's 100% free, no registration required.

I'm making my own Python routines that distort space.

I would like to visualise the effect of this distortion.

To this end I want to create {x,y,z} aligned gridlines, so that I can observe the effect of the transformation on them.

So an x-aligned gridline would be say 1000 points between x=-50 and x=+50 with y=z=0

And the points would be joined to one another by line segments.

How could I create such a gridline using Python?

I found resources such as:

http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Three_ways_to_create_objects

http://askubuntu.com/questions/325485/how-to-make-a-mesh-by-using-python-script-on-blender

http://stackoverflow.com/questions/3657120/how-to-create-a-simple-mesh-in-blender-2-50-via-the-python-api

but in each case it creates primitives from verts+faces, not verts+edges

share|improve this question

2 Answers 2

I've just completed a similar project. Here's what I had:

import bpy
#allows reading csv files
import csv
#currentstep is a csvfile with directory ie "C:\myfile.csv"
with open(currentstep) as csvfile:

        csvreader = csv.reader(csvfile, delimiter=',')

        #skip header row
        next(csvreader)

        #for each row, set x,y,z coordinates
        for row in csvreader:
            x=float(row[0])/100
            y=float(row[1])/100
            z=float(row[2])/100

            #create vertex
            vert = ( x,y,z )

            #append vertice to list of vertices
            verts.append(vert)

CLONE ARRAY

vertsclone = list(verts)

INDEX ARRAY edge from a to b

averts = []
bverts = []

loops through vertices and connects them according to rule (rule = X & Y are equal)

while vertsclone:
    currentvert = vertsclone.pop()
    for vert in vertsclone:
        #check if x and y are equal; if so, add one vertex to averts, the other to b vertices
        if currentvert[0] == vert[0] and currentvert[1] == vert[1]:

            averts.append(verts.index(currentvert))
            bverts.append(verts.index(vert))

            vertsclone.pop(vertsclone.index(vert))

take list averts and list bverts and create edges by linking them together in indexed list (ie vertex 1 links to vertex 8)

edges = list(zip(averts,bverts))

Create mesh

mesh = bpy.data.meshes.new("csv_input")
object = bpy.data.objects.new("csv_input",mesh)
bpy.context.scene.objects.link(object)

create mesh data from python csv

#Verts are (x,y,z) Edges are 2-dimension lists of indices, ie (1,2); faces are n-dimension lists of vertices ie (1,2,3,4)
mesh.from_pydata(verts,edges,faces)
mesh.update(calc_edges=True)

You can modify the above at the Create Mesh step with whatever your rule for create the edges. In your case, you'll likely just create a set of vertices, and connect them as you go in a square pattern.

All you need to remember is that an edge is defined by a list of vertex indices.

So with mesh.from_pydata(verts,edges,faces) you can import any type of mesh data. If you just add vertices, it'll only create those. If you only add vertex and edges, you'll only get that. If you do vertex and faces, edges will be generated for themselves.

ie:

mesh data for mesh.from_pydata

verts = [(1.3,3.2,4.2),(3.3,3.4,2.1),(3.3,3.8,3.6)]

edges = [(1,2),(2,3)]

faces = [(1,2,3)]

share|improve this answer

You can create a grid primitive and remove the faces:

import bpy
import bmesh

sce = bpy.context.scene

me = bpy.data.meshes.new("Grid")
bm = bmesh.new()
bmesh.ops.create_grid(bm, x_segments=32, y_segments=32, size=50)
bmesh.ops.delete(bm, geom=bm.faces, context=3)
bm.to_mesh(me)

ob = bpy.data.objects.new("Grid", me)
sce.objects.link(ob)
sce.update()

This will give you a grid at origin, dimension x/y 100 and with 1024 vertices.


from_pydata():

it creates primitives from verts+faces, not verts+edges

You can either pass two sequences for verts and faces, or you give it verts and edges - it should work the same.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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