0
$\begingroup$

I try to create this script but I’m newbie in python

enter image description here

The aim is to export a fbx in specific folder when you click Export button. If the checkbox is True, then the filename get the name of collection. Conversely, if is uncheck the the filename get the active object.

Here is my own script but i get an error message which says name ‘filename’ is not defined.

Thanks for your help.

bl_info = {
    "name" : "MyTab", 
    "author" : "",
    "version" :(0, 1),
    "blender" : (2, 93, 6),
    "location":"View3D > Tool",
    "warning":"",
    "wiki_url":"",
    "category": "Custom" 
}    

import bpy
from bpy.props import BoolProperty

bpy.types.Scene.my_checkbox = BoolProperty(
    name="is Collection",
    description="is Collection ?",
    default = False)
        
class MyPanel_PT_Panel(bpy.types.Panel): 
    bl_label = "MyPanel" 
    bl_idname = "my_panel" #id name du premier panneau 
    bl_space_type = 'VIEW_3D' # le type d'espace c'est à dire espace 3D
    bl_region_type = 'UI'  # ca agit sur quoi ? sur l'UI
    bl_category = 'MyTab' # nom de l'outil qui sera affiché sur les onglets tools
    
    
    def draw(self, context):
        layout = self.layout
        
        
        row = layout.row()
        layout.prop(context.scene, "my_prop") 
        row = layout.row()
        row.operator("fbx.exporter",text="Export")
        
        if (bpy.types.Scene.my_checkbox == True):
            fileName = bpy.context.collection.name + '.fbx'
        else:
            fileName = bpy.context.active_object.name + '.fbx'
                     
      
class Fbx_Exporter(bpy.types.Operator):
    # export in a Substance Folder for Stone project     # Use this as a tooltip for menu items and buttons.
    bl_idname = "fbx.exporter"        # Unique identifier for buttons and menu items to reference.
    bl_label = "fbx_exporter"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # Get the selected objects
        selectedObject = bpy.context.selected_objects

        # Get the name of selected object
        for obj in selectedObject:
            
            # to define folderPath
            # rajouter trjs \\ pour définir un chemin
            
            folderPath = 'C:\\Users\\Username\\Desktop\\FbxFolder\\'
            
            # customfilePath will be used to filepath of FPX API operator
            customFilePath = folderPath + fileName

            # FBX API operator

            bpy.ops.export_scene.fbx(filepath= customFilePath, check_existing=True, filter_glob='*.fbx', use_selection=True, use_active_collection=False, global_scale=1.0, apply_unit_scale=True, apply_scale_options='FBX_SCALE_UNITS', bake_space_transform=False, object_types={'ARMATURE', 'CAMERA', 'EMPTY', 'LIGHT', 'MESH', 'OTHER'}, use_mesh_modifiers=True, use_mesh_modifiers_render=True, mesh_smooth_type='OFF', use_subsurf=False, use_mesh_edges=False, use_tspace=False, use_custom_props=False, add_leaf_bones=True, primary_bone_axis='Y', secondary_bone_axis='X', use_armature_deform_only=False, armature_nodetype='NULL', bake_anim=True, bake_anim_use_all_bones=True, bake_anim_use_nla_strips=True, bake_anim_use_all_actions=True, bake_anim_force_startend_keying=True, bake_anim_step=1.0, bake_anim_simplify_factor=1.0, path_mode='AUTO', embed_textures=False, batch_mode='OFF', use_batch_own_dir=True, use_metadata=True, axis_forward='-Z', axis_up='Y')

               
            

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.  
    

    
def register():
    bpy.utils.register_class(MyPanel_PT_Panel)
    bpy.utils.register_class(Fbx_Exporter)
    
    
   
   

def unregister ():
    bpy.utils.unregister_class(MyPanel_PT_Panel)
    bpy.utils.unregister_class(Fbx_Exporter)
    
    
    
if __name__== "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ you are defining filename in the panel class, where it remains a local. That means it is not visible in the operator class. Create a string property attached to Scene and use it in both classes. or,more simply, move the if statement to the exporter class. $\endgroup$ Jan 28, 2022 at 23:08

1 Answer 1

0
$\begingroup$

