Advertisement
cfox04

dmodel functions

Aug 15th, 2022
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1.     def create_all_drawable_models(self):
  2.         """Create all drawable models and parent them to the drawable."""
  3.         if not self.materials:
  4.             self.create_materials()
  5.  
  6.         for models_group, lod_level in zip(self.cwxml.drawable_model_groups, list(LODLevel)):
  7.             for model_cwxml in models_group:
  8.                 self.create_drawable_model(model_cwxml, lod_level)
  9.                 # armature = None
  10.                 # if self.cwxml.has_skeleton() and not self.uses_external_skeleton:
  11.                 #     armature = self.bpy_object
  12.  
  13.                 # model_converter = ModelCWXMLConverter(
  14.                 #     model_cwxml, lod_level, self.materials, armature, self.bones_cwxml)
  15.  
  16.                 # model = model_converter.create_bpy_object()
  17.                 # model.parent = self.bpy_object
  18.  
  19.     def create_materials(self):
  20.         """Create all materials from this drawable's cwxml shader group."""
  21.         for shader_cwxml in self.cwxml.shader_group.shaders:
  22.             material = ShaderCWXMLConverter(
  23.                 shader_cwxml, self.filepath, self.cwxml.shader_group.texture_dictionary).create_bpy_object()
  24.             self.materials.append(material)
  25.  
  26.     def create_drawable_model(self, model_cwxml: ydrxml.DrawableModelItem, lod_level: int):
  27.         geometry_objs = []
  28.  
  29.         for geometry_cwxml in model_cwxml.geometries:
  30.             name = SOLLUMZ_UI_NAMES[SollumType.DRAWABLE_MODEL]
  31.             geometry = GeometryCWXMLConverter(
  32.                 geometry_cwxml, self.materials).create_bpy_object(name, self.bones)
  33.  
  34.             geometry_objs.append(geometry)
  35.  
  36.         model_object = join_objects(geometry_objs)
  37.  
  38.         model_object.drawable_model_properties.sollum_lod = lod_level
  39.         model_object.drawable_model_properties.render_mask = model_cwxml.render_mask
  40.         model_object.drawable_model_properties.unknown_1 = model_cwxml.unknown_1
  41.         model_object.drawable_model_properties.flags = model_cwxml.flags
  42.  
  43.         if self.cwxml.has_skeleton() and not self.uses_external_skeleton:
  44.             self.parent_drawable_model_bones(
  45.                 model_object, model_cwxml.bone_index)
  46.  
  47.     def parent_drawable_model_bones(self, model_object: bpy.types.Object, bone_index: int):
  48.         """Set drawable model parent bone based on its bone index."""
  49.         armature = self.bpy_object
  50.  
  51.         if armature.type != "ARMATURE":
  52.             return
  53.  
  54.         parent_bone_name = None
  55.         has_bone_translation = False
  56.  
  57.         if len(armature.pose.bones) > bone_index:
  58.             parent_bone_name = armature.pose.bones[bone_index].name
  59.             translation: Vector = self.bones_cwxml[bone_index].translation
  60.  
  61.             if translation is not None and translation.magnitude > 0:
  62.                 has_bone_translation = True
  63.  
  64.         if parent_bone_name is not None:
  65.             # Preserve transforms after parenting
  66.             original_world_mat = model_object.matrix_world.copy()
  67.  
  68.             model_object.parent_type = "BONE"
  69.             model_object.parent_bone = parent_bone_name
  70.  
  71.             if has_bone_translation is False:
  72.                 model_object.matrix_world = original_world_mat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement