Object/Bone Stepper Modifier 249

Hi Guys,
I’ve been hounding the guys in the simplequestion thread (in particular John316, thanks dood for all the help!!!) about stepping constraints on bones and other objects. Some of my pynode scripts may depend on this, and i know 2.50 has it for the action/ipos, but i know that a few people still are stuck with using 2.49 blender versions for different reasons until 2.50 is more functional. This script is for them (and in particular myself tehe).

This script can be put on any object (i use it on bones that are constrained to act like ‘slider’ controls) that has its loc constrained to 1 axis. This constraint will enforce preset graduations on the movement of the obj.

An example would be a bone that is constrained to move between 0.0 and 2.0 (Localspace) in its Y axis. This script will enforce the bone to move only by 0.2 Bunits at a time (0.0, 0.2,0.4, etc etc). Uses would be mainly for control-type bones which you want to be more regularly and not fluent. For me, this be in conjunction with another pynode that has 10 channels and needs to have 10 graduations in its control bone for it to work without dying if i accidently Iframe 0.21.

As far as i’m aware, this script can be expanded easily onto 2d and 3d constrained objects and perhaps even to objects not constrained by to a range. If people want, i can code these versions too.

Notes and things you need to know to run the script are in the header of the py file (simple enough to understand and customise i believe). If anyone is interested, i can record a simple youtube vid of applying this to an object and showing what its capable of. Think this might help?

Enjoy!

Example image (click for it)

#BPYCONSTRAINT
######################################################
#"""
#Name: 'Object 1D Stepper Modifer'
#Blender: '249B'
#Group: 'Python - Modifiers'
#Tooltip: 'Edit the values in the script below in the section User Definable Variables to work'
#"""

#__author__ = "Holly Tsukiko Grimes"
#__url__ = ["http://blenderartists.org/forum/member.php?find=lastposter&t=41201"]
#__version__ = "0.9a"
#__bpydoc__ = ["http://blenderartists.org/forum/showthread.php?p=1619945"]\
#Description:
#This script is perfect if you are using a slider bone in an animation and need to constrain it to
#certain graduations of values. For isntance, say you have a slider bone that in its local space
#is constrained between 0.0 and 2.0 and you only want it to move by 0.2 graduations at a time. 
#Normally blender will give you absolute minute control, so unless you enter in the value manually
#each time you can't have it like a limited slider. This script will allow that kind of control, 
#which is easily customizable below in the user defined values section. Like all constrainted actions
#use "VisualLoc" keyframing when animating to take account of the new constraint. It might be also nice
#to change the interpolation mode in the ipo to Linear or Constant.
#
#
#Notes:
#This works best with 1D bones (can only move up and down) with a limitloc constraint on it too
#(like in the example values, its limited to 0.0 and 2.0 in the Y direction). 2D bone sliders is
#possible but one would need to extend to code to accomodate this.
#
#Also you need to be careful which CSpace you set this in. If you animate in world space and your
#limitlocation constraint is in the world space, then set this to worldspace in the modifier control
#panel(F9). Normally with bones, LocalSpace is used for all constraints, I recommend working in this
#CSpace too.
#
#I need to test this if it works with other python scripts that will rely on its location (for instance
#a pynode in a material that needs its visualloc). See my thread for updates and latest version.
#
#I also need to test if this works outside bones. I think this should work with nearly every object type,
#but this needs testing. I built this for bones in mind but the function seems standard for any object.
#
#
#Edit the values in the specified field below. 
#The GNUM_AXIS variable indicates to the code which axis is constrained. 
#The GNUM_STEPS variable is how many graduations you wish between the limits
#The LIMIT variables should be identical to what you have placed in the LimitLocation constraint.
######################################################
#These never need changing
import Blender
import math
from Blender import Mathutils, Draw


######################################################
# The ammount of targets
# Do not change for this script.
######################################################
NUM_TARGETS = 0


######################################################
#User Definable Variables
#Edit these names for your own objects.
######################################################
GMAX_LIMIT = 2.0
GMIN_LIMIT = 0.0
GNUM_STEPS = 10.0
GNUM_AXIS = 1 #0 for X, 1 for Y and 2 for Z. This is for the constrainted axis.


######################################################
# The Main function
# Nothing needs changing here
######################################################
def doConstraint(obmatrix, targetmatrices, idprop):
    
    #get the needed information.
    obloc = obmatrix.translationPart()
    obrot = obmatrix.rotationPart()
    obsca = obmatrix.scalePart()
    
    #Axis stepper
    div_stepper=(GMAX_LIMIT - GMIN_LIMIT)/GNUM_STEPS
    div_q = obloc[GNUM_AXIS]/div_stepper
    div_mod = (obloc[GNUM_AXIS]%div_stepper)/div_stepper
    if div_mod<0.50:
        obloc[GNUM_AXIS]=(math.floor(div_q))*div_stepper
    else:
        obloc[GNUM_AXIS]=(math.ceil(div_q))*div_stepper
    
    #create the necessary matrices to return.
    mtxloc = Mathutils.TranslationMatrix(obloc)
    mtxrot = obrot.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 updated matrix for the object
    return outputmatrix

After some thought, i made an update of this file so that there is barely any editing to be done in python. You can choose which axes are constrained now using the options button in the modifier window. You can now change the graduations in the python file (in the User Definable Variables section) easily for each axes.

Here is an example file showing what you can do with the python file. The top bone isn’t constrained, the bottom 2 are constrained on different axes with different graduations and you can see the results on the bone set on the right.
http://www.4shared.com/file/vv8k1Xtz/StepperExample1.html
(script is in the zip file with the example blend file, and also is internal in the blend file)

I tested the script a bit more, it doesn’t work with normal objects as they don’t have the ‘script’ option in the modifier window. This only works with bones in pose mode.

Hope others can make use of this script.