[solved] how does a script constraint work ?

Hi all,

I’ve discover there is a possibility to write our own constraints. Like the wiki page concerning this feature is quite greedy of informations on its internal working, I downloaded Pyconstraint_noise.py to try to understand how are call the functions… but I didn’t. I googled a lot to find some information about it without success…

For exemple, let see the py i downloaded:

#BPYCONSTRAINT
'''
 PyConstraint template, access this in the "add constraint" scripts submenu.
 Add docstring here
'''
import Blender
from Blender import Draw, Mathutils, Noise
import math
 
''' 
 This variable specifies the number of targets  
 that this constraint can use 
'''
NUM_TARGETS = 1
 
'''
 This function is called to evaluate the constraint
 obmatrix:  (Matrix) copy of owner's 'ownerspace' matrix
 targetmatrices: (List) list of copies of the 'targetspace' matrices of the targets (where applicable)
 idprop:   (IDProperties) wrapped data referring to this 
     constraint instance's idproperties
'''
def doConstraint(obmatrix, targetmatrices, idprop):
 # Separate out the tranformation components for easy access.
 obloc = obmatrix.translationPart() # Translation
 obrot = obmatrix.toEuler()   # Rotation
 obsca = obmatrix.scalePart()  # Scale
 # Define user-settable parameters. # Must also be defined in getSettings().
 if not idprop.has_key('u_loc'): idprop['u_loc'] = 1
 if not idprop.has_key('u_rot'): idprop['u_rot'] = 0
 if not idprop.has_key('u_scale'): idprop['u_scale'] = 0
 if not idprop.has_key('u_locamount'): idprop['u_locamount'] = 1.0
 if not idprop.has_key('u_rotamount'): idprop['u_rotamount'] = 30.0
 if not idprop.has_key('u_scaleamount'): idprop['u_scaleamount'] = 1.0
 if not idprop.has_key('u_speed'): idprop['u_speed'] = 1.0
 
 la = idprop['u_locamount']
 ra = idprop['u_rotamount']
 sa = idprop['u_scaleamount']
 noise_speed = idprop['u_speed']
 
 time = Blender.Get('curtime')
 noise_vec = Mathutils.Vector(noise_speed*time, noise_speed*time, noise_speed*time)
 rv = Noise.vTurbulence(noise_vec, 3, 0, Noise.NoiseTypes.NEWPERLIN)
 
 half_vec = Mathutils.Vector(0.5, 0.5, 0.5)
 noise_vec = noise_vec - half_vec
 
 # Do stuff here, changing obloc, obrot, and obsca.
 if idprop['u_loc'] == 1:  
  obloc[0] += la*rv[0]
  obloc[1] += la*rv[1]
  obloc[2] += la*rv[2]
 if idprop['u_rot'] == 1:
  obrot[0] += ra*rv[0]
  obrot[1] += ra*rv[1]
  obrot[2] += ra*rv[2]
 if idprop['u_scale'] == 1:
  obsca[0] += sa*rv[0]
  obsca[1] += sa*rv[1]
  obsca[2] += sa*rv[2];
 
 # Convert back into a matrix for loc, scale, rotation,
 mtxloc = Mathutils.TranslationMatrix( obloc )
 mtxrot = obrot.toMatrix().resize4x4()
 mtxsca = Mathutils.Matrix([obsca[0],0,0,0], [0,obsca[1],0,0], [0,0,obsca[2],0], [0,0,0,1])
 
 # Recombine the separate elements into a transform matrix.
 outputmatrix = mtxsca * mtxrot * mtxloc
 # Return the new matrix.
 return outputmatrix
 
'''
 This function manipulates the matrix of a target prior to sending it to doConstraint()
 target_object:     wrapped data, representing the target object
 subtarget_bone:     wrapped data, representing the subtarget pose-bone/vertex-group (where applicable)
 target_matrix:     (Matrix) the transformation matrix of the target
 id_properties_of_constraint: (IDProperties) wrapped idproperties
'''
def doTarget(target_object, subtarget_bone, target_matrix, id_properties_of_constraint):
 return target_matrix
 
