def get_bone_name(l, index): if index==-1: return l.bones[0].name.decode('cp932') if index < len(l.bones): name=englishmap.getEnglishBoneName(l.bones[index].name.decode('cp932')) if name: return name return l.bones[index].name.decode('cp932') print('invalid bone index', index) return l.bones[0].name.decode('cp932')
def CreateBone(armature, b): name=englishmap.getEnglishBoneName(b.name.decode('cp932')) if not name: name=b.name.decode('cp932') # bone生成 bone=bl.armature.createBone(armature, name) bone.head = bl.createVector(*convert_coord(b.pos)) if b.type==pmd.Bone.ROTATE_MOVE: bone[bl.BONE_CAN_TRANSLATE]=True # armature layer 2 #bl.bone.setLayerMask(bone, [0, 1]) return bone
def __importRigidBodies(io): print("create rigid bodies") container=bl.object.createEmpty('RigidBodies') layers=[ True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, ] material=bl.material.create('rigidBody') rigidMeshes=[] for i, rigid in enumerate(io.rigidbodies): if rigid.bone_index==-1: # no reference bone bone=io.bones[0] else: bone=io.bones[rigid.bone_index] pos=bone.pos+rigid.shape_position # x, y, z -> x, z, y if rigid.shape_type==pmd.SHAPE_SPHERE: bpy.ops.mesh.primitive_ico_sphere_add( location=(pos.x, pos.z, pos.y), layers=layers ) w, h, d=rigid.shape_size.to_a() bpy.ops.transform.resize( value=[w, w, w]) elif rigid.shape_type==pmd.SHAPE_BOX: bpy.ops.mesh.primitive_cube_add( location=(pos.x, pos.z, pos.y), layers=layers ) w, h, d=rigid.shape_size.to_a() bpy.ops.transform.resize( value=[w, d, h]) elif rigid.shape_type==pmd.SHAPE_CAPSULE: bpy.ops.mesh.primitive_cylinder_add( location=(pos.x, pos.z, pos.y), layers=layers ) w, h, d=rigid.shape_size.to_a() bpy.ops.transform.resize( value=[w, w, h]) else: assert(False) meshObject=bl.object.getActive() mesh=bl.object.getData(meshObject) rigidMeshes.append(meshObject) bl.mesh.addMaterial(mesh, material) meshObject.name='r_%03d' % i meshObject[bl.RIGID_NAME]=rigid.name.decode('cp932') #meshObject.draw_transparent=True #meshObject.draw_wire=True meshObject.draw_type='WIRE' rot=rigid.shape_rotation meshObject.rotation_euler=(-rot.x, -rot.z, -rot.y) # custom properties meshObject[bl.RIGID_SHAPE_TYPE]=rigid.shape_type meshObject[bl.RIGID_PROCESS_TYPE]=rigid.mode bone_name = englishmap.getEnglishBoneName(bone.name.decode('cp932')) if not bone_name: bone_name=bone.name.decode('cp932') meshObject[bl.RIGID_BONE_NAME]=bone_name meshObject[bl.RIGID_GROUP]=rigid.collision_group meshObject[bl.RIGID_INTERSECTION_GROUP]=rigid.no_collision_group meshObject[bl.RIGID_WEIGHT]=rigid.mass meshObject[bl.RIGID_LINEAR_DAMPING]=rigid.linear_damping meshObject[bl.RIGID_ANGULAR_DAMPING]=rigid.angular_damping meshObject[bl.RIGID_RESTITUTION]=rigid.restitution meshObject[bl.RIGID_FRICTION]=rigid.friction for meshObject in reversed(rigidMeshes): bl.object.makeParent(container, meshObject) return container
def __importArmature(l): # create new armature armature, armature_object=bl.armature.create() # bone生成 bl.armature.makeEditable(armature_object) def CreateBone(armature, b): name=englishmap.getEnglishBoneName(b.name.decode('cp932')) if not name: name=b.name.decode('cp932') # bone生成 bone=bl.armature.createBone(armature, name) bone.head = bl.createVector(*convert_coord(b.pos)) if b.type==pmd.Bone.ROTATE_MOVE: bone[bl.BONE_CAN_TRANSLATE]=True # armature layer 2 #bl.bone.setLayerMask(bone, [0, 1]) return bone bl_bones=[CreateBone(armature, b) for b in l.bones] # build skeleton for b, bone in zip(l.bones, bl_bones): if b.parent_index!=0xFFFF: bone.parent=bl_bones[b.parent_index] if (b.tail_index!=0 and b.tail_index!=0xFFFF and b.type!=pmd.Bone.TWEAK ): bone.tail=bl_bones[b.tail_index].head else: bone.tail=bone.head+bl.createVector(0, 0.01, 0) # connect bones for bone in bl_bones: if bone.parent: if bone.parent.tail==bone.head: bl.bone.setConnected(bone) bl.armature.update(armature) bl.enterObjectMode() pose = bl.object.getPose(armature_object) print('pose.bones', len(pose.bones)) # pose params for b in l.bones: name=englishmap.getEnglishBoneName(b.name.decode('cp932')) if not name: name=b.name.decode('cp932') if not name in pose.bones: print("%s is not found !!" % name) continue p_bone=pose.bones[name] if b.parent_index!=0xFFFF: parent_b=l.bones[b.parent_index] if parent_b.tail_index==b.index: # 移動制限を尻尾位置の接続フラグに流用する bl.constraint.addLimitTranslateion(p_bone) elif parent_b.parent_index!=0xFFFF: parent_parent_b=l.bones[parent_b.parent_index] if parent_parent_b.tail_index==b.index: # 移動制限を尻尾位置の接続フラグに流用する bl.constraint.addLimitTranslateion(p_bone) if b.type==pmd.Bone.ROTATE_INFL: # 回転影響下 target_b=l.bones[b.ik_index] name=englishmap.getEnglishBoneName(target_b.name.decode('cp932')) if not name: name=target_b.name.decode('cp932') constraint_p_bone=pose.bones[name] bl.constraint.addCopyRotation(p_bone, armature_object, constraint_p_bone, 1.0) if b.type==pmd.Bone.ROLLING: # 軸固定 bl.constraint.addLimitRotation(p_bone) if b.type==pmd.Bone.TWEAK: # 回転連動 target_b=l.bones[b.tail_index] name=englishmap.getEnglishBoneName(target_b.name.decode('cp932')) if not name: name=target_b.name.decode('cp932') constraint_p_bone=pose.bones[name] bl.constraint.addCopyRotation(p_bone, armature_object, constraint_p_bone, b.ik_index * 0.01) # IK constraint for ik in l.ik_list: target=l.bones[ik.target] name = englishmap.getEnglishBoneName(target.name.decode('cp932')) if not name: name=target.name.decode('cp932') p_bone = pose.bones[name] if not p_bone: print('not found', name) continue if len(ik.children) >= 16: print('over MAX_CHAINLEN', ik, len(ik.children)) continue # IK effector effector_name=englishmap.getEnglishBoneName( l.bones[ik.index].name.decode('cp932')) if not effector_name: effector_name=l.bones[ik.index].name.decode('cp932') bl.constraint.addIk(p_bone, armature_object, effector_name, ik.children, ik.weight, ik.iterations) effector_bone=pose.bones[effector_name].bone effector_bone[bl.IK_UNITRADIAN]=ik.weight bl.armature.makeEditable(armature_object) bl.armature.update(armature) bl.enterObjectMode() # fix boneNameMap={} for b in l.bones: name=englishmap.getEnglishBoneName(b.name.decode('cp932')) if not name: name=b.name.decode('cp932') boneNameMap[name]=b for b in armature.bones.values(): if boneNameMap[b.name].type==pmd.Bone.UNVISIBLE: b.hide=True # create bone group for i, g in enumerate(l.bone_group_list): name=get_group_name(g.name) bl.object.createBoneGroup(armature_object, name, "THEME%02d" % (i+1)) # assign bone to group for b_index, g_index in l.bone_display_list: # bone b=l.bones[b_index] bone_name=englishmap.getEnglishBoneName(b.name.decode('cp932')) if not bone_name: bone_name=b.name.decode('cp932') # group g=l.bone_group_list[g_index-1] group_name=get_group_name(g.name) # assign pose.bones[bone_name].bone_group=pose.bone_groups[group_name] bl.enterObjectMode() return armature_object