Delete short segments

On 06/01/2018 at 18:59, xxxxxxxx wrote:

Hello Plugincafe! :)

With this python generator, I want to remove short segments of a spline.
In line 17 I'm exploding spline into segments and in line 30 I'm trying to delete short segments but seems like I'm doing something wrong.

    minLength = op[c4d.ID_USERDATA,1] #User defined Minimum lenght  
    for i in xrange(pcount) :  
        pointA = spline.GetPoint(spline.GetPointCount()-i-1)  
        pointB = spline.GetPoint(spline.GetPointCount()-i-2)  
        direction = (pointB - pointA)  
        distance = direction.GetLength()  
        if distance == 0:  
            continue  
        elif distance < minLength:  
            sel.Select(spline.GetPointCount()-i-1)  
            sel.Select(spline.GetPointCount()-i-2)  
            res = utils.SendModelingCommand(command = c4d.MCOMMAND_DELETE,list=[spline],mode=c4d.MODELINGCOMMANDMODE_POINTSELECTION,doc=doc)      
            sel.DeselectAll()

On 07/01/2018 at 05:44, xxxxxxxx wrote:

I found a solution... this is a bit brute force way, but at least it works...

import c4d
#Welcome to the world of Python
  
  
def main() :
    child = op.GetDown()
    if not child: return
    clone = child.GetClone()
    child.Touch()
  
    PolyFX = c4d.BaseObject(1019222)
    PolyFX[c4d.MGPOLYFX_MODE] = 1
    PolyFX[c4d.ID_MG_TRANSFORM_SCALE] = c4d.Vector(0.99)
    PolyFX.InsertUnder(clone)
  
    res = c4d.utils.SendModelingCommand(command = c4d.MCOMMAND_CURRENTSTATETOOBJECT,
                                        list = [clone],
                                        mode = c4d.MODELINGCOMMANDMODE_ALL,
                                        doc = op.GetDocument(),
                                        )
    if res[0] is not None: poly = res[0]
    else:     return None
  
    sel = poly.GetPointS()
    pcount = poly.GetPointCount()
    sel.SelectAll(pcount-1)
    
    res = c4d.utils.SendModelingCommand(command = c4d.MCOMMAND_EXPLODESEGMENTS,
                                        list = [poly],
                                        mode = c4d.MODELINGCOMMANDMODE_POINTSELECTION,
                                        doc = op.GetDocument()
                                        )
  
  
    length = c4d.utils.SplineLengthData()
    for i in poly.GetChildren() :
        length.Init(i)
        dist = length.GetLength()
        if dist < op[c4d.ID_USERDATA,1]:
            i.Remove()
            length.Free()
  
    return poly
  

On 07/01/2018 at 06:33, xxxxxxxx wrote:

Worth mentioning that this technique is a bit slow...

On 07/01/2018 at 09:26, xxxxxxxx wrote:

I figured it out...

  
    doc = op.GetDocument()
    sel = spline.GetPointS()
    minLength = op[c4d.ID_USERDATA,1] #User defined Minimum lenght
    n = 0
    for i in xrange(spline.GetPointCount()-1) :
        pointA = spline.GetPoint(n)
        n+=1
        pointB = spline.GetPoint(n)
        direction = (pointB - pointA)
        distance = direction.GetLength()
        if distance == 0:
            n+=1
        elif distance < minLength:
            sel.Select(n)
            res = utils.SendModelingCommand(command = c4d.MCOMMAND_DISCONNECT,list=[spline],mode=c4d.MODELINGCOMMANDMODE_POINTSELECTION,doc=doc)
            res = utils.SendModelingCommand(command = c4d.MCOMMAND_DELETE,list=[spline],mode=c4d.MODELINGCOMMANDMODE_POINTSELECTION,doc=doc) 
            n+=1   
            sel.DeselectAll()

On 08/01/2018 at 02:08, xxxxxxxx wrote:

Hi Merk, although it seems out you've already find your own way, I wonder looking at your second post, if an ObjectData Python plugin would fit better with the intent of the code since I see you handling object's child as much as normally happens with an ObjectData. Is my guess wrong?

Best, Riccardo

On 08/01/2018 at 02:54, xxxxxxxx wrote:

In object generator plugins, I'm using GetAndCheckHierarchyClone.
In the second example, python generator is used...