Skip to content

Instantly share code, notes, and snippets.

@murez
Created January 24, 2023 11:33
Show Gist options
  • Save murez/f8a268e0a8b11a7ce4733fec72597371 to your computer and use it in GitHub Desktop.
Save murez/f8a268e0a8b11a7ce4733fec72597371 to your computer and use it in GitHub Desktop.
import sys
import os
import bpy
import numpy as np
import pickle
from mathutils import Matrix, Vector
from math import pi
import json
def Quaternion2Matrix(q):
q_np = np.array(q)
norm = np.linalg.norm(q_np)
w,x,y,z = q[0] / norm, q[1] / norm, q[2] / norm, q[3] / norm
R = [[1-2*y**2-2*z**2, 2*x*y-2*z*w, 2*x*z+2*y*w],
[2*x*y+2*z*w, 1-2*x**2- 2*z**2, 2*y*z-2*x*w],
[2*x*z-2*y*w, 2*y*z+2*x*w, 1-2*x**2-2*y**2]]
return R
def GetLSR(model):
m_location = list(model.location[:])
m_scale = model.scale[0]
m_quaternion = model.rotation_euler.to_quaternion()
m_rotation_3_3 = Quaternion2Matrix(m_quaternion)
m_rotation_9 = np.array(m_rotation_3_3).reshape(9).tolist()
return m_location, m_scale, m_rotation_9
if __name__ == "__main__":
model_data = []
model_list = ['rat', 'ox', 'tiger', 'rabbit', 'dragon', 'snake', 'horse', 'goat', 'monkey', 'rooster', 'dog', 'pig']
for i, m in enumerate(model_list):
model = bpy.data.objects[m]
m_pos, m_scl, m_rot = GetLSR(model)
m_dic = {
'hint_model_name': m,
'model_id': str(i),
'scale': m_scl,
'position': m_pos,
'rotation': m_rot
}
model_data.append(m_dic)
with open('C:\\Users\\ACER\\siggraph\\scence2_obj.json', 'w') as f:
json.dump(model_data, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment