0

Currently this code creates an empty at a defined position and have used parent to get an object, in this case a cube to be in that position. However the newly created cube states its position as the origin rather than its actual position. How would I get the cube to display its actual position which would be the same as the empty?

import bpy
from mathutils import Matrix, Vector

context = bpy.context
scale = 0.01 

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

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

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))

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

bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.object
cube.parent = empty
newblender
  • 41
  • 1
  • 6
  • 3
    Please ask only one question per post to make your question useful for others too... Please read: https://blender.stackexchange.com/help/how-to-ask – brockmann Apr 30 '20 at 09:57
  • 1
    How to create an empty without using ops (allows to don't rely on the object in context in your case): https://blender.stackexchange.com/questions/51290/how-to-add-empty-object-not-using-bpy-ops – brockmann Apr 30 '20 at 10:00
  • 2
    I'll edit the other question out – newblender Apr 30 '20 at 10:01
  • 1
    Why not using the matrix again? my_cube.matrix_world.translation? Don't get your question... – brockmann Apr 30 '20 at 10:11
  • A little confused as to what that non bpy.ops does, the use of adding an empty in the right place showing an axis system was useful to indicate the positioning of an object if it were placed there – newblender Apr 30 '20 at 10:13
  • @brockmann did try to use that however it shifts everything by a transformation- what I wanted was for an object to be positioned in a specific location irrespective of its original location – newblender Apr 30 '20 at 10:15
  • Read it again and still not sure what you mean. All I can say is: the origin of an object is always the actual position. – brockmann Apr 30 '20 at 10:21
  • So when the code is run the empty is created and put in the position as shown by the matrix/vectors. The empty displays its position with respect to the origin (-500, 200, 1000 etc). When the cube is created it goes to the same position as the empty but when you click on it it says its location is (0,0,0) which is different to what the empty displays – newblender Apr 30 '20 at 10:29
  • Wasn't notified... I voted to re-open the question. – brockmann Apr 30 '20 at 13:16
  • Oh right didn't realise it was closed – newblender Apr 30 '20 at 13:27
  • Ok cube for empty in prior question,. Don't parent the object to the empty, don't even add the empty. Add the cube as you've done then cube.matrix_world = M If you do use the empty and parent the cube to it, empty.matrix_world.translation to get its global location. The python console is a good way to work this stuff out. @brockmann Think this is a dupe, just wrong dupe. . https://blender.stackexchange.com/questions/109984/how-to-get-global-object-location-python https://blender.stackexchange.com/questions/39677/how-do-you-get-an-objects-position-and-rotation-through-script – batFINGER Apr 30 '20 at 23:43

0 Answers0