0
$\begingroup$

I have this simple script for exporting meshes. It's all in one file and works fine (no errors) when executed from the scripting editor.

When I am installing it through "Preferences -> Install from file" it shows up in the list, I enable it, but it wont work untill I run it in the script editor, in blender.

The same goes after restarting Blender - it's visible in the list as enbaled but does not work. I need to run it through script editor to activate.

I checked user scripts folder for addon and it's there, so installation process went ok.

Windows 10.

How to fix it? Thanks

Code:

bl_info = {"name" : "Spike FBX Unity Export",   "category": "Export",   "author": "Sergiusz"}

import bpy
import os

from bpy_extras.io_utils import ExportHelper

class Spk_Object_Settings(bpy.types.PropertyGroup):
    layer: bpy.props.StringProperty(default="spk_EnviroStatic")
    collider: bpy.props.EnumProperty(
        name = "Collider",
        items = (("None", "None", ""), ("Box", "Box", ""), ("Sphere", "Sphere", ""), ("Capsule", "Capsule", ""), ("Mesh", "Mesh", "")), default = "Mesh")

#====

class Spk_PT_Object_Settings(bpy.types.Panel):
    bl_idname = "spike.object_settings_panel"
    bl_label = "Object Settings"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Spike"

    @classmethod
    def poll(cls, context):
        return (context.selected_objects != []) and (context.active_object.type == "MESH")

    def draw(self, context):
        layout = self.layout

        o = bpy.context.active_object

        box = layout.box()
        box.prop(o.data.Spk_Object_Settings, "layer", text = "Layer")
        box.prop(o.data.Spk_Object_Settings, "collider", text = "Collider")

#====

class Spk_OT_Fbx_Export(bpy.types.Operator, ExportHelper):
    bl_idname = "spike.fbx_export"
    bl_label = "Spike FBX Unity Export"

    filename_ext = ".fbx"

    filter_glob: bpy.props.StringProperty(
        default="*.fbx",
        options={'HIDDEN'},
        maxlen=255,  # Max internal buffer length, longer would be clamped.
    )

    @classmethod
    def poll(cls, context):
        return (context.selected_objects != []) and (context.active_object.type == "MESH")

    def execute(self, context):
        folder_path = (os.path.dirname(self.filepath))

        if folder_path == None:
            self.report({'ERROR'}, "Export folder not set")
        else:
            objs = bpy.context.selected_objects

            for o in objs:
                o.select_set(False)

            for o in objs:
                if o.type == "MESH":

                    print("Exporting: " + o.name + "...")

                    o["spk_export_layer"] = o.data.Spk_Object_Settings.layer
                    o["spk_export_collider"] = o.data.Spk_Object_Settings.collider

                    file_path = folder_path + "/" + o.name + ".fbx"

                    o.select_set(True)
                    bpy.context.view_layer.objects.active = o

                    bpy.ops.export_scene.fbx(
                        filepath = file_path,
                        use_selection = True,
                        global_scale = 1.0,
                        apply_unit_scale = True,
                        apply_scale_options = 'FBX_SCALE_NONE',
                        mesh_smooth_type = 'OFF',
                        bake_space_transform = True,
                        bake_anim = False,
                        object_types = {'MESH'},
                        use_tspace = True,
                        use_mesh_modifiers = True,
                        use_custom_props  = True,
                        axis_forward = '-Z',
                        axis_up = 'Y')

                    print("Exporting: ...done")

                    o.select_set(False)
#            self.report({"INFO"}, "Will export: " + (context.active_object.name) + " to: " + (folder_path))

            for o in objs:
                o.select_set(True)
        return {'FINISHED'}


classes = (Spk_OT_Fbx_Export, Spk_Object_Settings, Spk_PT_Object_Settings)

def menu_func_export(self, context):
    self.layout.operator(Spk_OT_Fbx_Export.bl_idname, text="Spike FBX Unity Export")

def register():
    for c in classes:
        bpy.utils.register_class(c)

    bpy.types.Mesh.Spk_Object_Settings = bpy.props.PointerProperty(type=Spk_Object_Settings)

    bpy.types.TOPBAR_MT_file_export.append(menu_func_export)

def unregister():
    bpy.utils.unregister_classes_factory(classes)

    bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)

if __name__ == "__main__": register()
$\endgroup$
5
  • 1
    $\begingroup$ If the script additionally needs to be executed from the script editor there is something wrong. Should work immediately after the add-on is enabled. Does the operator show up in the export menu? What's the error in the console? $\endgroup$ – brockmann Apr 7 '20 at 9:41
  • $\begingroup$ @brockmann no errors :( Export operator does not show up in the menu until I run it through the editor. I put the code in the OP, maybe someone will spot son-of-a problem $\endgroup$ – Sergiusz Apr 7 '20 at 13:44
  • $\begingroup$ @brockmann thanks man for your help -it worked!. What do you mean by un-register your classes properly - I checked console and there are no errors? $\endgroup$ – Sergiusz Apr 8 '20 at 12:11
  • $\begingroup$ Cool! Yeah, try to disable the add-on and you'll see that there is something wrong, again see my addon here: blender.stackexchange.com/questions/169537/… $\endgroup$ – brockmann Apr 8 '20 at 12:13
  • $\begingroup$ @brockmann I just saw it and it's great! Does what I want, except for custom attributes but it's a small thingy. I wish I could find it earlier, before getting into python development! :) Thanks man! $\endgroup$ – Sergiusz Apr 8 '20 at 12:16
1
$\begingroup$

Your Version Info (bl_info dict) is incomplete. Add a version number and especially the blender release. If not greater than 2,8,0 blender is going to ignore the addon. From Blender 2.80: Python API Changes:

Make sure the "blender" key in the bl_info dictionary is set to:

(2, 80, 0) not (2, 8, 0) or (2, 79, 0) Example: "blender": (2, 80, 0), for Otherwise, this error is reported:

Exception: Add-on 'ADDON_NAME' has not been upgraded to 2.8, ignoring

Reference

bl_info = {
    "name": "Your cool add-on name",
    "author": "Your cool author name",
    "version": (0, 1, 0),
    "blender": (2, 80, 0),
    "location": "Where the user can find it",
    "description": "What the add-on does",
    "warning": "",
    "wcooliki_url": "",
    "tracker_url": "",
    "category": ""}

I also suggest use os.path.join() for all file paths and un-register your classes properly (returns another error). Read and compare to Exporting multiple meshes individually at once, which is basically the same add-on, exporting fbx files to unity.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.