Jump to content
Daniel26

New mimetype: howto register it?

Recommended Posts

Posted (edited) · New mimetype: howto register it?

Hello,

 

I wrote a plugin which I wan't to start by a new mimetype. But how do I register that mimetype?

My __init__.py in the plugins directory:

def getMetaData():
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}

    return {
        "mesh_writer": {
            "output": [
                {
                    "mime_type": "text/chitu-g-code",
                    "mode": MeshWriter.OutputMode.TextMode,
                    "extension": "chitu",
                    "description": i18n_catalog.i18nc("@item:inlistbox", "chitu addons")
                }
            ]
        }
    }

Is that really enough so I can call that plugin via the printers definition with 

"file_formats": "text/x-gcode;text/chitu-g-code",

Regards

 

Daniel

Edited by Daniel26

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Your __init__.py needs a bit more (some imports and a register() function), but generally, yes: that is it.

Share this post


Link to post
Share on other sites
Posted (edited) · New mimetype: howto register it?

Sorry, here is the complete __init__.py

 

import sys

from UM.Logger import Logger
try:
    from . import ChituCodeWriter
except ImportError:
    Logger.log("w", "Could not import ChituCodeWriter")

from UM.i18n import i18nCatalog #To translate the file format description.
from UM.Mesh.MeshWriter import MeshWriter #For the binary mode flag.

i18n_catalog = i18nCatalog("cura")

def getMetaData():
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}

    return {
        "mesh_writer": {
            "output": [
                {
                    "mime_type": "text/chitu-g-code",
                    "mode": MeshWriter.OutputMode.TextMode,
                    "extension": "chitu",
                    "description": i18n_catalog.i18nc("@item:inlistbox", "Chitu Code")
                }
            ]
        }
    }

def register(app):
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}

    return { "mesh_writer": ChituCodeWriter.ChituCodeWriter() }

 

 

How can I be sure that the new mimetype is known by cura?

I don't see any signs that my plugin is used.

 

Regards

 

Daniel

 

Edited by Daniel26

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

File -> Export should allow you to at least try to export as a .chitu file (Save as type). That is, unless ChituCodeWriter fails to import. If ChituCodeWriter fails to import, that should be visible in the logs.

 

 

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Ok. Thats for testing.

What I wan't: if printer is a tronxy, the g-code should be created, that g-code hat to be modified by my plugin.

How can I do that? Is that done by "file_formats": "text/x-gcode;text/chitu-g-code" in the printers definition?

 

Sorry for that stupid questions, but I can't find any info about "Mesh_writer" Plugins.

 

Regards

 

Daniel

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

> if printer is a tronxy, the g-code should be created, that g-code hat to be modified by my plugin.

 

What sort of modifications are you talking about? Does the modified gcode file have to be named *.chitu? If not, it is easier/less confusing for the user to not use a meshwriter, but use something that simply post-processes the gcode when it is written.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

If you still want to go the MeshWriter route, I would suggest you take the GCodeGzWriter as an example/starting point:

https://github.com/Ultimaker/Cura/tree/master/plugins/GCodeGzWriter

 

https://github.com/Ultimaker/Cura/blob/master/plugins/GCodeGzWriter/GCodeGzWriter.py shows how to let the GCodeWriter to actually create the gcode file string, which you can then modify before you write it to the output stream.

Share this post


Link to post
Share on other sites
Posted (edited) · New mimetype: howto register it?
31 minutes ago, ahoeben said:

> if printer is a tronxy, the g-code should be created, that g-code hat to be modified by my plugin.

 

What sort of modifications are you talking about? Does the modified gcode file have to be named *.chitu? If not, it is easier/less confusing for the user to not use a meshwriter, but use something that simply post-processes the gcode when it is written.

 

It adds e.g. a Screenshot to the gcode so the part is shown on the touchscreen of the printer. The timestamps also have a special format.

I tried to submit a post process script, but they won't include it in the cura release. They advised me to write a mesh writer plugin and to submit it to the plugin database.

 

Edited by Daniel26

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Ok, then have a look at the UFPWriter as an example instead.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

To Use the UFPWriter I have to "save as" and select "UFP"?

If yes, this is not what I wan't.

If I add a Tronxy printer the gcode simply has also "run thru" my plugin ans should be saved as gcode.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?
49 minutes ago, Daniel26 said:

To Use the UFPWriter I have to "save as" and select "UFP"?

Yes, unless you specify your printer prefers using the mimetype specified in the UFPwriter plugin, like so:

 

"file_formats": "application/x-ufp;text/x-gcode"

 

Note how application/x-ufp is in front of text/x-gcode

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Do you want to write multiple files at once (ie: a thumbnail and a gcode file in two separate files)? That is not something the MeshWriter supports easily. You would have to build that yourself.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?
Quote

"file_formats": "application/x-ufp;text/x-gcode"

 

Note how application/x-ufp is in front of text/x-gcode

 

So, If I understand correctly: The file generated from ufp is "piped thru" the Script that creates the gcode? So with the order of the fileformats I can control the order the plugins are used?

 

Quote

