I'm working on a project that requires me to get some of the position and rotation values to give to an Arduino via pyserial, but after sifting through the Blender documentation I can't find how to get them through script.
You can access an object's transformations through the relevant properties (location, rotation_euler - for Euler XYZ rotations, and scale) this way:
>>> object.location Vector((-2.609607696533203, -3.618044137954712, 1.8581657409667969)) >>> object.rotation_euler Euler((-0.6668576598167419, 0.697817325592041, -1.8853096961975098), 'XYZ') >>> object.scale Vector((1.2768771648406982, 1.4246054887771606, 0.6418209075927734))
Do note though that these values represent the object origin. If your origin is not at the center of your object, you might get unexpected location values.
Move the origin to the geometry (Ctrl+Alt+Shift+C > Origin to Geometry) first on all your objects to avoid this.
bpy.ops.object.select_all( action = 'SELECT' ) bpy.ops.object.origin_set( type = 'ORIGIN_GEOMETRY' )
On a more general note though, I'd like to mention some great sources for documentation and examples for Blender python scripting that will help you find answers more quickly next time (since finding object location and other transformations is probably one of the most basic and best documented things you can do in scripting, I'm assuming you're not familiar with these sources):
Blender API official documentation (with useful search bar)
I'd also suggest using to_translation for location and to_euler or to_quat for rotation
>>> C.object.matrix_world.to_translation() Vector((-0.7104860544204712, -0.3396846354007721, -0.06676528602838516)) >>> C.object.matrix_world.to_euler('XYZ') Euler((0.0, -0.0, 0.0), 'XYZ') >>> C.object.matrix_world.to_quaternion() Quaternion((1.0, 0.0, 0.0, 0.0))
especially if you are using a constraints or similar to move / rotate any objects, or are being moved by the parent object etc.