## Adding 2 vec3d in python

Clarisse Scripting related topics

### Adding 2 vec3d in python

Hi,

I have a very basic question about adding 2 vec3d in python. This fails with a TypeError:

#### python code

```a = ix.api.GMathVec3d(1.0,2.0,3.0)b = ix.api.GMathVec3d(4.0,5.0,6.0)a + b
```

TypeError: in method 'GMathVec3d___add__', argument 2 of type 'double'

I've tried a bunch of things to call the add method directly instead of the + operator but nothing worked. Im sure im missing something simple.

Thanks for the help.
Sylvain
stheroux

Posts: 68
Joined: Thu Jan 15, 2015 9:23 pm

### Re: Adding 2 vec3d in python

Hi,

The GMathVec3d class does not come from python. There is no built-in function in python to do what you want. You are trying to add two custom classes, Python does not understand.

So you have to make your own function to do that. There is an example of what you could do:

#### python code

```a = ix.api.GMathVec3d(1.0,2.0,3.0)b = ix.api.GMathVec3d(4.0,5.0,6.0)def addVec3d(*args):    result = [0.0, 0.0, 0.0] # Init the result to avoid issues in the for loop    for arg in args:        if isinstance(arg, ix.api.GMathVec3d): # Check if the argument is a GMathVec3d            for i in range(3):                result[i] = result[i] + arg[i]    return ix.api.GMathVec3d(result[0], result[1], result[2])                print addVec3d(a, b)
```

The function addVec3d() can take as many GMathVec3d you want. I hope that can help you.

Regards.
Benoit VALDES
Isotropix
Clarisse QA

bvaldes

Posts: 286
Joined: Mon Sep 26, 2016 10:44 am

### Re: Adding 2 vec3d in python

Hi Benoit,

Ok that's what i thought, i ended up using a similar solution.
Thanks for the sample code

Sylvain
stheroux

Posts: 68
Joined: Thu Jan 15, 2015 9:23 pm