Three important points that should be changed:

  1. As mentioned, the export operator needs the filename (line 36 in following script)

  2. Access to registered properties in scene with bpy.context.scene.my_checkbox (line 51)

  3. You export the same object several times, according to the number of your selected objects (line 62). No need for this cause bpy.ops.export_scene.fbx(use_selection = True) by default

And a few little things more directly commented in the script.

bl_info = {
    "name" : "MyTab",  
    "author" : "",
    "version" :(0, 1),
    "blender" : (2, 93, 6),
    "location":"View3D > Tool",
    "warning":"",
    "wiki_url":"",
    "category": "Custom"  ### use "Tool" here if Panel should be in View3D > Tool ; no existing category = new tab
}    

import bpy
from bpy.props import BoolProperty

bpy.types.Scene.my_checkbox = BoolProperty(
    name="is Collection",
    description="is Collection ?",
    default = False)   

    
class MyPanel_PT_Panel(bpy.types.Panel): 
    bl_label = "MyPanel" 
    bl_idname = "MY_PT_PANEL"  ### avoid PT warning    #id name du premier panneau  
    bl_space_type = 'VIEW_3D' # le type d'espace c'est à dire espace 3D
    bl_region_type = 'UI'  # ca agit sur quoi ? sur l'UI
    bl_category = 'MyTab' # nom de l'outil qui sera affiché sur les onglets tools
    
    def draw(self, context):
        layout = self.layout
        row = layout.row()

        layout.prop(context.scene, "my_checkbox") ### use prop identifier , "scene.my_prop" is not registered
        row = layout.row()
        row.operator("fbx.exporter",text="Export")
        
#        if (bpy.types.Scene.my_checkbox == True):              ### moved to operator, cause export op needs fileName
#            fileName = bpy.context.collection.name + '.fbx'
#        else:
#            fileName = bpy.context.active_object.name + '.fbx'
                     
      
class Fbx_Exporter(bpy.types.Operator):
    # export in a Substance Folder for Stone project     # Use this as a tooltip for menu items and buttons.
    bl_idname = "fbx.exporter"        # Unique identifier for buttons and menu items to reference.
    bl_label = "fbx_exporter"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.   ### undo export ???

    def execute(self, context):        # execute() is called when running the operator.

#       if (bpy.types.Scene.my_checkbox == True):    ### this calls the rna_property not the value
        if context.scene.my_checkbox == True:        ### prop 'my_checkbox' is registered in scene 
            fileName = bpy.context.collection.name + '.fbx'    ### returns 'Master Collection.fbx' if active collection is 'Scene Collection' 
        else:
            fileName = bpy.context.active_object.name + '.fbx' ### returns '[active obj].fbx' , e.g 'Cube.fbx'


        # Get the selected objects
#       selectedObject = bpy.context.selected_objects  ### no need cause by default bpy.ops.export_scene.fbx(use_selection = True)
        
        # Get the name of selected object   
#       for obj in selectedObject:    ### no, this gets each obj in list ; if names: for obj.name in selectedObject:  ... but
        ### ... you export the same object several times, according to the number of selected objects            

            # to define folderPath
            # rajouter trjs \\ pour définir un chemin
            
        folderPath = 'C:\\Users\\Username\\Desktop\\FbxFolder\\'  ### unindent next lines to execute once 
          
        # customfilePath will be used to filepath of FPX API operator
        customFilePath = folderPath + fileName
        

        # FBX API operator
        ### no need to call default options again if not changed, see blue ones in API bpy.ops.export_scene.fbx()
        bpy.ops.export_scene.fbx(filepath= customFilePath, use_selection=True, use_active_collection=False, apply_scale_options='FBX_SCALE_UNITS')

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.  
    

def register():
    bpy.utils.register_class(MyPanel_PT_Panel)
    bpy.utils.register_class(Fbx_Exporter)

def unregister ():
    bpy.utils.unregister_class(MyPanel_PT_Panel)
    bpy.utils.unregister_class(Fbx_Exporter)

    
if __name__== "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ Thank you so much for taking time to respond me. It will allow me to improve my workflow considerably and you commentary is very clearly. Thanks lot ! Its very precious for me. $\endgroup$
    – MaryK
    Feb 3, 2022 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .