Skip to content

Commit

Permalink
Add Add Center of Gravity feature
Browse files Browse the repository at this point in the history
  • Loading branch information
UuuNyaa committed Aug 4, 2022
1 parent a0fe42a commit b7feb2e
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 313 deletions.
2 changes: 1 addition & 1 deletion mmd_uuunyaa_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'name': 'mmd_uuunyaa_tools',
'description': 'Utility tools for MMD model & scene editing by Uuu(/>ω<)/Nyaa!.',
'author': 'UuuNyaa',
'version': (1, 4, 0),
'version': (1, 5, 0),
'blender': (2, 83, 0),
'warning': '',
'location': 'View3D > Sidebar > MMD Tools Panel',
Expand Down
104 changes: 0 additions & 104 deletions mmd_uuunyaa_tools/converters/physics/panels.py

This file was deleted.

58 changes: 58 additions & 0 deletions mmd_uuunyaa_tools/generators/physics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
# Copyright 2022 UuuNyaa <[email protected]>
# This file is part of MMD UuuNyaa Tools.

from typing import List

import bpy
from mmd_uuunyaa_tools.m17n import _


class AddCenterOfGravityObject(bpy.types.Operator):
bl_idname = 'mmd_uuunyaa_tools.add_center_of_gravity_object'
bl_label = _('Add Center of Gravity')
bl_options = {'REGISTER', 'UNDO'}

radius: bpy.props.FloatProperty(name=_('Radius'), default=0.1, min=0.0, precision=4, unit='LENGTH')

@classmethod
def poll(cls, context: bpy.types.Context):
return context.mode == 'POSE' and len(context.selected_pose_bones) > 0

def execute(self, context: bpy.types.Context):
armature_object: bpy.types.Object = context.active_object
selected_pose_bones: List[bpy.types.PoseBone] = context.selected_pose_bones

center_object: bpy.types.Object = bpy.data.objects.new('EMPTY', None)
center_object.name = 'CenterOfGravity'
center_object.empty_display_type = 'PLAIN_AXES'
center_object.empty_display_size = self.radius
center_object.show_in_front = True
context.view_layer.active_layer_collection.collection.objects.link(center_object)

total_bone_length = sum([b.length for b in selected_pose_bones])

for pose_bone in selected_pose_bones:
constraint: bpy.types.CopyLocationConstraint = center_object.constraints.new('COPY_LOCATION')
constraint.name = f'CenterOfGravity.{pose_bone.name}'
constraint.target = armature_object
constraint.subtarget = pose_bone.name
constraint.head_tail = 0.5
constraint.use_offset = True
constraint.influence = pose_bone.length / total_bone_length

floor_object: bpy.types.Object = bpy.data.objects.new('EMPTY', None)
floor_object.name = 'CenterOfGravityOnFloor'
floor_object.parent = center_object
floor_object.empty_display_type = 'SPHERE'
floor_object.empty_display_size = self.radius
floor_object.show_in_front = True
context.view_layer.active_layer_collection.collection.objects.link(floor_object)

constraint: bpy.types.CopyLocationConstraint = floor_object.constraints.new('COPY_LOCATION')
constraint.target = armature_object
constraint.use_x = False
constraint.use_y = False
constraint.use_z = True

return {'FINISHED'}
Loading

0 comments on commit b7feb2e

Please sign in to comment.