creating a tetrahedron frame

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

zappfinger
Posts: 7
Joined: Mon Dec 24, 2012 12:55 pm
Location: Breda, the Netherlands

creating a tetrahedron frame

Postby zappfinger » Wed Dec 26, 2012 10:18 am

Hello,

I am trying to create a tetrahedron frame, consisting of cylinders.
Any ideas?

Richard

Mr.Yeah
Posts: 7
Joined: Mon Dec 24, 2012 1:41 pm

Postby Mr.Yeah » Wed Dec 26, 2012 1:22 pm

At least I can tell you how to make tetrahedra.
There's a script named add_mesh_solid.py in scripts\addons. When the add-on is active, you can create a tetrahedron with

Code: Select all

bpy.ops.mesh.primitive_solid_add(source='4')

Look at the source code so you understand how to get the right vertices. I think you'll need something like this (taken from add_mesh_solid.py)

Code: Select all

s = sqrt(2)/3.0
t = -1/3
u = sqrt(6)/3

v = [(0,0,1),(2*s,0,t),(-s,u,t),(-s,-u,t)]
so you can use v as a basis to create cylinders with

Code: Select all

bpy.ops.mesh.primitive_cylinder_add()
Article of primitive_cylinder_add() in the Blender API documentation

zappfinger
Posts: 7
Joined: Mon Dec 24, 2012 12:55 pm
Location: Breda, the Netherlands

Postby zappfinger » Wed Dec 26, 2012 6:14 pm

Thanks, I will take a look.
I was trying along another path, having found the code to create a tetrahedron and also some code to create a cylinder between 2 points.
But combining then does not give the correct result:

import math
import bpy
from mathutils import Vector

def doCils():

X1, Y1, Z1 = 0, -1 / math.sqrt(3),0
X2, Y2, Z2 = 0.5, 1 / (2 * math.sqrt(3)), 0
X3, Y3, Z3 = -0.5, 1 / (2 * math.sqrt(3)), 0
X4, Y4, Z4 = 0, 0, math.sqrt(2 / 3)

D1x, D1y, D1z = X2-X1, Y2-Y1, Z2-Z1
doIt(D1x, D1y, D1z)
D2x, D2y, D2z = X3-X2, Y3-Y2, Z3-Z2
doIt(D2x, D2y, D2z)
D3x, D3y, D3z = X4-X3, Y4-Y3, Z4-Z3
doIt(D3x, D3y, D3z)
D4x, D4y, D4z = X4-X1, Y4-Y1, Z4-Z1
doIt(D4x, D4y, D4z)


def doIt(dx,dy,dz):
Vaxis = Vector((dx, dy, dz)).normalized() # versor
Vobj = Vector((0,0,1)) # z axis versor
Vrot = Vobj.cross(Vaxis) # rotation axis: normal to both vecs
angle = math.acos(Vobj.dot(Vaxis)) # angle between Vaxis and Vobj

Cone = bpy.ops.mesh.primitive_cylinder_add(radius=.1)
bpy.ops.transform.rotate(value=(angle,),axis=Vrot) # rotate


doCils()

Mr.Yeah
Posts: 7
Joined: Mon Dec 24, 2012 1:41 pm

Postby Mr.Yeah » Thu Dec 27, 2012 9:25 am

I didn't look closely at your code so I don't know why it doesn't work but it's way too complicated. Here's the code I made:

Code: Select all

import bpy
from math import sqrt
from mathutils import Vector


def cylinder(a, b, radius=1.0):
    depth = (a-b).length
    location = a.lerp(b, 0.5)
    rotation = Vector((0, 0, 1)).rotation_difference(a-b).to_euler()

    bpy.ops.mesh.primitive_cylinder_add(
        depth=depth,
        location=location,
        rotation=rotation,
        radius=radius)


def tetrahedron_frame(radius=0.1):
    s = sqrt(2)/3
    t = -1/3
    u = sqrt(6)/3

    v = [(0, 0, 1), (2*s, 0, t), (-s, u, t), (-s, -u, t)]

    cylinder(Vector(v[0]), Vector(v[1]), radius=radius)
    cylinder(Vector(v[0]), Vector(v[2]), radius=radius)
    cylinder(Vector(v[0]), Vector(v[3]), radius=radius)
    cylinder(Vector(v[1]), Vector(v[2]), radius=radius)
    cylinder(Vector(v[2]), Vector(v[3]), radius=radius)
    cylinder(Vector(v[3]), Vector(v[1]), radius=radius)


tetrahedron_frame()
And next time please use the [code]-Tags.

zappfinger
Posts: 7
Joined: Mon Dec 24, 2012 12:55 pm
Location: Breda, the Netherlands

Postby zappfinger » Thu Dec 27, 2012 9:36 am

Thanks very much!

Very usefull and definitely more elegant.


Richard

Mr.Yeah
Posts: 7
Joined: Mon Dec 24, 2012 1:41 pm

Postby Mr.Yeah » Thu Dec 27, 2012 10:28 am

BTW: You can add an option to scale the frame while creating.
Replace

Code: Select all

def tetrahedron_frame(radius=0.1):
    s = sqrt(2)/3
    t = -1/3
    u = sqrt(6)/3

    v = [(0, 0, 1), (2*s, 0, t), (-s, u, t), (-s, -u, t)]
with

Code: Select all

def tetrahedron_frame(radius=0.1, scale=1.0):
    s = sqrt(2)/3*scale
    t = -1/3*scale
    u = sqrt(6)/3*scale

    v = [(0, 0, scale), (2*s, 0, t), (-s, u, t), (-s, -u, t)]


cu
Mr.Yeah


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest