# Self.parent.parent\_?

**URL:** <https://discourse.processing.org/t/self-parent-parent/29855>\
**Category:** Processing.py\
**Created:** [May 4, 2021, 6:23pm UTC](https://discourse.processing.org/t/self-parent-parent/29855 "2021-05-04T18:23:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [May 4, 2021, 6:23pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/1 "2021-05-04T18:23:58Z")

</div>

class LineAgent(IParticle):  
def **init** (self, parent, dir):  
IParticle. **init** (self, parent.pos().cp(dir))

```
    self.parent = parent
    self.dir = dir
    self.hide()
    self.fric(0.2)

    self.isColliding = False

```

def update(self):

```
    if self.isColliding:
        self.del()

    else:
        if self.time() == 0 and self.alive():
            if self.parent != None and self.parent.alive():
                ln = ISpringLine(self, self.parent, 100).clr(255)
                t = -cos(IG.time()*0.06)*0.5+0.5
                ln.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 1.0)

                if self.parent.parent != None and self.parent.parent.alive():
                    st = IStraightenerCurve(
                        self, self.parent, self.parent.parent).tension(100)
                    st.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 0.5)

```

in this code block i didn t find how can i write true self.parent.parent.Anyone help me\_?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [May 4, 2021, 8:11pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/2 "2021-05-04T20:11:27Z")

</div>

Just to clarify something: do you intend to use an object-oriented approach that employs inheritance? If so, your code structure strikes me as odd, but maybe I’m confused.

Conceptually, would something like this work better?

```python
class IParticle(object):
    def __init__ (self, parent):
        self.x = parent
        self.y = 2

class LineAgent(IParticle):
    def __init__ (self, parent, z):
        super(LineAgent, self). __init__ (parent)
        self.z = z

la = LineAgent(1, 3)

print(la.x) # 1
print(la.y) # 2
print(la.z) # 3

```

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [May 4, 2021, 9:26pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/3 "2021-05-04T21:26:56Z")

</div>

> [@tabreturn](#):
>
> your code structure strikes me as odd, but maybe I’m confused.

@tabreturn : I think OP is trying to port one of the [IGeo](http://igeo.jp/) tutorials on “[Particle-based Branching Algorithm with Spring Force](http://igeo.jp/tutorial/57.html)”.

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [May 4, 2021, 9:58pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/4 "2021-05-04T21:58:47Z")

</div>

yes exacttly that is what i m trying to do

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [May 5, 2021, 8:41am UTC](https://discourse.processing.org/t/self-parent-parent/29855/5 "2021-05-05T08:41:05Z")

</div>

i wanna draw a line using with IStraightnerCurve object in my class LineAgent…It requests 3 points or vectors.LineAgent class product a point or vector and use that point production in itself.self is my main class production and self.parent is its parameter as a point or vector and what would be third one\_?self.parent.parent syntax does not work how can i produce third one\_?

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [May 5, 2021, 12:36pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/6 "2021-05-05T12:36:31Z")

</div>

@Mayarch : Please post the full script so people on the forum can investigate your issue.

My guess is that you are not correctly initializating the `LineAgent` class where a distinction is made between child agents that have a parent attribute (`this.parent`) and those that don’t have one because they are the very first _LineAgents_ created in `setup()`.

Hence the error when calling `parent` on agents that don’t have one.

@tabreturn : I’m not sure how `super(parent.pos().cp(dir))` (on line 7 below) would translate in Python.

– original class in Java –

```auto
class LineAgent extends IParticle{ 
  LineAgent parent;
  boolean isColliding;
  IVec dir;
    
  LineAgent(IParticle parent, IVec dir){
    super(parent.pos().cp(dir));
    if(parent instanceof LineAgent){
      this.parent = (LineAgent)parent;
    }
    isColliding=false;
    hide(); // hide point
    this.dir = dir;
    fric(0.2);
  }

```

– Python Mode –

```auto
class LineAgent(IParticle):
    def __init__ (self, parent, dir):
        super(LineAgent, self). __init__ (parent, dir) #or IParticle. __init__ (self, parent, dir)
        
        #parent.pos().cp(dir) #<-- WHERE should this go ?
        
        if isinstance(parent, LineAgent):
            self.parent = parent
        
        self.isColliding = False
        self.dir = dir
        self.hide()
        self.fric(0.2)

```

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [May 5, 2021, 2:18pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/7 "2021-05-05T14:18:29Z")

</div>

> [@solub](#):
>
> `#parent.pos().cp(dir) #<-- WHERE should this go ?`

exactly where should i use\_?because i wanted to try initialized like u but i could not make code work in previous session’s code

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [May 5, 2021, 2:20pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/8 "2021-05-05T14:20:09Z")

</div>

> [@solub](#):
>
> Please post the full script so people on the forum can investigate your issue.

add\_library(‘igeo’)

def setup():

```
# fullScreen(IG.GL)

size(480, 360, IG.GL)

IG.top()

IG.blackBG()

for i in range(4):

    LineAgent(IParticle(IRand.pt(-40, -40, 0, 40, 40, 0)

                        ).hide().fric(0.2), IRand.dir(IG.zaxis).len(2))

for i in range(20):

    IAttractor(IRand.pt(-200, -200, 0, 200, 200, 0)

               ).linear(50).intensity(-30)

```

class LineAgent(IParticle):

```
def __init__ (self, parent, dir):

    IParticle. __init__ (self, parent.pos().cp(dir))

    self.parent = parent

    self.dir = dir

    self.hide()

    self.fric(0.2)

    self.isColliding = False

def interact(self, agents):

    if self.time() == 0: # only in the first time

        for agent in agents:

            if isinstance(agent, LineAgent):

                if agent is not self:

                    if not agent.isColliding and not self.isColliding:

                        intxn = IVec.intersectSegment(

                            self.parent.pos(), self.pos(), agent.parent.pos(), agent.pos())

                        if intxn is not None: # intersection exists

                            if not intxn.eq(self.parent.pos()): # not parent agent

                                self.isColliding = True

                                return

def update(self):

    if self.isColliding:

        self.del()

    else:

        if self.time() == 0 and self.alive():

            if self.parent != None and self.parent.alive():

                ln = ISpringLine(self, self.parent, 100).clr(255)

                t = -cos(IG.time()*0.06)*0.5+0.5

                ln.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 1.0)

                if self.parent.parent != None and self.parent.parent.alive():

                    st = IStraightenerCurve(

                        self, self.parent, self.parent.parent).tension(100)

                    st.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 0.5)

        if self.time == 4:

            dir2 = self.dir.cp()

            angle = PI*0.12

            if IRand.pct(50):

                self.dir.rot(angle)

                dir2.rot(-angle)

            else:

                self.dir.rot(-angle)

                dir2.rot(angle)

            LineAgent(self, self.dir)

            if IRand.pct(20):

                LineAgent(self, dir2)

```

thats my code but i didn t find right way to make it work as you can see!!!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 5, 2021, 4:06pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/9 "2021-05-05T16:06:36Z")

</div>

Just the same code reposted w/ better formatting:

```auto
add_library('igeo')

def setup():
    size(480, 360, IG.GL)
    
    IG.top()
    IG.blackBG()

    for i in range(4):
        LineAgent(IParticle(IRand.pt(-40, -40, 0, 40, 40, 0)
        ).hide().fric(0.2), IRand.dir(IG.zaxis).len(2))

    for i in range(20):
        IAttractor(IRand.pt(-200, -200, 0, 200, 200, 0)
        ).linear(50).intensity(-30)

class LineAgent(IParticle):
    def __init__ (self, parent, dir):
        IParticle. __init__ (self, parent.pos().cp(dir))
        self.parent = parent
        self.dir = dir
        self.hide()
        self.fric(.2)
        self.isColliding = False

    def interact(self, agents):
        if self.time() == 0: # only in the first time

            for agent in agents:
                if isinstance(agent, LineAgent) and agent is not self:
                    if not agent.isColliding and not self.isColliding:
                        intxn = IVec.intersectSegment(
                            self.parent.pos(), self.pos(),
                            agent.parent.pos(), agent.pos())

                        # intersection exists and not parent agent
                        if intxn and not intxn.eq(self.parent.pos()):
                                    self.isColliding = True
                                    return

    def update(self):
        if self.isColliding:
            self.del()
        else:
            if self.time() == 0 and self.alive():
                if self.parent and self.parent.alive():
                    ln = ISpringLine(self, self.parent, 100).clr(255)
                    t = -cos(IG.time()*0.06)*0.5+0.5
                    ln.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 1.0)

                    # self.parent.parent is an IObject; got no method alive():
                    if self.parent.parent and self.parent.parent.alive():
                        st = IStraightenerCurve(
                            self, self.parent, self.parent.parent).tension(100)
                        st.hsb(0.7-t*0.2, 1, 0.8-t*0.2, 0.5)
    
            if self.time == 4:
                dir2 = self.dir.cp()
                angle = PI*0.12

                if IRand.pct(50):
                    self.dir.rot(angle)
                    dir2.rot(-angle)
                else:
                    self.dir.rot(-angle)
                    dir2.rot(angle)

                LineAgent(self, self.dir)

                if IRand.pct(20):
                    LineAgent(self, dir2)

```

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [May 5, 2021, 4:47pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/10 "2021-05-05T16:47:52Z")

</div>

With IGeo there shouldn’t be a `draw()` method I think.

---

<div class="post-metadata">

**Author:** ![Mayarch](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mayarch/32/10647_2.png) [@Mayarch](https://discourse.processing.org/u/Mayarch)\
**Post date:** [May 6, 2021, 1:32pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/11 "2021-05-06T13:32:15Z")

</div>

acording to duration time, IGeo executes update method in my class then with interact method check relationship new produced particle with old executed particles.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [May 6, 2021, 2:44pm UTC](https://discourse.processing.org/t/self-parent-parent/29855/12 "2021-05-06T14:44:57Z")

</div>

I’m pretty sure it is the opposite: `interact()` → `update()`

The `interact()` method runs first, looking for collisions (`isColliding`), then the `update()` method decides whether to spawn a new child agent or not based on the boolean result (`True` or `False`).

That is actually one of the main reasons why you should have an error appearing in the `interact()` method first rather than in the `update()` method (cf. your sketch).

Again, make sure that the attribute `parent` in the constructor of the `LineAgent()` object is conditioned by the type of its parent:

– Java –

```auto
if(parent instanceof LineAgent){
      this.parent = (LineAgent)parent;
    }

```

– Python –

```auto
if isinstance(parent, LineAgent):
    self.parent = parent

```

For the rest (the error itself), I’m afraid I can’t help, really sorry. I’ve asked Satoru Sugihara for some insights on the IGeo repo, let’s hope he’ll respond.
