0
$\begingroup$

I think I know this is a basic question but I dont got the connection between values inside a class and outside.

I have an Array with string values defined in a OperatorClass and I can use it inside but what I really want is defining the Array Values outside of the class and then reference it when needed - everytime I define a PropertyGroup or StringProperty I cant find it...

I think I miss some basic understanding where values are stored in that blender data structure or how to add the array to some kind of globalness…

EDIT: so I would like to outsource the Arrays from this class

class OBJECT_OT_createWorkFolder(bpy.types.Operator):
    bl_idname = "object.workfoldbtn"
    bl_label = "Create Projectfolder"
    bl_description = "Fügt dem Ausgew. Verzn. Arbeitsfolder hinzu"

    Workfolder = ["00_DATA","01_INCOMING","02_Workspace","03_Render","04_Compositing","05_OUT"]
    WS_Subfolder = ["02_0_Exports","02_1_Assets", "02_2_Textures","02_3_Light","02_4_Scripts","02_x_Caches"]
    Rndr_Subfolder = ["03_0_TestRender","03_1_FINAL"]
    Comp_Subfolder = ["Photoshop","AfterEffects","DaVinciRes"]
...
$\endgroup$
  • $\begingroup$ You can just add a list (array), dict or set as usual to your script and use a callback to get the values when calling the operator, easy. If you want user interaction use a CollectionProperty: blender.stackexchange.com/a/30446/31447. What's your goal? What exactly you can't find? $\endgroup$ – brockmann Apr 1 at 12:26
  • $\begingroup$ Thx for the link I think I got through this in the past, but I will give it a next try. .. PS: I edited the Question, to what I want mor exactly $\endgroup$ – Master Heavy Apr 1 at 12:35
1
$\begingroup$

Two methods:

  1. Method 1: python way
import bpy

arr = ["string1", "string2"]

class ClassName(bpy.types.Operator):

    def execute(self, context):
        for str in arr:
        print(str)

    return {'FINISHED'}

classes = (
    ClassName,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)

if __name__ == "__main__":
    register()

  1. Method 2: Blender way
import bpy

class Variables(bpy.types.PropertyGroup):
    string1: bpy.props.StringProperty(name="a string")
    string2: bpy.props.StringProperty(name="another string", default="default value")

class ClassName(bpy.types.Operator):

    def execute(self, context):
        bpy.context.scene.var.string1 = "Hello World"
        print(bpy.context.scene.var.string1)
        print(bpy.context.scene.var.string2)

    return {'FINISHED'}


classes = (
    ClassName,
    Variables,
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
    bpy.types.Scene.var = bpy.props.PointerProperty(type=Variables)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)      
    del bpy.types.Scene.var

if __name__ == "__main__":
    register()


learnt from: Godot Game Tools

edit: python console this is how you can access a variable in blender from anywhere. I've used GodotGameTools as an example and also highlighted what it should have been if you used my code

| improve this answer | |
$\endgroup$
  • $\begingroup$ thx for the efford but can you show an example "the Blender Way" without using single strings and putting all in an Array ? … Or does the python way mean, that an array that is created somewhere can addressed everywhere ? $\endgroup$ – Master Heavy Apr 1 at 12:46
  • $\begingroup$ @MasterHeavy I don't think you can store arrays of strings easily, especially arrays of varying lengths. The simplest would be to store all the elements in a single string with a separator like '|', then use a method to parse and unparse it to get all your elements with the string.split('|') method. $\endgroup$ – Gorgious Apr 1 at 15:29
  • $\begingroup$ This: blender.stackexchange.com/questions/16511/… - seems to be what I need, but I cant sort it out and use it for my case - he has this array and can use it... I am confused $\endgroup$ – Master Heavy Apr 1 at 19:16
  • $\begingroup$ "seems what I need" is the use of CollectionProperty as mentioned in my comment years ago @MasterHeavy To me it seems you don't need that because your list is hard coded and unique so you just can declare a set, done. Again, in case you'd like to have user interaction (changing the list), let me know... $\endgroup$ – brockmann Apr 2 at 9:24

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.