From dc612a18b60341b233fcac00f99405d98b61c01a Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 8 Aug 2025 15:03:57 -0400 Subject: small stuff on collada / OBJ exporter for KCL / importer for NeoKCLCreate OBJs --- obj_kcl_export.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 obj_kcl_export.py (limited to 'obj_kcl_export.py') diff --git a/obj_kcl_export.py b/obj_kcl_export.py new file mode 100644 index 0000000..b106869 --- /dev/null +++ b/obj_kcl_export.py @@ -0,0 +1,73 @@ +import bpy, math +from . import blender_funcs + +# just a simple OBJ exporter wrapper +# it only selects all the meshes inside an armature and sets some OBJ exporter variables + +# write_kcl_obj function +# function to write custom obj file for kcl conversion +def write_kcl_obj(context, filepath): + + # this thing is always needed for stuff + scene = bpy.context.scene + + # if nothing is selected end the exporter + if (scene.objects.active == None or scene.objects.active.type != 'ARMATURE'): + if (scene.objects.active == None): + blender_funcs.disp_msg("No Armature selected. Select one and try again.") + else: + blender_funcs.disp_msg("No Armature selected. Currently selecting: \"%s\"" + % (scene.objects.active.name)) + return {'FINISHED'} + + # get armature object + armature = scene.objects.active + print("\nArmature found: \"%s\"" % (armature.name)) + # change to object mode + bpy.ops.object.mode_set(mode='OBJECT') + + # check if the armature contains only mesh objects inside + for obj in armature.children: + if (obj.type != 'MESH'): + blender_funcs.disp_msg("\"%s\": contains non-mesh object (%s)." + % (armature.name, obj.name)) + return {'FINISHED'} + + # select all the meshes inside the armature + blender_funcs.select_obj(scene, armature, True) + + # export the object + bpy.ops.export_scene.obj(filepath = filepath, axis_up = "Y", axis_forward = "-Z", + use_selection = True, use_triangles = True, global_scale = 100, + use_materials = True, use_normals = True) + + # done! + blender_funcs.select_obj(scene, armature, False) + blender_funcs.disp_msg("Meshes from \"%s\" exported!" % (armature.name)) + return {'FINISHED'} + +# Stuff down is for the menu appending +# of the exporter to work plus some setting stuff +# comes from a Blender importer template +from bpy_extras.io_utils import ExportHelper +from bpy.props import StringProperty, BoolProperty, EnumProperty +from bpy.types import Operator + +class export_obj_kcl(Operator, ExportHelper): + """Export a OBJ file for KCL conversion""" + bl_idname = "export_scene.obj_kcl" + bl_label = "Export OBJ for KCL (.OBJ)" + filename_ext = ".obj" + filter_glob = StringProperty(default = "*.obj", options = {'HIDDEN'}, maxlen = 255) + def execute(self, context): + return write_kcl_obj(context, self.filepath) + +# Only needed if you want to add into a dynamic menu +def menu_export_obj_kcl(self, context): + self.layout.operator(export_obj_kcl.bl_idname, text="OBJ for KCL (.obj)") + +bpy.utils.register_class(export_obj_kcl) +bpy.types.INFO_MT_file_export.append(menu_export_obj_kcl) + +# test call +bpy.ops.export_scene.obj_kcl('INVOKE_DEFAULT') -- cgit v1.2.3-70-g09d2