9
$\begingroup$

In Blender's I can add/subtract mathutils.Vector without any problems.

vec3 = vec1 - vec2  # OK return a vector

But! It's paint to multiply/divide vector. For example if i multiply vectors i'll get a float.

vec3 = vec1 * vec2  # ERROR return a float

Is there a method to multiply/divide vectors correctly? like vec1.mult(vec2) which exists in other game engines.

$\endgroup$
6
  • 2
    $\begingroup$ vec1.cross(vec2) returns the cross product vec1 x vec2 and vec1.dot(vec2) returns the dot product vec1 · vec2. $\endgroup$
    – user7952
    Commented Mar 18, 2015 at 21:22
  • $\begingroup$ Note, this is known as "component-wise" or "element-wise" multiplication, see: math.stackexchange.com/questions/32516/… $\endgroup$
    – ideasman42
    Commented Mar 27, 2015 at 19:47
  • $\begingroup$ @mifth, I'll look into adding element-wise-multiple, there doesn't seem to be a convention for this for object-oriented-Vector API's though, asked here: programmers.stackexchange.com/questions/277583/… $\endgroup$
    – ideasman42
    Commented Mar 27, 2015 at 20:42
  • $\begingroup$ @ideasman42 While I agree that v1.hadamard_product(v2) is overly verbose, how about simply v1.hadamard(v2) or v1.compwise(v2)? $\endgroup$
    – user7952
    Commented Mar 27, 2015 at 20:53
  • $\begingroup$ @SixthOfFour, best move suggestions to the page linked. $\endgroup$
    – ideasman42
    Commented Mar 28, 2015 at 7:44

2 Answers 2

7
$\begingroup$

After a little bit of research, I concluded that the closest you can get is to use mathutils.Vector.cross() to do a cross multiplication of two vectors, or mathutils.Vector.dot() for dot multiplication; this info was gathered from The API documentation.

However, this doesn't seem to be the behavior you want - it seems you want to multiply the individual components of the vector, for which you can use something similar to this:

def mult(vec, vec2):
    temp = []

    for x, i in enumerate(vec):
        temp.append(i * vec2[x])

    return mathutils.Vector(temp)
$\endgroup$
1
  • $\begingroup$ Just curious: does anybody know why this is not mentioned in this source? ( docs.blender.org/api/current/mathutils.html ) To make it even more puzzling, it is mentioned in the v2.44 documentation... Am I looking at the wrong package or something? $\endgroup$
    – Ideogram
    Commented Apr 9, 2020 at 6:17
12
$\begingroup$

I am amazed mathutils.Vector class does not support vector scaling by another vector!

This can be for example useful when you get scale vector from matrix decomposition and you want to scale other vectors accordingly.

This is with a generator expression for better performance:

vec3 = Vector(x * y for x, y in zip(vec1, vec2))
$\endgroup$
2
  • $\begingroup$ generator-expression is a little nicer then list-comprehension here, edited. $\endgroup$
    – ideasman42
    Commented Mar 27, 2015 at 19:39
  • $\begingroup$ @ideasman42 good point! $\endgroup$ Commented Mar 27, 2015 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .