Created
December 16, 2023 14:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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