1
$\begingroup$

I wrote a little script addon but I can't put operator in panel with filepath to export to folder. The example under, I wan't to replace path with panel.

# EXPORTE EN COLLADA -
class ColladaExport(bpy.types.Operator):
    bl_idname = "object.colladaexport"
    bl_label = "Collada export"

    def execute(self, context):
        path = "D:\Documents\VR\mixedRea\polypheme\models"
        selection = bpy.context.selected_editable_objects
        bpy.ops.object.select_all(action='DESELECT')
        for obj in selection:
            if obj.type == 'MESH':
                obj.select = True
                bpy.ops.wm.collada_export(filepath = path + "/" + obj.name,
                                          check_existing = False,
                                          filter_blender = False,
                                          filter_image = False,
                                          filter_movie = False,
                                          filter_python = False,
                                          filter_font = False,
                                          filter_sound = False,
                                          filter_text = False,
                                          filter_btx = False,
                                          filter_collada = True,
                                          filter_folder = True,
                                          filemode = 8,
                                          selected = True,
                                          active_uv_only = True)
                obj.select = False

        for obj in selection:
            obj.select = True
        return {'FINISHED'}

Thank you Thank you very much for these indications. I'm going fast with an ant ... I followed your directions. The good text is written correctly with the brother button But the recording in collada in the chosen file does not work. Here is the script:

import bpy
import os

from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator

class ColladaExport(bpy.types.Operator):
    bl_idname = "object.colladaexport"
    bl_label = "Export collada"
    filter_folder = BoolProperty(default=True) # only folders
    directory = StringProperty(subtype='DIR_PATH')

    def execute(self, context):
        selection = [o for o in context.selected_objects if o.type == 'MESH']
        scene = context.scene
        for ob in context.selected_objects:
            ob.select = False

        for obj in selection:

            scene.objects.active = obj
            obj.select = True
            filepath = os.path.join(self.directory, "%s.dae" % obj.name)

            bpy.ops.wm.collada_export(filepath=filepath, check_existing=False, filter_blender=False, filter_image=False, filter_movie=False, filter_python=False, filter_font=False, filter_sound=False, filter_text=False, filter_btx=False, filter_collada=True, filter_folder=True, filemode=8, selected=True, active_uv_only=True)

            print("collada export", filepath)
            obj.select = False

        for obj in selection:
            obj.select = True
        return {'FINISHED'}


class VrTools(bpy.types.Panel):
    bl_category = "Tools"
    bl_context = "objectmode"
    bl_label = "VR Tools"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout
        self.layout.prop(context.scene, "directory")
        op = self.layout.operator("object.colladaexport")
        op.directory = context.scene.directory

def register():
    bpy.utils.register_class(ColladaExport)
    bpy.utils.register_class(VrTools)


def unregister():
    bpy.utils.unregister_class(ColladaExport)
    bpy.utils.register_class(VrTools)


if __name__ == "__main__":
    register()
$\endgroup$
  • 2
    $\begingroup$ You need to create a StringProperty to hold your path, then to access it in panel with layout.prop(...). $\endgroup$ – Jaroslav Jerryno Novotny Mar 22 '18 at 20:09
  • $\begingroup$ Don't use backslashes for file paths, or escape them properly. Use either r"D:\Documents\VR\mixedRea\polypheme\models", "D:\\Documents\\VR\mixedRea\\polypheme\\models", or "D:/Documents/VR\mixedRea/polypheme/models" $\endgroup$ – dr. Sybren Mar 22 '18 at 22:50
2
$\begingroup$

Invoke the filebrowser

If the filebrowser is invoked with only a directory property and no filepath it will only have choice of folder to export to. Possibly more handy than having a path to set in the UI.

To call the operator from code with a directory use bpy.ops.object.colladaexport(directory="/tmp") and it wont invoke the filebrowser.

import bpy
import os

from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator

class ColladaExport(bpy.types.Operator):
    bl_idname = "object.colladaexport"
    bl_label = "Multi Collada export"
    filter_folder = BoolProperty(default=True) # only folders
    directory = StringProperty(subtype='DIR_PATH')

    def execute(self, context):

        selection = [o for o in context.selected_objects if o.type == 'MESH']
        scene = context.scene
        for ob in context.selected_objects:
            ob.select = False

        for obj in selection:
            scene.objects.active = obj
            obj.select = True
            filepath = os.path.join(self.directory, "%s.dae" % obj.name)
            '''
            bpy.ops.export_scene.collada(
                    filepath=filepath,
                    use_selection=True,
                    )
            '''
            print("collada export", filepath)
            obj.select = False

        for obj in selection:
            obj.select = True
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        wm.fileselect_add(self)
        return {'RUNNING_MODAL'}

# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    self.layout.operator(ColladaExport.bl_idname, text="Text Export Operator")

def register():
    bpy.utils.register_class(ColladaExport)
    bpy.types.INFO_MT_file_export.append(menu_func_export)

def unregister():
    bpy.utils.unregister_class(ColladaExport)
    bpy.types.INFO_MT_file_export.remove(menu_func_export)

if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.colladaexport('INVOKE_DEFAULT')

Note: just found out the collada exporter I have doesn't work, so I commented it out

As a stand alone property

If you wish to define a directory on say the scene object, use the 'DIR_PATH' sub-type of a StringProperty

bpy.types.Scene.directory = StringProperty(subtype='DIR_PATH`)

can display in a draw method via

self.layout.prop(context.scene, "directory")

which will include a browse button in layout to set folder using filebrowser, like that shown for setting the render output folder.

enter image description here

and can use this setting in operator above from draw method with

op = self.layout.operator("object.colladaexport")
op.directory = context.scene.directory

in which case could remove the invoke method in operator above to not invoke the file browser.

Since you are using if for an addon, saving to the addons user preferences would be a good option. See this answer

EDIT given the edit to your question, you need to register the scene property.

def register():
    bpy.utils.register_class(ColladaExport)
    bpy.utils.register_class(VrTools)
    bpy.types.Scene.directory = StringProperty(subtype='DIR_PATH')


def unregister():
    bpy.utils.unregister_class(ColladaExport)
    bpy.utils.register_class(VrTools)
    del(bpy.types.Scene.directory)

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much. I understood a number of things thanks to you. But in fact I did not speak well (my English is not good). To be honest, this is an addon to help put 3D models in VR with A-Frame. Here is the whole code of my addon. I wish I could enter the destination folder of my selection in a text box and I sadly fails on this detail. Thank you so much. $\endgroup$ – falloff Mar 23 '18 at 7:45
  • $\begingroup$ You forgot to paste the link? $\endgroup$ – batFINGER Mar 24 '18 at 11:38
  • $\begingroup$ 320/5000 Thanks thanks. I did everything right. it's very nice, but it does not save the collada file in the destination folder I chose. I think it's the "filepath" that is not recognized .. I give up. I will manually enter the destination file each time. Thank you for this valuable information. See my sad failure on the blend file: olborne.com/telechargement/exportCollada.blend $\endgroup$ – falloff Mar 25 '18 at 8:34

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.