Blender (jusqu'à 2.49)
|
Connecter
des points
dans
l'espace
scripts/macro
python
|
|
#-*-coding:utf-8-*-
import Blender
from Blender import *
from Blender.Scene import Render
import math
length=2.0
epsilon=0.0000001
PI=math.pi
listesommets=[[[0.0,0.0,0.0], [0.0,1.0,0.0]],
[[0.0,0.0,0.0], [1.0,0.0,0.0]],
[[0.0,0.0,0.0], [0.0,-1.0,0.0]],
[[0.0,0.0,0.0], [-1.0,0.0,0.0]],
[[0.0,0.0,0.0], [1.0,1.0,0.0]],
[[0.0,0.0,0.0], [-1.0,1.0,0.0]],
[[0.0,0.0,0.0], [-1.0,-1.0,0.0]],
[[0.0,0.0,0.0], [1.0,-1.0,0.0]]]
for n in range(40):
listesommets.append([[Blender.Noise.random()*5,
Blender.Noise.random()*5,0.0],
[Blender.Noise.random()*5,
Blender.Noise.random()*5,0.0]])
matrix=Mathutils.Matrix([0.0,0.0,-1.0,0.0],
[0.0,1.0,0.0,0.0],
[1.0,0.0,0.0,0.0],
[1.0,0.0,0.0,1.0])
me1=Mesh.Primitives.Icosphere(2, length*1.5)
sc=Scene.GetCurrent()
ob1=sc.objects.new(me1,'Mesh')
ob1.setLocation(0.0,0.0,0.0)
mat=Material.New('Mat')
mat.setRGBCol(*[0.4,0.0,0.4])
me1.materials+=[mat]
a=ob1.getLocation()
for n in listesommets:
mat0=Material.New('Mat0')
mat0.setRGBCol([1,1,0])
me2=Mesh.Primitives.Icosphere(2, length)
ob2=sc.objects.new(me2,'Mesh')
ob2.setLocation(n[1][0]*10.0,n[1][1]*10.0,n[1][2])
ob2.setName(' '+str(listesommets.index(n))+'_Y')
me2.materials+=[mat]
b=ob2.getLocation()
ba1=float(b[1]-a[1])
ba0=float(b[0]-a[0])
longueur= ((b[0]-a[0])**2.0+(b[1]-a[1])**2)**0.5
ang_B=0.0
if abs(ba1)>epsilon and abs(ba0)>epsilon:
ang_B=math.atan(abs(ba1)/abs(ba0))
if ba0>0.0 and ba1<0.0: ang_B = 2.0*PI-ang_B
elif ba0<0.0 and ba1<0.0: ang_B -=PI #correct
elif ba0<0.0 and ba1>0.0: ang_B=PI-ang_B
elif abs(ba1) < epsilon :
if ba0<0.0 : ang_B = PI
elif abs(ba0) < epsilon :
if ba1<0.0 : ang_B = PI*1.5
if ba1>0.0 : ang_B = PI*0.5
me_a=Mesh.Primitives.Cylinder(8, 1.0, 2.0)
me_a.transform(matrix, True)
ob_a=sc.objects.new(me_a,'MyOb')
print longueur
print ang_B
ob_a.setSize(longueur/2.0,1.0,1.0)
ob_a.setEuler(0.0,0.0,ang_B)
ob_a.setLocation( a[0],a[1],0.0)
ob_a.setName(' '+str(listesommets.index(n))+'_A')
me_a.materials+=[mat0]
a=ob2.getLocation()
Window.RedrawAll()
|
#-*-coding:utf-8-*-
import Blender
from Blender import *
from Blender.Scene import Render
import math
length=2.0
epsilon=0.0000001
PI=math.pi
listesommets=[[[0.0,0.0,0.0], [0.0,1.0,0.0]],
[[0.0,0.0,0.0], [1.0,0.0,0.0]],
[[0.0,0.0,0.0], [0.0,-1.0,0.0]],
[[0.0,0.0,0.0], [-1.0,0.0,0.0]],
[[0.0,0.0,0.0], [1.0,1.0,0.0]],
[[0.0,0.0,0.0], [-1.0,1.0,0.0]],
[[0.0,0.0,0.0], [-1.0,-1.0,0.0]],
[[0.0,0.0,0.0], [1.0,-1.0,0.0]]]
for n in range(40):
listesommets.append([
[Blender.Noise.random()*5,
Blender.Noise.random()*5,
Blender.Noise.random()*25 ],
[Blender.Noise.random()*5,
Blender.Noise.random()*5,
Blender.Noise.random()*25]])
#====================
# cette matrice effectuie une rotation de
# 90 degres sur l'axe x :
#
# 0.0, 0.0, 0.0, 0.0
# 0.0, cos, -sin, 0.0
# 0.0, sin, cos, 0.0
# et deplace le point d'une unité sur
# l'axe y :
# x, y, z, 1.0
#====================
matrix=Mathutils.Matrix([1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, -1.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 1.0])
sc=Scene.GetCurrent()
#====================
# On cree un premier materiau : mat1,
# pour les sphere
#====================
mat1=Material.New('Mat1')
mat1.setRGBCol(*[0.4,0.0,0.4])
#====================
# On cree un second materiau : mat2,
# pour les cylndres
#====================
mat2=Material.New('Mat2')
mat2.setRGBCol([1,1,0])
#====================
# On cree une premiere sphere : ob1
#====================
me1=Mesh.Primitives.Icosphere(2, length*1.5)
ob1=sc.objects.new(me1,'Mesh')
ob1.setLocation(0.0,0.0,0.0)
me1.materials+=[mat1]
#====================
# On enregistre la position de cette sphere
# pour en faire l'origine d'un cylindre
# plus tard.
#====================
a=ob1.getLocation()
#====================
# On cree une serie de spheres et de cylindres
# au hasard.
#====================
for n in listesommets:
#====================
# point suivant dans l'espace : ob2
#====================
me2=Mesh.Primitives.Icosphere(2, length)
ob2=sc.objects.new(me2,'Mesh')
ob2.setLocation(n[1][0]*10.0,n[1][1]*10.0,n[1][2])
ob2.setName(' '+str(listesommets.index(n))+'_Y')
me2.materials+=[mat1]
#====================
# On range les coordonnees de ce nouveau point
#====================
b=ob2.getLocation()
#====================
# On s'en sert pour calculer la distance
# au point precedent.
#====================
longueur= ((b[0]-a[0])**2.0 + (b[1]-a[1])**2 + (b[2]-a[2])**2)**0.5
#====================
# On cree un cylindre : ob_a
# 8 divisions
# 1 de rayon
# 2 de hauteur
#====================
me_a=Mesh.Primitives.Cylinder(8, 1.0, 2.0)
#====================
# Comme ce cylindre est cree par defaut
# sur l'axe z, on doit les faire pivoter
# sur l'axe y et surtout amener sa base sur
# le point d'origine du maillage
#====================
me_a.transform(matrix, True)
#====================
# Le maillage doit etre associe a un
# objet pour etre pris en compte dans
# la base de donnee de Blender.
# On cree l'objet
#====================
ob_a=sc.objects.new(me_a,'Ob_a')
#====================
# Il ne faut pas trop s'inquieter du
# nom.
# Si un objet portant le meme
# patronyme existe deja, le suivant
# est automatiquement renomme ".001"
# ou ".nnn" si un numero identique
# a deja ete attribue
#====================
#====================
# On donne la taille definitive en
# utilisant la fonction setSize.
# qui comme son nom ne l'indique pas
# multiplie la taille.
# Celle-ci etant deja de 2.0 su rl'axe
# y, on doit donc la divider par deux.
#====================
ob_a.setSize(1.0,longueur/2.0,1.0)
#====================
# on positionne sur l'emplacement
# de la premirer sphere
#====================
ob_a.setLocation( a[0],a[1],a[2])
#====================
# poru eviter d'avoir a calculer
# un rotation, on ajoute une
# fonction de tracking sur
# la seconde sphere.
#====================
tracked = ob2
ob_a.makeTrack(tracked)
#====================
# On renomme par commodite.
#====================
ob_a.setName(' '+str(listesommets.index(n))+'_A')
#====================
# On attribue une couleur differente
# pour plus de lisiblilite.
#====================
me_a.materials+=[mat2]
#====================
# On remplace la position de
# la sphere 1 pour le prochain passage
# de la boucle.
#====================
a=ob2.getLocation()
Window.RedrawAll()
|
Les questions concernant cette page
peuvent être posées sur :
news://news.zoo-logique.org/3D.Blender
|