Last active
November 5, 2021 11:10
-
-
Save michalpelka/f05dbbd8c54c627518d82cfdffe47b35 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # This example assumes we have a mesh object selected | |
| import bpy | |
| import bmesh | |
| import numpy as np | |
| obj = bpy.context.active_object | |
| P = [] | |
| A = [] | |
| for v in obj.data.vertices: | |
| co_final = obj.matrix_world @ v.co | |
| A.append([co_final.x,co_final.y,co_final.z]) | |
| A = np.array(A) | |
| print (A.shape) | |
| cenx = np.average(A[:,0]) | |
| ceny = np.average(A[:,1]) | |
| cenz = np.average(A[:,2]) | |
| A = [] | |
| for v in obj.data.vertices: | |
| co_final = obj.matrix_world @ v.co | |
| A.append([co_final.x-cenx,co_final.y-ceny,co_final.z-cenz]) | |
| A = np.array(A) | |
| print ("centroid %f %f %f" % (cenx,ceny,cenz)) | |
| u,s,vh = np.linalg.svd(A, full_matrices=True) | |
| np.set_printoptions(suppress=True) | |
| print ("A=") | |
| print (A) | |
| print ("S=") | |
| print (s) | |
| print ("u=") | |
| print (vh) | |
| nn = obj.name+"_svd " | |
| if nn in bpy.data.objects: | |
| empty = bpy.data.objects[nn] | |
| else: | |
| print ("aa") | |
| empty = bpy.data.objects.new( nn, None ) | |
| empty.empty_display_size = 10 | |
| empty.empty_display_type = 'PLAIN_AXES' | |
| bpy.context.collection.objects.link(empty) | |
| empty.matrix_world = ((vh[0,0],vh[0,1],vh[0,2],0), | |
| (vh[1,0],vh[1,1],vh[1,2],0), | |
| (vh[2,0],vh[2,1],vh[2,2],0), | |
| (0,0,0,1)) | |
| empty.location.x = cenx | |
| empty.location.y = ceny | |
| empty.location.z = cenz | |
| print ("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment