Hello everyone,
I just wrote this simple script that write a new .blend file with selected/all objects into another folder, it’s aim is to create a copy of the current, opened .blend file into a Unity project folder without leaving Blender and doing it by hand.
The problem is when I try to install it as addon in order to have it always ready: console says it’s installed, but it doesn’t appear in the addon list neither in the File->Export menu as normally it does if run from text editor.
Thank you in advance.
Here’s the code
import bpy
import os
# path and filename of the actual file
actualBlendFile = os.path.dirname(__file__)
actualBlendFileName = os.path.split(actualBlendFile)[1]
def save_scene(context, filepath, saveSelected):
# save the actual file
bpy.ops.wm.save_mainfile(filepath=actualBlendFile,check_existing=True)
if (saveSelected):
selectedObjects = bpy.context.selected_objects
if not selectedObjects:
#alert("Nothing to save")
#raise RuntimeError("Stopping the script here")
return {'CANCELLED'}
else:
# delete all the unnecessary objects
for obj in bpy.data.objects:
if not(obj.select):
bpy.data.scenes[0].objects.unlink(obj)
bpy.data.objects.remove(obj)
else:
obj.select = False
if (len(bpy.context.selected_objects) > 1):
bpy.ops.object.select_all(action='TOGGLE')
# for every object to export to unity
for obj in bpy.data.objects:
# select
obj.select = True
# rotate x -90
bpy.ops.transform.rotate(value=-1.5708, axis=(1, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
# freeze rotation
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
# deselect
obj.select = False
# save the file inside Unity project folder
bpy.ops.wm.save_mainfile(filepath=filepath,check_existing=True)
# open the original blend file
bpy.ops.wm.open_mainfile(filepath=actualBlendFile)
return {'FINISHED'}
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator
class tkmkSave2Unity(Operator, ExportHelper):
"""Save the current scene or selected objects to a Unity project folder"""
bl_idname = "tkmk_save2unity.now"
bl_label = "Save 2 Unity folder"
# ExportHelper mixin class uses this
filename_ext = ".blend"
filter_glob = StringProperty(
default="*.blend",
options={'HIDDEN'},
)
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
save_selected = BoolProperty(
name="Save selected",
description="Save only selected objects",
default=True,
)
def execute(self, context):
result = save_scene(context, self.filepath, self.save_selected)
if (result == {'CANCELLED'}):
self.report({'ERROR'}, 'Please select at least one object')
print("Please select at least one object")
return result
# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
self.layout.operator(tkmkSave2Unity.bl_idname, text="tkmk Save to Unity")
def register():
bpy.utils.register_class(tkmkSave2Unity)
bpy.types.INFO_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(tkmkSave2Unity)
bpy.types.INFO_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
# test call
bpy.ops.tkmk_save2unity.now('INVOKE_DEFAULT')