Isotropix Forums

Adding 2 vec3d in python

Clarisse Scripting related topics

Adding 2 vec3d in python

Unread postby stheroux » Fri Sep 08, 2017 8:50 pm

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: 43
Joined: Thu Jan 15, 2015 10:23 pm

Re: Adding 2 vec3d in python

Unread postby bvaldes » Mon Sep 11, 2017 9:10 am

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
User avatar
bvaldes
 
Posts: 160
Joined: Mon Sep 26, 2016 11:44 am

Re: Adding 2 vec3d in python

Unread postby stheroux » Mon Sep 11, 2017 2:25 pm

Hi Benoit,

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

Sylvain
stheroux
 
Posts: 43
Joined: Thu Jan 15, 2015 10:23 pm


Return to Scripting