[ create a new paste ] login | about

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

Python, pasted on Oct 29:
import c4d
from c4d.utils import SplineHelp

def main():
    smp_mode = op[c4d.ID_USERDATA,1] # the sample mode
    smp_offset = op[c4d.ID_USERDATA,2] # the sample offset in real world units
    smp_index = op[c4d.ID_USERDATA,3] # the point index to sample
    spline = op.GetObject() # the spline object
    target = spline.GetDown() # the null
    
    if isinstance(spline, c4d.SplineObject) and target is not None:
        # real world unit offset
        if smp_mode == 1000:
            sh = SplineHelp()
            sh.InitSpline(spline)
            if smp_offset > 0.0:
                rwoff = float(smp_offset) / sh.GetSplineLength()
            else:
                rwoff = 0.0
            # get the spline matrix at that point. if i do remember correctly the method does not 
            # offer any stabilazition (banking) like for example the c4d sweep nurbs object does.
            # so you would have to implement that yourself (Paralllel Transport Frame | Rotation 
            # Minimization Frame). The SplineHelp class has its flaws in general IMHO.
            mg = sh.GetMatrix(rwoff, realoffset = True)
            target.SetMg(mg)
            sh.FreeSpline()
        # index based offset
        elif smp_mode == 1001:
            # create a linear clone
            spline_clone = spline.GetClone()
            spline_clone[c4d.SPLINEOBJECT_TYPE] = c4d.SPLINEOBJECT_TYPE_LINEAR
            sh = SplineHelp()
            sh.InitSpline(spline_clone)
            # get data
            pdata = spline.GetAllPoints()
            pmid = spline.GetPointCount() - 1
            if smp_index > pmid:
                smp_index =  pmid
            # calclulate the percentage offset. i actually wrote bullshit in the the thread
            # the formula obviously has to be poff = (p(1) - p(0)) + ... + (p(n+1) - p(n)).
            poff, plen = 0.0, 0.0
            if smp_index != 0:
                for pid in xrange(pmid):
                    idlen = (pdata[pid + 1] - pdata[pid]).GetLengthSquared()
                    if pid < smp_index: poff += idlen
                    plen += idlen
            if poff > 0.0:
                pfac = poff / plen
            else:
                pfac = 0.0
            noff = sh.GetOffsetFromReal(pfac)
            mg = sh.GetMatrix(noff, realoffset = True)
            # there is quite an error margin with that approach, i am not really sure
            # why. might be some kind of rounding error or a more fundamental problem
            # with my solution.
            target.SetMg(mg)
            sh.FreeSpline()


Create a new paste based on this one


Comments: