Blender (jusqu'à 2.49)
Python
échange réciproque (swap) des matrices 
entre 2 ou plusieurs objets
 
    Début   Index
précédentScript Python
Script python Suivant
1/ première version : échange 2 à 2
2/ seconde version : demi liste par demi liste
3/ par object connecté deux à deux dans une liste préparée 
interactivement
4/  lien sur Blender artist

1/ première version : échange 2 à 2

Dans cette première version, il n'y a que deux objets . En soi, ce script n'est pas très utile il ne fait que doubler la procédure suivante :

1/ sélectionner 2 objets et seulement 2 (plus ne servirait à rien, le surplus serait tout simplement ignoré).
2/ activer [move object center only] (le bouton avec 3points soulignés par une flêche)
3/ faire S(key), -(key), 1(key) et [entrée]
#!BPY

"""
Name: 'Swap Objetc's Location'
Blender: 237
Group: 'Object'
Tooltip: 'Swap the location of the two selected object'
"""

__author__ = "Authors Name"
__url__ = ("blender", "elysiun","Inverse Objetc's Location script homepage, http://http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_invobjloc.htm",)
__version__ = "0.02"
__bpydoc__ = """\
In this script, the first object of the the half list is swaped with the first object of with the second half .

If the  objects list contains 2 elements, the script  swaps the first one and second one.  if the list  owns 4  elements, it swaps the first with the third one and so on .
Usage: Select two objects or more but the number must always be even (pair)  and call the script .
Notes: two objects must be selected at least .
"""
# --------------------------------------------------------------------------
# Inverse Objetc's Location (c) Jean-Michel Soler 26/07/2005
# --------------------------------------------------------------------------

import Blender
from Blender import Object, Mathutils
try:
  O=Object.GetSelected()
  n2=len(O)/2
  for n in range(0,n2):
     mat=O[n].getMatrix()
     O[n].setMatrix(O[n+n2].getMatrix())
     O[n+n2].setMatrix(mat)
  Blender.Redraw()
except:
   print "two objects must be selected at least "

2/ seconde version : demi liste par demi liste
 

#!BPY

"""
Name: 'Swap Objetc's Location'
Blender: 237
Group: 'Object'
Tooltip: 'Swap the location of the two selected object'
"""

__author__ = "Jean-Michel Soler"
__url__ = ("blender", "elysiun","Inverse Objetc's Location script homepage, http://http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_invobjloc.htm",)
__version__ = "0.02"
__bpydoc__ = """\
In this script, the first object of the the half list is swaped with the first object of with the second half .

If the  objects list contains 2 elements, the script  swaps the first one and second one.  if the list  owns 4  elements, it swaps the first with the third one and so on .
Usage: Select two objects or more but the number must always be even (pair)  and call the script .
Notes: two objects must be selected at least .
"""

# --------------------------------------------------------------------------
# Inverse Objetc's Location (c) jmsoler 26/07/2005
# --------------------------------------------------------------------------

import Blender
from Blender import Object, Mathutils
try:
  O=Object.GetSelected()
  n2=len(O)/2
  for n in range(0,n2):
     mat=O[n].getMatrix()
     O[n].setMatrix(O[n+n2].getMatrix())
     O[n+n2].setMatrix(mat)
  Blender.Redraw()
except:
   print "two objects must be selected at least "

3/ Par objets connectés deux à deux dans une liste préparée interactivement
Télécharger le fichier blende d'exemple
Le procédé est un peu plus long . On commence par lancer le script 'swap_link_obj.py' soit en le plaçant dans la fenêtre de texte et en faisant Alt-p,soit en l'appelant par le menu Scripts (dans ce cas, il se chargera  automatiquement) .

Le script va se connecter à l'objet actif courant, et lui ajouter un scriptlink avec un évennement Redraw, c'est- à-dire un lien qui oblige le logiciel à exécuter le script chaque fois que la fenêtre 3D est redessinée . A partir de cet instant,ill crée une liste de nom d'objets qu'il accroche au module python principal . Il peut ainsi la retrouver à l'appel d'exécution suivant  ( les variables crées par les scripts  sont effacées entre deux exécutions sauf celles qui sont liées directement au module Blender ).

Chaque fois qu'il est exécuté, le script analyse la liste des noms d'objet . Si l'objet courant n'y figure pas , il ajoute le nom de ce dernier et déplace le lien scriptlink de l'ancien (objet actif) au nouveau (le nom du précédent objet est mémorisé dans la variable Blender.master) .  Cette méthode permet d'éviter d'avoir plus d'une seule et unique exécution du script  puisqu'il n'est attaché qu'à un seul objet.

Le script  compte aussi le nombre d'objet sélectionnés et si aucun objet, n'est sélectionné, il  vide la liste mémorisée , ce qui permet de recommencer une série de liens . 

Télécharger le script

#!BPY

"""
Name: 'Prepare swap connected objects'
Blender: 232
Group: 'Object'
Tooltip: 'draw connections to swap location between several selected objects'
"""
# ============================================
# jmsoler 27/07/2005, Blender artistic licence
# updated : 27/07/2005
# last update : 26/10/2005
# ============================================
import Blender
from Blender import BGL,Draw, Window, Scene
NAME='swap_link_obj2.py'

if NAME not in [t.name for t in Blender.Text.Get()]:
        SCRIPTDIR = Blender.Get('scriptsdir')+Blender.sys.sep
        print SCRIPTDIR+NAME
        Blender.Text.Load(SCRIPTDIR+NAME) 

sce=Scene.getCurrent()

if sce.getScriptLinks('Redraw')== None : 
        sce.addScriptLink(NAME,'Redraw')
elif NAME not in sce.getScriptLinks('Redraw'): 
        sce.addScriptLink(NAME,'Redraw')

if 1 : : 
  viewMatrix = Window.GetPerspMatrix()
  viewBuff = [viewMatrix[i][j] for i in xrange(4) for j in xrange(4)]
  viewBuff = BGL.Buffer(BGL.GL_FLOAT, 16, viewBuff)

  BGL.glLoadIdentity()
  BGL.glMatrixMode(BGL.GL_PROJECTION)
  BGL.glPushMatrix()
  BGL.glLoadMatrixf(viewBuff)

  O= Blender.Scene.getCurrent().getActiveObject()
  connectedlist=[]
  if len(Blender.Object.GetSelected())>1:
    for ob in Blender.Object.GetSelected():
        connectedlist.append(ob.getName())
 

  for P in range(0,len(connectedlist)-1,2):

     p=Blender.Object.Get(connectedlist[P]).getMatrix()[3][:]
     p2=Blender.Object.Get(connectedlist[P+1]).getMatrix()[3][:]

     BGL.glColor4f(1.0,0.5,0.0,1.0)
     BGL.glBegin(BGL.GL_LINES)
     BGL.glVertex3f(p[0],p[1],p[2])
     BGL.glVertex3f(p2[0],p2[1],p2[2])
     BGL.glEnd()
     BGL.glColor4f(1.0,1.0,1.0,1.0)

Blender.connectedlist=connectedlist

Après la sélection, un second script récupère la liste Blender.connectedlist et effectue l'échange :
Télécharger le script

#!BPY

"""
Name: 'Swap Objetc's Location'
Blender: 237
Group: 'Object'
Tooltip: 'Swap the location of the selected objects connected  with the scrtipt swap_link_obj.py'
"""

__author__ = "Jean-Michel Soler"
__url__ = ("blender", "elysiun","Inverse Objetc's Location script homepage, http://http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_invobjloc.htm",)
__version__ = "0.03"
__bpydoc__ = """\

Usage: Select two objects or more with the number must always be even (pair)  and call the script .
Notes: two objects must be selected at least .
"""

# --------------------------------------------------------------------------
# Inverse Object's Location (c) jmsoler 26/07/2005
# --------------------------------------------------------------------------

import Blender
from Blender import Object, Mathutils
if 1:#try:
  OBJECT=Blender.connectedlist
  for o in range(0,len(OBJECT)-1,2):
     O1=Blender.Object.Get(OBJECT[o])
     O2=Blender.Object.Get(OBJECT[o+1])
     mat=O1.getMatrix()
     O1.setMatrix(O2.getMatrix())
     O2.setMatrix(mat)
  Blender.Redraw()
#except:
#   print "two objects must be selected at least "
 

Liens sur Blender Artist
précédentScript Python
 Script python Suivant
Vers le  Haut de page

Les questions concernant cette page  peuvent être posées sur  :
 news://news.zoo-logique.org/3D.Blender


 

 

Livre en français
Blender : apprenez, pratiquez, Créez, livre, Ed. Campus Press, coll. Starter Kit
Blender Starter Kit

Forum
FAQ
Lexique
Didacticiels
Compilations
Blender2KT
Débuter
Modelage
Blender python
Materiaux
Lumière
Animation
API python (eng)
Archives nzn
Statistiques
Doc flash Sculptris
Galerie Sculptris

mon site de démos sur youtube