Last active
March 28, 2022 23:42
-
-
Save MicroCBer/1ce0fb8bd9a6e04bdb72751274dfa86f to your computer and use it in GitHub Desktop.
Convert models into cubes which can be used by ScuffedWalls ModelToWalls func.
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
# How to use this | |
# 1. Rename the model you want to convert into '1' | |
# 2. Copy the script into the text editor in the blender (you can find it in the Scripting tab, create a file and rename it into gen.py if needed) | |
# 3. Click the start button. | |
# It may spend a long time to covert it due to the slow runtime speed of python. Blender might keep no response. Just wait patiently. | |
# You can see the progress in the console. Open the console by clicking Window->Switch system console. | |
# By MicroBlock | |
# 2021.2.1 | |
# If you are changing the script and republishing it, please include the original author and address | |
import bpy | |
import mathutils as mu | |
import math | |
ob=bpy.data.objects["1"] | |
edges=ob.data.edges | |
verts = ob.data.vertices | |
def safeDiv(a,b): | |
# avoid (something / 0) and throw a error; | |
if(b==0):return math.inf | |
else:return a/b | |
def dist(A,B): | |
return (A-B).length/2 | |
def th(A,degree): | |
t=[] | |
for x in range(0,len(A)): | |
if x!=degree:t.append(A[x]) | |
return mu.Vector(t); | |
for edge in edges: | |
A=mu.Vector(verts[edge.vertices[0]].co) | |
B=mu.Vector(verts[edge.vertices[1]].co) | |
mid=(A+B)/2 | |
distance=dist(A,B) | |
bpy.ops.mesh.primitive_cube_add(align='WORLD', location=mid) | |
bpy.context.object.name="_mgen_g1_"+str(edge.index) | |
bpy.context.object.scale=(distance, 0.01, 0.01) | |
bpy.context.object.rotation_euler[0] = 0 | |
# 2d A ad Mid Z | |
tZA=th(A,2) | |
tZmid=th(mid,2) | |
bpy.context.object.rotation_euler[2] = math.atan(safeDiv((tZA[1]-tZmid[1]),(tZA[0]-tZmid[0]))) | |
# 2d A and Mid Y | |
tYA=th(A,1) | |
tYmid=th(mid,1) | |
bpy.context.object.rotation_euler[1] = math.atan(safeDiv((tYA[1]-tYmid[1]),((tZA-tZmid).length))) | |
bpy.context.view_layer.update() | |
if((bpy.context.object.matrix_world@bpy.context.object.data.vertices[0].co-A).length>0.1): | |
bpy.context.object.rotation_euler[1]*=-1 | |
if(edge.index%10==0): | |
print("Finished "+str(math.floor(edge.index/len(edges)*100))+"%.") | |
print("Everything finished, no errors.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couple suggestions:
A
orB
is.print
to have a\r
andend=""
instead of making a newline for every progress.safeDiv
could be improved or entirely removed.