def symmetrize_armature(obj=None, cutoff=.001, scale=.5): ac = set_ac(obj) mode = set_mode("EDIT") ebones = obj.data.edit_bones bones = obj xcos = tuple(b.head[0] for b in ebones) l = tuple(ebones[i] for i in get_side(coords=xcos)) r = tuple(ebones[i] for i in get_side(coords=xcos, cmp_func=lambda x, y: x < y)) n_r = mirror_arm_side(bones=l, side_tag="_R", scale=scale) n_l = mirror_arm_side(bones=r, side_tag="_L", scale=scale) for b in r: b.name += "_R" for b in l: b.name += "_L" res_r = (tuple(b.name for b in r), tuple(b.name for b in n_r)) res_l = (tuple(b.name for b in l), tuple(b.name for b in n_l)) set_mode(mode) set_ac(ac) return res_r, res_l
def set_minimum_envelopes(obj = None): ac = set_ac(obj) mode = set_mode("EDIT") for b in obj.data.edit_bones: b.envelope_distance = 0 b.head_radius = .0001 b.tail_radius = .0001 set_mode(mode) set_ac(ac)
def ordered_bone_parent(bone_names,obj = None): ac = set_ac(obj) mode = set_mode("EDIT") ebones = obj.data.edit_bones if bone_names[0] != None: ebones[bone_names[1]].parent = ebones[bone_names[0]] if len(bone_names) > 2: for b in range(2,len(bone_names)): bname,parent_name = (bone_names[b],bone_names[b-1]) ebones[bname].parent = ebones[parent_name] set_mode(mode) set_ac(ac)
def create_modified_duplicate2(apply_count=0, obj_name="modified", mesh_mod_clear=False, shape_apply=True, obj=None): ac = set_ac(obj) modified = obj.copy() if mesh_mod_clear: rem_types = ["MIRROR"] for m in modified.modifiers[:][::-1]: if (not m.show_viewport) or (m.type in rem_types): print(m) modified.modifiers.remove(m) if shape_apply: bpy.ops.object.shape_key_add(from_mix=True) for kidx, k in enumerate(modified.data.shape_keys.key_blocks[-1].data): modified.data.vertices[kidx].co = k.co modified.name = obj_name modmesh = modified.data.name modmeshdup = bpy.data.meshes[modmesh].copy() modified.data = modmeshdup modkeys = None try: modkeys = modified.data.shape_keys.key_blocks except: pass modmods = modified.modifiers bpy.context.scene.objects.link(modified) bpy.context.scene.objects.active = modified bpy.context.scene.update() oselect = obj.select modified.select = True obj.select = False bpy.context.scene.objects.active = modified if modkeys: bpy.ops.object.shape_key_remove(all=True) nummods = len(modmods) for m in range(nummods): ''' #if apply_count > 0: #if m > apply_count: #break ''' bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modmods[0].name) obj.select = oselect set_ac(ac) return modified
def envelope_armature_settings(envelope_distance=.25, envelope_weight=1.0, head_radius=0, tail_radius=0, obj=None): prop_dict = kwargize(head_radius=head_radius, tail_radius=tail_radius, envelope_distance=envelope_distance, envelope_weight=envelope_weight) ac = set_ac(obj) mode = set_mode("EDIT") ebones = obj.data.edit_bones tmap(lambda b: prop_copy(b, prop_dict), ebones) set_mode(mode) set_ac(ac)
def bone_trans(matrix,obj = None,bone_names = None,scale = .5): ac = set_ac(obj) mode = set_mode("EDIT") ebones = obj.data.edit_bones if bone_names: target_bones = tuple(ebones[bn] for bn in bone_names) else: target_bones = tuple(ebones) m3 = matrix.to_3x3() mt = matrix.to_translation() anymap(lambda b: b.head.rotate(matrix),target_bones) anymap(lambda b: b.tail.rotate(matrix),target_bones) anymap(lambda b: setattr(b,"head",mt+b.head),target_bones) anymap(lambda b: setattr(b,"tail",Vector((0,1,0))*scale+b.head),target_bones) set_mode(mode) set_ac(ac)
def make_bone(name = "Bone",obj = None,autoswitch = True,loc = None,dir = None,scale = .05,parent = None,props = {}): if loc == None: loc = (0,0,0) if dir == None: dir = (0,1,0) if autoswitch: ac = set_ac(obj) mode = set_mode("EDIT") ebones = obj.data.edit_bones newbone = init_new_bone(ebones) newbone.name = name newbone.head = loc newbone.tail = Vector(loc) + Vector(dir)*scale if parent != None: newbone.parent = parent prop_copy(newbone,props) if autoswitch: set_mode(mode) set_ac(ac) return newbone
def simple_hair_curve(split_bevel=True, curve_type="n", bevel_curve_type="p", use_existing_bevel=False, bevel_obj_name="Bevel_curve", use_cyclic_u=True, use_fill_caps=True, zero_last_radius=True): objs = bpy.context.scene.objects curve_type_dict = { "NURBS": "NURBS", "POLY": "POLY", "BEZIER": "BEZIER", "n": "NURBS", "p": "POLY", "b": "BEZIER" } nc = gtls.make_obj(type="CURVE", name="Basis_curve") prop_dict = { "data.dimensions": "3D", "data.use_uv_as_generated": True, "data.use_stretch": True, "data.use_deform_bounds": True } nc.data.dimensions = "3D" nc.data.use_uv_as_generated = True nc.data.use_stretch = True nc.data.use_deform_bounds = True nc.data.use_fill_caps = use_fill_caps nspl = nc.data.splines.new(type=curve_type_dict[curve_type]) nspl.use_endpoint_u = True nc.location = bpy.context.scene.cursor_location set_ac(nc) nc.select = True nspl.points.add(4) for x in range(5): nspl.points[x].co = (x, 0, 0, 1) nspl.order_u = 5 if use_existing_bevel: nc2 = objs[bevel_obj_name] else: nc2 = gtls.make_obj(type="CURVE", name="Bevel_curve") nc2.data.dimensions = "3D" nc2.data.use_uv_as_generated = True nc2.data.use_stretch = True nc2.data.use_deform_bounds = True nspl2 = nc2.data.splines.new(type=curve_type_dict[bevel_curve_type]) nspl2.use_endpoint_u = True if split_bevel: nspl2.points.add(1) nspl3 = nc2.data.splines.new(type="POLY") nspl3.use_endpoint_u = True nspl3.points.add(1) for x in range(2): nspl2.points[x].co = (x, -x, 0, 1) for x in range(2): nspl3.points[x].co = (-x, -x, 0, 1) else: nspl2.points.add(2) for x in range(3): nspl2.points[x].co = (x - 1, -abs(1 - x), 0, 1) nc.data.bevel_object = nc2 if zero_last_radius: for s in nc.data.splines: s.points[-1].radius = 0 for s in nc2.data.splines: s.use_cyclic_u = use_cyclic_u return (nc, nc2)
def curve_to_armature(obj=None, base_name="Bone", make_new_arm=True, arm=None, do_armature=True, scale=.5, do_symmetrize=False, obj_trans_copy=True, bone_props=None, root_bone=None, use_apply_on_spline=False, align_bones=False, do_bone_parent=True): if bone_props == None: bone_props = {} if make_new_arm: arm = gtls.make_arm(name="arm_to_curve_" + obj.name) else: if arm == None: arm = get_sel_obj() for i in rlen(arm.layers): arm.layers[i] = True arm.data.draw_type = "STICK" mods = obj.modifiers splines = tuple(s.bezier_points if s.type == "BEZIER" else s.points for s in obj.data.splines) ac = set_ac(arm) res = tuple( map( lambda verts: tuple( map( lambda v: bone_fs.make_bone(name=base_name + str(verts[0]), scale=scale, obj=arm, loc=v.co[0:3], props=bone_props), verts[1])), enumerate(splines))) res = unpack(res) res = list(b.name for b in res) res.sort() #rootボーンが指定された場合、チュープルの前に置いとく if root_bone == None: bone_names = res else: bone_names = (root_bone, *res) if do_armature: mod = mods.new(name="Curve_to_Armature", type="ARMATURE") props = { "use_vertex_groups": not use_apply_on_spline, "use_bone_envelopes": use_apply_on_spline, "object": arm, "show_in_editmode": True, "use_apply_on_spline": use_apply_on_spline, } prop_copy(mod, props) if do_symmetrize: armature_fs.symmetrize_armature(obj=arm) curve_fs.symmetrize_spline(obj=obj) if obj_trans_copy: bone_fs.bone_trans(obj.matrix_world, bone_names=bone_names, obj=arm, scale=scale) mode = set_mode("EDIT") ebones = arm.data.edit_bones c = 0 for s in rlen(splines): c += 0 ls = len(splines[s]) if do_bone_parent: bone_fs.ordered_bone_parent(bone_names[c:(c + ls)], obj=arm) if align_bones: for b in range(ls - 1): b1 = ebones[bone_names[c]] b2 = ebones[bone_names[c + 1]] b1.tail = b2.head c += 1 c += 1 set_mode(mode) set_ac(ac) return arm