Do you want to write multiple files at once (ie: a thumbnail and a gcode file in two separate files)? That is not something the MeshWriter supports easily. You would have to build that yourself.

Nope. There are only additional M-Commands included in the gcode-file. So I only wan't one file.

 

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?
1 hour ago, Daniel26 said:

If I understand correctly:

No, that is not correct. There is no automatic piping of anything. The order of mimetypes just specifies what MeshWriter plugin is preferred for that printer. Only one MeshWriter gets called by Cura, but a MeshWriter might call other meshwriters.

 

I'm sorry, I don't have time to hold your hand on this at the moment.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

No Prob, thanks. I will try to figure it out. Sadly, there is no documentation about that.

 

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Hmmm, my Plugin seems not to be registered. I put a simple "print" to the register and the getMetaData function, but I can't find the output in any log.

Any idea?

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Not without either the full code you use (and where you put it) or a link to your cura.log

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Here the __init__.py:

#Copyright (c) 2018 Ultimaker B.V.
#Cura is released under the terms of the LGPLv3 or higher.

import sys

from UM.Logger import Logger
try:
    from . import ChituCodeWriter
except ImportError:
    Logger.log("w", "Could not import ChituCodeWriter")

from UM.i18n import i18nCatalog #To translate the file format description.
from UM.Mesh.MeshWriter import MeshWriter #For the binary mode flag.

i18n_catalog = i18nCatalog("cura")

def getMetaData():
    print("Loading chitu_mods metadata")
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}

    return {
        "mesh_writer": {
            "output": [
                {
                    "mime_type": "text/chitu-g-code",
                    "mode": MeshWriter.OutputMode.TextMode,
                    "extension": "gcode",
                    "description": i18n_catalog.i18nc("@item:inlistbox", "Chitu gcode")
                }
            ]
        }
    }

def register(app):
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}
    else:
        print("fregistering chitu_mods")
        return { "mesh_writer": ChituCodeWriter.ChituCodeWriter() }

 

Cura.log is attached.

 

Thanks for your time

cura.log

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Do you have a plugin.json? I don't think the plugin is loaded unless there is a plugin.json file with the appropriate data in it.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?
{
    "name": "Chitu code Writer",
    "author": "Spanni26",
    "version": "1.0.0",
    "description": "Adds chitu code to gcode",
    "api": "7.1",
    "i18n-catalog": "cura"
}

Yes, see above.

 

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

So, where did you put those files then?

 

The print() in getMetaData() works for me. But since getMetaData() returns {} (because there is no ChituCodeWriter), register() never gets called.

 

How do you run Cura? print() statements are not logged. They show up if you run Cura from source, but don't end up in cura.log. If you want entries in cura.log, use UM.Logger.Logger.log() instead.

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Ok, thanks. Will try the logger.

The file are in a seprate folder in the plugins dir.

 

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

Ok, tried it now with the logger, but still no message in the logs:

 

#Copyright (c) 2018 Ultimaker B.V.
#Cura is released under the terms of the LGPLv3 or higher.

import sys

from UM.Logger import Logger
try:
    from . import ChituCodeWriter
except ImportError:
    Logger.log("w", "Could not import ChituCodeWriter")

from UM.i18n import i18nCatalog #To translate the file format description.
from UM.Mesh.MeshWriter import MeshWriter #For the binary mode flag.

i18n_catalog = i18nCatalog("cura")

def getMetaData():
    Logger.log("w", "--- get metadata of ChituCodeWriter ---")
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}

    return {
        "mesh_writer": {
            "output": [
                {
                    "mime_type": "text/chitu-g-code",
                    "mode": MeshWriter.OutputMode.TextMode,
                    "extension": "gcode",
                    "description": i18n_catalog.i18nc("@item:inlistbox", "Chitu gcode")
                }
            ]
        }
    }

def register(app):
    Logger.log("w", "--- register  ChituCodeWriter ---")
    if "ChituCodeWriter.ChituCodeWriter" not in sys.modules:
        return {}
    else:
        return { "mesh_writer": ChituCodeWriter.ChituCodeWriter() }

Nothing in the logs.

I also have no such mime type in the save-as-dialog.

 

Frustrating...

 

Regards


Daniel

Share this post


Link to post
Share on other sites
Posted · New mimetype: howto register it?

It is also frustrating trying to help you when you don't answer simple questions.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Our picks

    • Survey: Understanding your workflow
      Interact with future concepts and aim to collect your feedback and opinion. In particular, if this would/could be a welcome addition to your 3D printing workflow. Interested?
      • 0 replies
    • Coronavirus: Let's do our part
      Through this post I would like to further explain what we are doing, and what you could be doing. 
      Our efforts consist of 2 layers. First; connect medical institutions and hospitals to (local) 3D Printing hubs to help them print parts of which a 3D model already exists. And second, contribute to design the necessary part and then have it printed via a (local) 3D printing hub. Experts are available from within Ultimaker and from within our network of 3D printing experts.
        • Like
      • 49 replies
×
×
  • Create New...

Important Information

Welcome to the Ultimaker Community of 3D printing experts. Visit the following links to read more about our Terms of Use or our Privacy Policy. Thank you!