1

I'm working on a plugin modification which is a tool for exporting gltf. On the export windows, we have a choice to put "Copyright" & "Default Texture Location" parameters as you can see here :

enter image description here

I wanted to add some input into the preferences tab of the addon by adding two input :

  • Default Texture Location
  • Default Copyright Name

That way, we could just set this one time and no need to make some operator presets etc.. enter image description here

My code is for the preference part is :

## class to add the preference settings
class addSettingsPanel(bpy.types.AddonPreferences):
    bl_idname = __package__
settings_default_texture_location: bpy.props.StringProperty (
    name = "Default Texture Location",
    description = "Default Texture Location",
    default = "../texture"

)



settings_default_copyright: bpy.props.StringProperty (
    name = "Default Copyright Name",
    description = "Default Copyright Name",
    default = ""

)

##bpy.types.Scene.toto = settings_default_copyright

bpy.types.Scene.my_sel_value = bpy.props.StringProperty(name="Sel")

## draw the panel in the addon preferences
def draw(self, context):
    layout = self.layout
    box = layout.box()

    ##row = layout.row()
    col = box.column(align = True)

    ## texture default location
    col.prop(self, 'settings_default_texture_location', expand=True)

    ## default copyright
    col.prop(self, 'settings_default_copyright', expand=True)

The question is :

How to access to these values in the export windows, which is on another file to show it in the first image instead of the blank label "Textures" and "Copyright" ?

EDIT :

As @brockmann Suggested in the comments,

I modified the code to :

preferences = bpy.context.preferences
addon_prefs = preferences.addons[__package__].preferences

export_copyright: StringProperty( name='Copyright', description='Legal rights and conditions for the model', default=addon_prefs.settings_default_texture_location )

But I've got the error " KeyError: ... key "Blender2MSFS.exporter" not found. So I tried to manually put "Blender2MSFS" instead of package and it is not working too.

The code is here : https://github.com/btronquo/Blender2MSFS2 exporter/init.py (line 96,97 and then 129 / 151)

Boris
  • 23
  • 3
  • Hello, could you also post the code that's responsible for drawing the UI showed in the 1st screenshot ? – Gorgious Oct 29 '21 at 08:05
  • Open up __init__.py in the export folder, declare your preferences there and assign the new properties to the default parm of e.g. export_copyright (line 122). – brockmann Oct 29 '21 at 08:05
  • @Gorgious I guess the OP is talking about this add-on and wants to modify it. I guess... :) – brockmann Oct 29 '21 at 08:07
  • @brockmann, I don't understand

    How i'm supposed to retrive the value and put it in the default="" place ? export_copyright: StringProperty( name='Copyright', description='Legal rights and conditions for the model', default='' )

    It would be : default=settings_default_copyright.default or something ?

    – Boris Oct 29 '21 at 08:22
  • Pretty straight forward, see the demo in the docs. You can get a reference to the prefs via addon_prefs = preferences.addons[__name__].preferences and then just assign it (default=addon_prefs.default_texture_location) as mentioned. – brockmann Oct 29 '21 at 08:27
  • It may be a bit too involved for your current python level, but if you don't want to change the other addon code you can add an update callback to your preferences that will dynamically unregister and re-register the addon properties with the new default. See https://blender.stackexchange.com/a/195977/86891 or https://blender.stackexchange.com/questions/161529/is-there-any-way-to-override-uilayout-prop-tooltip/161533#161533 – Gorgious Oct 29 '21 at 08:39
  • Thank you @Gorgious, I prefer to understand the concept first and then I'll try to add the callback for dynamics register and unregister :) – Boris Oct 29 '21 at 09:22
  • @brockmann , it's not really straight forward for me since I use python in a beginner / intermediate level and in blender it's totally different that I use to work with :/ I have made a lot of trials and can't see how to made this works.. I have context is not defined (tried with bpy.context, same error) Could you tell me where to add theses lines exactly ? – Boris Oct 29 '21 at 09:32

1 Answers1

2

In the menu.

Could simply set these values in the menu function, which is how most users will invoke the operator (filebrowser). This will "fill in" the exporter properties with those set in the addon prefs.

Note have simply named same as exporter prop, eg export_texture_dir rather than the "long winded" settings_default_texture_location

exporter/__init__.py circa line 962

def menu_func_export(self, context):
    # get the addon name
    name = __name__.split(".")[0]
    preferences = context.preferences.addons.get(name)
    op = self.layout.operator(ExportExtendedGLTF2.bl_idname, text='extended glTF 2.0 (.glb/.gltf) for MSFS')
    if preferences:
        addon_prefs = preferences.preferences
        op.export_copyright = addon_prefs.export_copyright
        op.export_texture_dir = addon_prefs.export_texture_dir

How do I change the default settings of export operators?

Alternatively could do same in the operators invoke method.

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • Awesome, I was so focused on the field them self that I have missed the menu function

    A BIG THANK YOU !

    – Boris Oct 29 '21 at 18:01