Skip to content

Instantly share code, notes, and snippets.

@Pentan
Created August 24, 2012 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pentan/3455286 to your computer and use it in GitHub Desktop.
Save Pentan/3455286 to your computer and use it in GitHub Desktop.
Blender's script to create Envelope display type Armature like mesh.
import bpy
import math
import mathutils
from mathutils import Vector
from mathutils import Matrix
for selobj in bpy.context.selected_objects:
#print("{} is {}".format(selobj.name, selobj.type))
if selobj.type == 'ARMATURE':
arm = selobj.data
# print("Armature {} contains {} bones".format(arm.name, len(arm.bones)))
if len(arm.bones) <= 0:
print("Armature {} has no bones. skip it.".format(arm.name))
continue
verts = []
faces = []
vgroups = []
for abone in arm.bones:
"""
print("{} length:{}".format(abone.name, abone.length))
# from parent bone
#print(" head(loc:{}, r:{}".format(abone.head, abone.head_radius))
#print(" tain(loc:{}, r:{}".format(abone.tail, abone.tail_radius))
#print(" matrix:\n{}".format(abone.matrix))
# from armature
print(" head(loc:{}, r:{}".format(abone.head_local, abone.head_radius))
print(" tain(loc:{}, r:{}".format(abone.tail_local, abone.tail_radius))
print(" matrix_local:\n{}".format(abone.matrix_local))
"""
head_r = abone.head_radius
tail_r = abone.tail_radius
bone_len = abone.length
tmpverts = [
Vector((-head_r, -head_r, head_r)),
Vector(( head_r, -head_r, head_r)),
Vector(( head_r, -head_r,-head_r)),
Vector((-head_r, -head_r,-head_r)),
Vector((-head_r, head_r, head_r)),
Vector(( head_r, head_r, head_r)),
Vector(( head_r, head_r,-head_r)),
Vector((-head_r, head_r,-head_r)),
Vector((-tail_r, bone_len-tail_r, tail_r)),
Vector(( tail_r, bone_len-tail_r, tail_r)),
Vector(( tail_r, bone_len-tail_r,-tail_r)),
Vector((-tail_r, bone_len-tail_r,-tail_r)),
Vector((-tail_r, bone_len+tail_r, tail_r)),
Vector(( tail_r, bone_len+tail_r, tail_r)),
Vector(( tail_r, bone_len+tail_r,-tail_r)),
Vector((-tail_r, bone_len+tail_r,-tail_r))
]
tmpfaces = [
(3, 2, 1, 0),
(0, 1, 5, 4),
(1, 2, 6, 5),
(2, 3, 7, 6),
(3, 0, 4, 7),
(4, 5, 9, 8),
(5, 6, 10, 9),
(6, 7, 11, 10),
(7, 4, 8, 11),
( 8, 9, 13, 12),
( 9, 10, 14, 13),
(10, 11, 15, 14),
(11, 8, 12, 15),
(12, 13, 14, 15)
]
voffset = len(verts)
tmpvgrp = {'name':abone.name, 'verts':[]}
for i, v in enumerate(tmpverts):
verts.append(abone.matrix_local * v)
tmpvgrp['verts'].append(i + voffset)
vgroups.append(tmpvgrp)
for f in tmpfaces:
faces.append((f[0]+voffset, f[1]+voffset, f[2]+voffset, f[3]+voffset))
#print(verts)
#print(faces)
#print(vgroups)
# create meshes
newname = arm.name + "_mesh"
newmesh = bpy.data.meshes.new(newname)
newobj = bpy.data.objects.new(newname, newmesh)
newobj.matrix_world = selobj.matrix_world
bpy.context.scene.objects.link(newobj)
newmesh.from_pydata(verts, [], faces)
newmesh.update(calc_edges=True)
for vg in vgroups:
newvg = newobj.vertex_groups.new(vg['name'])
for vid in vg['verts']:
newvg.add([vid], 1.0, 'REPLACE')
submod = newobj.modifiers.new('Subsurf', 'SUBSURF')
armmod = newobj.modifiers.new(arm.name, 'ARMATURE')
armmod.object = selobj
armmod.use_bone_envelopes = False
armmod.use_vertex_groups = True
arm.draw_type = 'OCTAHEDRAL'
selobj.show_x_ray = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment