0
$\begingroup$

I'm building an addon which is a collection of scripts, and I have my UI coded as one of this scripts. On of my tools (script) requires a user input, in this case a folder path, but I don`t know how to pass it onto my export tool so it knows where to export the selected objects. Here is how it is written in my UI script, I just copied it:

# root for export
    scn = context.scene
    col = layout.column(align=True)
    col.prop(scn.my_tool, "path", text="")

It shows great in my Panel but I'm not sure how use the string it gives in another script.

Thanks

EDIT

Following @WhataMesh pointed out here is what I did. In my UI script I added a class with a string property and changed the layout to fit, also had to register and unregister the new class and the property.

class MySettings(PropertyGroup):

path = StringProperty(
    name="path",
    description="Path to Directory",
    default="",
    maxlen=1024,
    subtype='DIR_PATH'


def draw(self, context):
    layout = self.layout

# root for export
    scn = context.scene
    mytool = scn.my_tool
    col = layout.column(align=True)
    col.prop(mytool, "path", text="")

def register():
    bpy.utils.register_class(UI)
    bpy.utils.register_class(MySettings)
    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)



def unregister():
    bpy.utils.unregister_class(UI)
    bpy.utils.unregister_class(MySettings)
    del bpy.types.Scene.my_tool
$\endgroup$
2
$\begingroup$

You can add properties to the scene that can be retrieved by all scripts. In the code you can find different 'variabletypes' (I think you can also make custom properties/variables yourself). To actually retrieve the value you will have to write bpy.data.scenes['Scene'].Annotation.my_bool

import bpy
from bpy.props import (StringProperty,
                       BoolProperty,
                       IntProperty,
                       FloatProperty,
                       EnumProperty,
                       PointerProperty,
                       )
from bpy.types import (Panel,
                       Operator,
                       PropertyGroup,
                       )

class Annotation(PropertyGroup):
    my_bool = BoolProperty(
        name="Enable or Disable",
        description="A bool property",
        default=False
    )

    my_int = IntProperty(
        name="Int Value",
        description="A integer property",
        default=23,
        min=10,
        max=100
    )

    my_float = FloatProperty(
        name="Float Value",
        description="A float property",
        default=23.7,
        min=0.01,
        max=30.0
    )

    my_string = StringProperty(
        name="User Input",
        description=":",
        default="",
        maxlen=1024,
    )

    my_enum = EnumProperty(
        name="Dropdown:",
        description="Apply Data to attribute.",
        items=[('OP1', "Option 1", ""),
               ('OP2', "Option 2", ""),
               ('OP3', "Option 3", ""),
               ]
    )

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.Annotation = PointerProperty(type=Annotation)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.Annotation    
$\endgroup$
  • $\begingroup$ If I understood correctly, this class should go with my UI script? and once done calling any variable (for instance my_string) in any other script it will be read? $\endgroup$ – Juan Manuel Lynch Sep 20 '18 at 18:46
  • $\begingroup$ @JuanManuelLynch Yes $\endgroup$ – WhatAMesh Sep 20 '18 at 18:50
  • 1
    $\begingroup$ For a folder path can use path = StringProperty(subtype='DIR_PATH') Which will give folder selection widget in ui. Worth noting with 2.8 around the corner bpy.utils.register_module(...) has been phased out in 2.8. Instead use register_class(...) on a per class basis. $\endgroup$ – batFINGER Sep 20 '18 at 19:12
  • $\begingroup$ @WhatAMesh it took me a while to make it work as I think that the register function is not correct as you state it and also how you call it didn't work. Still I managed to make it work. Check my edited question to see if I actually undestood what I'm doing. $\endgroup$ – Juan Manuel Lynch Sep 25 '18 at 7:22

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.