Skip to content

Instantly share code, notes, and snippets.

@jmpinit
Created December 16, 2023 14:48
Show Gist options
  • Save jmpinit/097dcb40bf879636d9d0977d1861b93f to your computer and use it in GitHub Desktop.
Save jmpinit/097dcb40bf879636d9d0977d1861b93f to your computer and use it in GitHub Desktop.
Extract the positions and orientations of specified objects in a Blender scene as JSON
# Usage:
# blender my_scene.blend --background --python extract_positions.py -- Foo Bar
import bpy
import json
import sys
def get_object_data(object_names):
data = {}
for name in object_names:
obj = bpy.data.objects.get(name)
if not obj:
raise Exception(f'Failed to find object "{name}"')
data[name] = {
'location': list(obj.location),
'rotation': [obj.rotation_quaternion.w, obj.rotation_quaternion.x,
obj.rotation_quaternion.y, obj.rotation_quaternion.z]
}
return data
def main():
# The arguments for us come after --
object_names = sys.argv[sys.argv.index('--') + 1:]
object_data = get_object_data(object_names)
print(json.dumps(object_data, indent=4))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment