johnzero7 / XNALaraMesh

Blender addon Import/Export XPS Models, Poses
525 stars 94 forks source link

Batch Importing using bpy? #82

Closed shkodykoshka closed 2 years ago

shkodykoshka commented 3 years ago

It is possible to write a script that will import several .obj files, or other normal Blender files. I added some code to the "init.py", and I think it is close to working, however I have very little experience with Blender.

My Blender script to import them:

import os
import bpy

path_to_dir = '*directory*'

file_list = sorted(os.listdir(path_to_dir))

xna_list = [item for item in file_list if item.endswith('ascii')]
print(xna_list)

for item in xna_list:
    path_to_file = os.path.join(path_to_dir, item)
    bpy.ops.import_mesh.xna(filepath = path_to_file)

And the code I added into the init file:

class ImportXNA(bpy.types.Operator, ImportHelper):
    """Load a XNALara file"""
    bl_idname = "import_mesh.xna"
    bl_label = "Import XNA"
    bl_options = {'UNDO'}

    files: CollectionProperty(
        name="File Path",
        description="File path used for importing the XNA file",
        type=bpy.types.OperatorFileListElement,
    )

    #I don't know what I am doing :[
    hide_props_region: BoolProperty(
        name="Hide Operator Properties",
        description="Collaps the region displaying the operator settings",
        default=True,
    )

    directory: StringProperty()

    filename_ext = ".ascii"
    filter_glob: StringProperty(default="*.ascii", options={'HIDDEN'})

    def exeute(self, context):
        import os
        from . import import_xnalara_model
        print('Started executing')

        context.window.cursor_set('WAIT')

        paths = [
            os.path.join(self.directory, name.name)
            for name in self.files
        ]

        if not paths:
            paths.append(self.filepath)

        for path in paths:
            #import_xnalara_model.load(self, context, path)
            import_xnalara_model.loadXpsFile(self, path)

        context.window.cursor_set('DEFAULT')

        return {'FINISHED'}

I also added the class into classesToRegister. But I am getting error, linked below. If it for some reason doesn't load, message is:

ERROR (wm.operator): C:\blender-git\blender\source\blender\windowmanager\intern\wm_event_system.c:1371 wm_operator_invoke: invalid operator call 'IMPORT_MESH_OT_xna'

Any help would be greatly appreciated. error

ldo commented 3 years ago

Already done, and supports other importer addons besides this one.

shkodykoshka commented 3 years ago

Where would I put those 2 files?

ldo commented 3 years ago

How about /usr/local/share/man and /usr/local/bin, if you really want some kind of formal installation process. Or just access them out of the source directory, which is what I do.

shkodykoshka commented 3 years ago

I am sorry, could you explain how to access them out of the source directory? I see that the file has Python code inside, but what program should I use to run it?

ldo commented 3 years ago

You see how it has a shebang line at the start? So you just type the name of the file, and it will run. E.g.

/path/to/import_to_blender «file-to-import» «output-blend-file»

It’s a shell script which automatically launches Blender and loads a Python script into it. The man page gives examples of how to invoke it. You can even give it the name of a compressed archive, and it will extract and import the contents.

shkodykoshka commented 3 years ago

Sorry to bother you again, I have the import_to_blender file in the same directory as the file I want to open, but when attempting to launch it using Command Prompt I get the error that it doesn't recognize it as a program. I added a .cmd extension in hopes that it would work, it says that an external program, unar, is needed. It also writes the first few lines of the man page, and then closes itself.

ldo commented 3 years ago

Yup, it needs unar to do the archive extracting.

Normally the shell should be picked up automatically from the shebang line, you don’t need file extensions for executables on Linux-compatible systems.

shkodykoshka commented 3 years ago

I am on Windows, would the same things apply?

ldo commented 3 years ago

I thought that was already Linux-compatible.

Or if that doesn’t work, try Cygwin.

Or, you know, use a proper Linux system.

shkodykoshka commented 3 years ago

I installed Debian, unar, and all of its dependencies. I navigated to my directory but I get the error -bash: import_to_blender: command not found How would I launch it through unar?

ldo commented 3 years ago

It will use unar automatically as necessary. You just run the script as in the example I gave (more examples in the man page).

shkodykoshka commented 3 years ago

I am using the Windows Linux Subsystem to run Debian, which is probably not ideal, and don't have a gui, so how would I install Blender plugins?

ldo commented 3 years ago

Same as on any platform, I guess: use Blender’s “Install” button.

shkodykoshka commented 3 years ago

I was able to try to run the script, however I encountered a couple problems:

On Linux the script only imports some of the files, and on others Blender has problems. I am able to manually import the files on Windows, but the script is not able to import them. (I am using Kubuntu, is there a certain distro that I should use for this script?)

On Windows, the script starts running and then gives a couple errors from Blender-GIS-225 addon, and also says that python file is invalid.

What should I do to resolve these problems?