2
$\begingroup$

New to blender and python here,

Given the following:

Coordinates (X,Y,Z) = (-100,100,1000) and Direction Z vector = (1,0,0) and Direction Y vector = (0,0,-1) where angle between direction vectors is 90 deg

How would I position the default cube in blender there using python?

$\endgroup$
  • 1
    $\begingroup$ It is a little hard to understand the expected workflow here, could you add a little more detail ? $\endgroup$ – Gorgious Apr 27 at 12:00
  • $\begingroup$ Wondering if it is possible to have an add on in blender which will load sources using the above list array into correct positions and rotations like lighttools software. $\endgroup$ – newblender Apr 27 at 12:08
  • $\begingroup$ Ok so you have a csv or excel file with transforms data, and you want to map these values to light objects transforms inside blender, is this correct ? $\endgroup$ – Gorgious Apr 27 at 12:31
  • $\begingroup$ Yes, I believe so $\endgroup$ – newblender Apr 27 at 12:34
  • 1
    $\begingroup$ Please use text to explain the data rather than image. Blender uses python. See stackoverflow.com (or other python sites, or search here for "[python] csv") re how to import csv or excel etc. Yes it is possible to make an addon. Suggest edit the question with the python to read the data. (or not) Add an example of one empty placed to match data line. As the question stands at the moment, to answer (other than "yes it's doable") requires teaching file I/O as well as the blender part. Please consider condensing question to just the how to position empty from data .. $\endgroup$ – batFINGER Apr 28 at 8:56
2
$\begingroup$

make a 3 x 3 rotation matrix with X, Y and Z axis as columns. Blender matrices are in rows, so will transpose rows to columns. The X axis is Y.cross(Z).

Make the matrix a 4x4 transform matrix and set its translation to coordinate

Note Added a scale factor so location 1000 -> 10. To avoid clipping. Set to 1 or remove.

import bpy
from mathutils import Matrix, Vector

context = bpy.context
scale = 0.01 # 1000 is a long way away

x, y, z, l, m, n, p, q, r = -500, 200, 1000, 1, 0, 0, 0, 0, -1

y_axis = Vector((l, m, n)).normalized()
z_axis = Vector((p, q, r)).normalized()

x_axis = y_axis.cross(z_axis).normalized()

M = Matrix((x_axis, y_axis, z_axis)).transposed()

M = M.to_4x4()

M.translation = scale * Vector((x, y, z))

#test
print(M)
print(x_axis, y_axis, z_axis)
bpy.ops.object.empty_add()
mt = context.object
mt.empty_display_type = 'ARROWS'
mt.matrix_world = M

or to add a cube

#test
bpy.ops.mesh.primitive_cube_add()
cube = context.object
cube.matrix_world = M
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for the answer @batFINGER, just wondering how I would go about running this correctly? New to this software. $\endgroup$ – newblender Apr 28 at 14:47
  • 1
    $\begingroup$ Paste into empty text in text editor. Click the run script button [>] $\endgroup$ – batFINGER Apr 28 at 14:48
  • $\begingroup$ If I wanted an object, say the cube for example to be at this specific position but it does not start from the origin, how would I get this to happen? $\endgroup$ – newblender Apr 29 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.