Jump to content

Measuring multiple spline length and adding them


Recommended Posts

Hello guys, 

 

I'm a big Python noob, basically I know basics but I still stumble when it comes putting together code from scratch (documentation). But since this is something I will be doing a lot I started digging around. What I would need is to know the added length of all splines selected based on name. For example, having 3 spline 0,5m, 1m and 1,5m and the result would be 3 meters. 

 

I have found code that selects based on name or even part of a name and gets the spline length from point 0 and 1 (which would be sufficient, cause my splines will always be vertical, just Y axis) and I just don't know how to combine them, if that's the way to go anyway...

here are the scripts:

1) spline length

import c4d
from c4d import gui
# Welcome to the world of Python

def main():
    spline = doc.SearchObject ("Spline")
    print spline

    start = spline.GetPoint(0)
    end = spline.GetPoint(spline.GetPointCount()-1)
    print start, end
    direction = (end - start)
    length = direction.GetLength()
    print length

# Execute main()
if __name__=='__main__':
    main()

2) get objects by name

import c4d
from c4d import gui
# Welcome to the world of Python

def main():
    name = "Sp"
    c4d.CallCommand(100004766)
    objList = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_0)
    for obj in objList:
        if obj.GetName().find(name) == int(-1):
            doc.SetSelection(obj, c4d.SELECTION_SUB)
    c4d.EventAdd

# Execute main()
if __name__=='__main__':
    main()

 

 

Thanks for help,

 

Clear

Link to post

I got this code from Lester Banks for getting spline length for any spline (not just linear). Might interest you

 

import c4d
from c4d import utils
#Welcome to the world of Python


def main():
    spline = doc.SearchObject('Spline')

    newinst = utils.SplineLengthData() # creates new instance of the SplineLengthData class
    newinst.Init(spline) # initializes the spline object

    L = newinst.GetLength() # length

    #length.Free # frees up memory used by new SplineClass instance - needed ?

    print L

from here

Link to post

Thanks, I did find the first script there. Unfortunately I usually have like 1000-2000 splines of which I need the total length. 

Link to post

If the splines are all in a parent, you could loop through them and add the lengths

 

import c4d
from c4d import utils


def main():
    top = doc.SearchObject('my splines')

    splines = top.GetChildren() # list

    total = 0

    for spline in splines:

        newinst = utils.SplineLengthData() # creates new instance of the SplineLengthData class
        newinst.Init(spline) # initializes the spline object

        L = newinst.GetLength() # length

        total += L

        # newinst.Free() # frees up memory ?

    print total

in this file I checked the result with XPresso

 

spline_test.c4d

 

seems to agree

Link to post
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...