[ create a new paste ] login | about

Link: http://codepad.org/Yt1jzwwP    [ raw code | fork ]

Python, pasted on Jul 2:
import c4d
from c4d import utils

def main():
    obj = op.GetObject() # get the object hosting the python tag
    # try to get the data
    try:
        traceSpl = op[c4d.ID_USERDATA,1] # the userdata holding the traced spline, the user data type has to be link.
        minThickness = op[c4d.ID_USERDATA,2] #min thickness, ud type has to be real_percent 0-100% (which is the real default value)
        scaleSplData = obj[c4d.SWEEPOBJECT_SPLINESCALE] # the sweep nurbs spline data
    # something went wrong
    except:
        traceSpl, scaleSplData = None, None
    # check if our objects are of the required type
    if isinstance(traceSpl, c4d.SplineObject) and isinstance(scaleSplData, c4d.SplineData):
        # make sure thickness value is valid:
        if not isinstance(minThickness, float): minThickness = 0
        else: minThickness = min(minThickness, 1.0)
        # get the normalized and range mapped velcoity data
        veldata = getNormalizedVelocity(traceSpl.GetAllPoints(), minThickness)
        l = len(veldata)
        # init the SplineData according to our velocity data list length
        scaleSplData.MakeLinearSplineLinear(l)
        # write the values:
        for i, vel in enumerate(veldata):
            vec = c4d.Vector(float(i)/float(l), vel, 0)
            scaleSplData.SetKnot(i, vec)
        # write the splinedata back
        obj[c4d.SWEEPOBJECT_SPLINESCALE] = scaleSplData

def getNormalizedVelocity(pnts, minThickness = None):
    """
    Compute the velcoity vetcors out of an SplineObject control point list.
    :param pnts         : the control point list
    :param minThickness : the minimum profile radius as a perecentage value (0.0 - 1.0).
    :return             : float(0.0 - 1.0)[]
    """
    l = (len(pnts))
    mvel, temp, result = 0, [], []
    # build raw velocity data
    for i in xrange(l - 1):
        vel = (pnts[i+1] - pnts[i]).GetLength()
        mvel = max(mvel,vel) # the peak velocity
        temp.append(vel)
    # normalize the data
    for n in temp:
        val = float(n) * (1.0 / float(mvel))
        if minThickness is None: # unclamped
            result.append(val)
        else: # clamped (actually range mapped)
            result.append(utils.RangeMap(val, 0.0, 1.0, minThickness, 1.0, False))
    return result


Create a new paste based on this one


Comments: