Ghostkeeper / Blender3mfFormat

Blender add-on to import/export 3MF files
GNU General Public License v2.0
173 stars 20 forks source link

python script? #34

Closed eskeyaar closed 3 years ago

eskeyaar commented 3 years ago

Hi, Thanks for this add on. What will be its python script to call? will it be like normal method to import and export as stl? like below? _import bpy import os bpy.ops.import_scene.3mf(filepath='xx/xxx/1.3mf') bpy.ops.exportmesh.stl(filepath='1.stl')

Cheers,

Ghostkeeper commented 3 years ago

I'm not really familiar with conventions within Blender in that regard.

From my point of view, the main entry point of importing 3MF is this function: https://github.com/Ghostkeeper/Blender3mfFormat/blob/6f33fb71dd58d4116d96c1a82b4dbffb43202aa0/io_mesh_3mf/import_3mf.py#L79

That function reads these three attributes of the class:

https://github.com/Ghostkeeper/Blender3mfFormat/blob/6f33fb71dd58d4116d96c1a82b4dbffb43202aa0/io_mesh_3mf/import_3mf.py#L60-L62

This was taken from the examples of the STL import and PLY import scripts. Perhaps those import scripts have some helper functions that make it easier to use them from a script that I have overlooked. If so, such helper functions should be added to this Import/Export add-on as well.

eskeyaar commented 3 years ago

Thanks, am not well versed with python, but I will try and see.

Cheers

eskeyaar commented 3 years ago

@Ghostkeeper i tried to do run similar to which stl gets import? like using import_scene.3mf, am getting SyntaxError: invalid syntax

location: :-1

Thanks!

Ghostkeeper commented 3 years ago

I've never implemented any method to make it work from the Python command line in Blender. I hadn't considered that until you mentioned it. So most likely it's not going to work (yet).

However the SyntaxError you're getting signifies that you made a mistake in the command you typed in. Possibly a typo, unmatched brackets, etc.

Ghostkeeper commented 3 years ago

So it turns out that by registering the operator, it was already accessible from the Python scripting environment in Blender automatically. You could discover this by running:

>>> dir(bpy.ops.import_mesh)
['3mf', 'ply', 'stl']

However if you try to call it that way (bpy.ops.import_mesh.3mf(filepath="/path/to/file.3mf")), Python gives a syntax error. This is because names in Python may not start with a number.

So I've changed the operator's ID to be import_mesh.threemf. This avoids the problem. The import operator is now in bpy.ops.import_mesh.threemf and the export operator in bpy.ops.export_mesh.threemf.

I've documented how to call these operators from a scripting environment here: https://github.com/Ghostkeeper/Blender3mfFormat#scripting

eskeyaar commented 3 years ago

Awesome, Thanks Works!

Deuracell commented 2 years ago

Hi,

There still seems to be an error with the python integration. Regardles of which version of Blender, I get the following error:

bpy.ops.import_mesh.threemf(filepath="F:\Nextcloud\Deventus\3mf-test\microvred.3mf") Traceback (most recent call last): File "", line 1, in File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\modules\bpy\ops.py", line 201, in call ret = op_call(self.idname_py(), None, kw) AttributeError: Calling operator "bpy.ops.import_mesh.threemf" error, could not be found

Ghostkeeper commented 2 years ago

That there is Blender calling an operator that doesn't exist. This add-on registers itself and then waits to be called. Apparently Blender kind of semi-registered it with you, where it knows that it exists but then doesn't find the operator any more, and never calls the code in this add-on. So the add-on itself could never do anything about this error.

This could be the result of something like a syntax error upon registering, but then the rest of us would have the same issue (unless you edited these files yourself). More likely it is somehow not installed correctly.

Deuracell commented 2 years ago

Hi,

Late reply and all but here we are. I have tried redownloading and installing again on different versions of Blender and on three different computers, but the result is the same. I've made no changes to the source. The following pops up when i type so it's defenetly loaded, i guess "threemf" is suposed to be there as well, rigth? Or should it replace "3mf"?

>>> dir(bpy.ops.import_mesh) ['3mf', 'ply', 'stl']

Regards Simon

Deuracell commented 2 years ago

Never mind, I now changed the bl_idname to "import_mesh.threemf" and with that it works! I assumed this whas already in the 1.0.1 release but apparently not.

SourceCodeZone commented 2 years ago

If anyone having the issue again, it's because the commit https://github.com/Ghostkeeper/Blender3mfFormat/commit/f4319b36addb1bb08ea593e4c50ccc3736c37a40 seem not updated in release package here https://github.com/Ghostkeeper/Blender3mfFormat/releases/tag/v1.0.1 Manually replacing the file in plugin directory from here https://github.com/Ghostkeeper/Blender3mfFormat/tree/master/io_mesh_3mf will fix the issue.