'''
 This function draws a pupblock that lets the user set
 the values of custom settings the constraint defines.
 This function is called when the user presses the settings button.
 idprop: (IDProperties) wrapped data referring to this 
   constraint instance's idproperties
'''
def getSettings(idprop):
 # Define user-settable parameters.
 # Must also be defined in getSettings().
 if not idprop.has_key('u_loc'): idprop['u_loc'] = 1
 if not idprop.has_key('u_rot'): idprop['u_rot'] = 0
 if not idprop.has_key('u_scale'): idprop['u_scale'] = 0
 if not idprop.has_key('u_locamount'): idprop['u_locamount'] = 1.0
 if not idprop.has_key('u_rotamount'): idprop['u_rotamount'] = 30.0
 if not idprop.has_key('u_scaleamount'): idprop['u_scaleamount'] = 1.0
 if not idprop.has_key('u_speed'): idprop['u_speed'] = 1.0
 
 # create temporary vars for interface 
 uloc = Draw.Create(idprop['u_loc'])
 ulocamount = Draw.Create(idprop['u_locamount'])
 urot = Draw.Create(idprop['u_rot'])
 urotamount = Draw.Create(idprop['u_rotamount'])
 uscale = Draw.Create(idprop['u_scale'])
 uscaleamount = Draw.Create(idprop['u_scaleamount'])
 uspeed = Draw.Create(idprop['u_speed'])
 
 # define and draw pupblock
 block = []
 block.append(("Speed", uspeed, 0.0000001, 1000.0, "The speed of animation"))
 
 block.append(" ")
 block.append(("Location", uloc, "Randomly modify the object's location"))
 block.append(("Amount", ulocamount, 0.0000001, 1000.0, "The amount of location randomness"))
 
 block.append(" ")
 block.append(("Rotation", urot, "Randomly modify the object's rotation"))
 block.append(("Amount", urotamount, 0.0000001, 1000.0, "The amount of rotation randomness"))
 
 block.append(" ")
 block.append(("Scale", uscale, "Randomly modify the object's scale"))
 block.append(("Amount", uscaleamount, 0.0000001, 1000.0, "The amount of scale randomness"))
 retval = Draw.PupBlock("Noise Constraint", block)
 
 # update id-property values after user changes settings
 if (retval):
  idprop['u_loc']= uloc.val
  idprop['u_locamount']= ulocamount.val
  idprop['u_rot']= urot.val
  idprop['u_rotamount']= urotamount.val
  idprop['u_scale']= uscale.val
  idprop['u_scaleamount']= uscaleamount.val
  idprop['u_speed']= uspeed.val

The function doTarget(), where or how is it called ? And its parameters, to what they correspond ? I suppose there is a document I missed which explain all those kind of stuffs, but i didn’t find it…

So, here is my request : Does somebody can give me a link or explain me how does it work ? Please. You would have all my gratitude (unfortunatly, I’m afraid to not have anything more to offer :D)

I think this might be a simpler example to look at: http://www.alienhelpdesk.com/python_scripts/pivot_constraint

Thanx man, I quickly red it, and i understood one or two things :)… now, I’m going to read it carefully compare etc… If understand enough things, i’ll report here what I understand.

Thanx again

Hi again, I finally made a pdf about scripts in blender, it’s a quick overview, and ther is explanations (more assumptions) about the pyconstraints… her is the link :
-out of order-

EDIT : damnit ! I didn’t see my account has expired… can you still download ? Wathever, i’m searching another file hosting…

Sorry for the triple post, this time this is a correct link :rolleyes::
http://ntox.fileave.com/Scripts%20in%20blender.pdf
Edit : like the number of users is limited to 2, if the link does’nt work, try it a bit later…