Good day, all!
I'm trying to create very simple addon that will makes my everyday life a bit more convenient. It applies all transformation of selected object, deleting all material slots and export two versions of selected object - triangulated and quad version.
Here's my code:
import bpy
import os
basedir = os.path.dirname(bpy.data.filepath)
selection = bpy.context.selected_objects
# if not basedir:
# raise Exception("Blend file is not saved")
view_layer = bpy.context.view_layer
obj_active = view_layer.objects.active
selection = bpy.context.selected_objects
def main(context):
for obj in selection:
obj.data.materials.clear()
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
bpy.ops.object.modifier_add(type='TRIANGULATE')
bpy.context.object.modifiers["Triangulate"].keep_custom_normals = True
obj.select_set(True)
# some exporters only use the active object
view_layer.objects.active = obj
name = bpy.path.clean_name(obj.name)
fn = os.path.join(basedir, name)
path = "D:/SVN/Characters/Models/"
bpy.ops.export_scene.fbx(filepath=path + name + ".fbx", use_selection=True)
bpy.ops.object.modifier_remove(modifier="Triangulate")
print("Triangulated mesh has been written:", fn)
bpy.ops.export_scene.fbx(filepath=path + name + "_quad.fbx", use_selection=True)
print("Quad mesh has been written:", fn)
obj.select_set(False)
view_layer.objects.active = obj_active
for obj in selection:
obj.select_set(True)
class SaveTrisAndQuad(bpy.types.Operator):
"""Export quad and triangulated meshes, apply transforms and delete materials"""
bl_idname = "object.savetrisandquad"
bl_label = "Save tiangulated and Quad mesh"
def execute(self, context):
main(context)
return {'FINISHED'}
# Creating Panel ======================================
class RedNeckPanel(bpy.types.Panel):
bl_label = "RedNeck Tools"
bl_idname = "OBJECT_PT_redneck"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "RedNeck"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.operator(SaveTrisAndQuad.bl_idname, text="Export Mesh", icon='EXPORT')
# Creating Panel ======================================
from bpy.utils import register_class, unregister_class
_classes = [
SaveTrisAndQuad,
RedNeckPanel
]
def register():
# bpy.utils.register_class(SaveTrisAndQuad)
for cls in _classes:
register_class(cls)
def unregister():
# bpy.utils.unregister_class(SaveTrisAndQuad)
for cls in _classes:
unregister_class(cls)
if __name__ == "__main__":
register()
It works just fine, but the only problem is that "path" variable is hard coded. How can I add browse button to be able to choose a path to save file to and how can I set a "path" variable from this selection?
Can someone please point me in right direction. I've tried to google it, but can't really find what I need and blender API documentation is kinda confusing.
Thanks.