Simple line drawing in Blender using Python

I’m sorry for the “newbie” question - but I’m trying to write a script that draws a series of lines between a series of objects. I’ve looked and look for some method or api that let’s me pass in two 3d points (x,y,z) and then just draws a straight line between them.

I can’t believe this is not one of the basic, built-in features available and I’m hoping I just haven’t found it yet. I don’t want to calc eulers or quats and rotation matrices or anything else like that - simply dump a total of 6 points to some api and have it draw the line for me.

Desperately seeking an answer for this and not wanting to do it the hard way.

Thanks,

Aktad…

For the following purposes, I assumed that a line meant connecting two vertices (in a mesh object) with an edge. If you meant something else, please clarify.

Its relatively easy, not one function call, but still easy (and it could be turned into a function call):


import Blender
from Blender import*

me = Mesh.New('line')
me.verts.extend([ [1, 1, 1], [2,2,-1] ])
me.edges.extend(0, 1)

scn = Scene.GetCurrent()
scn.objects.new(me, 'line')

To turn it into a function call you could:


import Blender
from Blender import Mesh, Scene

scn = Scene.GetCurrent()
me = Mesh.New('line')

def drawLine(mes, p1, p2):
     mes.verts.extend(p1, p2)
     mes.edges.extend(-1, -2)

drawLine(me, [0, 1, 2], [3, -2, 1])
scn.objects.new(me, 'LineObj')

Some of the code isn’t completely generic and would need a little tweaking, but it should work for the most part (wrote the code on the fly, NOT tested)

hmm i guess he means object-relation lines.
like the ones drawn between a parented objects.

Thanks for the reply guys - sorry I wasn’t clear enough…

Assume two cubes - one at 1,1,1 and one at 10,10,10. I need to draw or create a line that runs from the center of the cube at 1,1,1 to the center of the cube at 10,10,10.

In a real example - assume 100 cubes in a 10 by 10 grid 10 or 20 units above another 100 by 100 grid.

Some of the things in the top grid are related to some of the things in the bottom grid. I want to draw lines connecting the related things…

I’m still learning and so I’m not used to some of the terminology but that’s how I would explain my goals.

Thanks again…

ForTe,

Your quick code sample does indeed draw a line between the two objects. However the line doesn’t show up in renders, I can’t change the width, and I can’t apply a material to it.

I’m sure this is due to a lack of understanding on my part about the actual artifact being drawn here. I’ll check the manual/online refs again to see what else I might be missing
but this is absolutely a step in the right direction.

Many thanks,

Aktad

My code draws an edge, so if you change the material settings to wireframe, you will be able to see it. You can also turn on shadeless to have it appear to be solid line, without shading.

It’d probably be overkill, but it wouldn’t be too hard to make a very thin extruded box using similar techniques to what I used above in the code.

The Blender Caliper script by macouno already does this with many settings.
http://www.alienhelpdesk.com/python_scripts/caliper
m.a.

Thanks Meta-Androcto, I had looked at the caliper code a few weeks back but backed off on wading thru the whole script to pull out just the pieces I needed. It looked to me like that script is going through all the work I had been hoping to avoid (lazy I guess).

I’ll keep working the edges.extend idea for now as it seems to give me most of what I want in just a few lines of code. I’ll print out the caliper code too - maybe it will make more sense if I physically shuffle the paper around.

Thanks,

Aktad

ForTe,

I finally had a chance to get back to my scripts - you’re absolutely correct about turning on wireframe and shadeless. Flipping those two options allow the edge to be rendered and gives me a “good enough” version of what I’m looking to do at the moment.

Thanks!

Aktad