whole
[carveJwlIkooP6JGAAIwe30JlM.git] / blender_export.py
index 04fd6c344b69245f172dc139801d24196f5c1b04..acd28ec3bcfcadbd96bc17cc539bd1508a78f86d 100644 (file)
@@ -205,6 +205,9 @@ def write_model(collection_name):
    def emplace_material( mat ):
       nonlocal material_cache, material_buffer
 
+      if mat == None:
+         return 0
+
       if mat.name in material_cache:
          return material_cache[mat.name]
 
@@ -395,7 +398,9 @@ def write_model(collection_name):
          can_use_cache = True
 
          for mod in obj.modifiers:
-            if mod.type == 'DATA_TRANSFER' or mod.type == 'SHRINKWRAP':
+            if mod.type == 'DATA_TRANSFER' or mod.type == 'SHRINKWRAP' or \
+               mod.type == 'BOOLEAN' or mod.type == 'CURVE' or \
+               mod.type == 'ARRAY':
                can_use_cache = False
 
             if mod.type == 'ARMATURE':
@@ -471,7 +476,11 @@ def write_model(collection_name):
                   # WEight groups
                   #
                   if armature_def:
-                     weight_groups = sorted( data.vertices[vi].groups, key = \
+                     src_groups = [_ for _ in data.vertices[vi].groups \
+                                 if obj.vertex_groups[_.group].name in \
+                                    armature_def['bones']]
+
+                     weight_groups = sorted( src_groups, key = \
                                              lambda a: a.weight, reverse=True )
                      tot = 0.0
                      for ml in range(3):
@@ -653,12 +662,25 @@ def write_model(collection_name):
                         for pb in armature.pose.bones:
                            if pb.name == bone_name:
                               rb = armature.data.bones[ bone_name ]
-
-                              loc, rot, sca = pb.matrix_basis.decompose()
+                              
+                              # relative bone matrix
+                              if rb.parent is not None:
+                                 offset_mtx = rb.parent.matrix_local
+                                 offset_mtx = offset_mtx.inverted_safe() @ \
+                                              rb.matrix_local
+
+                                 inv_parent = pb.parent.matrix @ offset_mtx
+                                 inv_parent.invert_safe()
+                                 fpm = inv_parent @ pb.matrix 
+                              else:
+                                 bone_mtx = rb.matrix.to_4x4()
+                                 local_inv = rb.matrix_local.inverted_safe()
+                                 fpm = bone_mtx @ local_inv @ pb.matrix
+
+                              loc, rot, sca = fpm.decompose()
 
                               # local position
-                              vp = rb.matrix @ loc
-                              final_pos = Vector(( vp[0], vp[2], -vp[1] ))
+                              final_pos = Vector(( loc[0], loc[2], -loc[1] ))
 
                               # rotation
                               lc_m = pb.matrix_channel.to_3x3()
@@ -1011,7 +1033,7 @@ def cv_draw():
    for obj in bpy.context.collection.objects:
       if obj.type == 'ARMATURE':
          for bone in obj.data.bones:
-            if bone.cv_data.collider:
+            if bone.cv_data.collider and obj.data.pose_position == 'REST':
                c = bone.head_local
                a = bone.cv_data.v0
                b = bone.cv_data.v1
@@ -1336,17 +1358,10 @@ class CV_BONE_SETTINGS(bpy.types.PropertyGroup):
    v0: bpy.props.FloatVectorProperty(name="v0",size=3)
    v1: bpy.props.FloatVectorProperty(name="v1",size=3)
 
+   con0: bpy.props.BoolProperty(name="Constriant 0",default=False)
    mins: bpy.props.FloatVectorProperty(name="mins",size=3)
    maxs: bpy.props.FloatVectorProperty(name="maxs",size=3)
 
-   con0: bpy.props.BoolProperty(name="Constriant 0",default=False)
-   c0: bpy.props.FloatVectorProperty(name="dir",size=3)
-   s0: bpy.props.FloatVectorProperty(name="limits",size=3)
-
-   con1: bpy.props.BoolProperty(name="Constriant 1",default=False)
-   c1: bpy.props.FloatVectorProperty(name="dir",size=3)
-   s1: bpy.props.FloatVectorProperty(name="limits",size=3)
-
 class CV_BONE_PANEL(bpy.types.Panel):
    bl_label="Bone Config"
    bl_idname="SCENE_PT_cv_bone"
@@ -1366,16 +1381,9 @@ class CV_BONE_PANEL(bpy.types.Panel):
       _.layout.prop( bone.cv_data, "v1" )
 
       _.layout.label( text="Angle Limits" )
+      _.layout.prop( bone.cv_data, "con0" )
       _.layout.prop( bone.cv_data, "mins" )
       _.layout.prop( bone.cv_data, "maxs" )
-      
-      _.layout.prop( bone.cv_data, "con0" )
-      _.layout.prop( bone.cv_data, "c0" )
-      _.layout.prop( bone.cv_data, "s0" )
-
-      _.layout.prop( bone.cv_data, "con1" )
-      _.layout.prop( bone.cv_data, "c1" )
-      _.layout.prop( bone.cv_data, "s1" )
 
 class CV_SCENE_SETTINGS(bpy.types.PropertyGroup):
    use_hidden: bpy.props.BoolProperty( name="use hidden", default=False )