0
$\begingroup$

I have registered some variable to store some value/string through def register() in the __innit__.py. And I wrote an operator in a separate file trying to access that variable, but fail.

In the __init__.py I have register the following:

def register():
    bpy.types.Scene.myVariable= "sometext"

import bpy

from bpy.types import Operator

from bpy.props import (
    IntProperty,
    FloatProperty,
    StringProperty,
)

in the Operator file my_operators.py I tried

import bpy

from bpy.types import Operator

from bpy.props import (
        IntProperty,
        FloatProperty,
        StringProperty,
)

class MyOperator(bpy.types.Operator):
    bl_idname = "myops.addthings"
    bl_label = "Add Base Mesh"
    bl_options = {'REGISTER', 'UNDO'}
    bl_description ="Add Base Mesh"    

    #this line tries to get the registered variable
    myVariable = bpy.context.scene.myVariable

def execute(self, context):
    return {'FINISHED'}
$\endgroup$
0
$\begingroup$

Your trying to make a property to the scene object the wrong way in the __init__.py file line 2 bpy.types.Scene.myVariable= "sometext ... that is why this is failing to work. Also please put all import statements at the very top.

The correct way to do this in the __init__.py put

import bpy 

# This creates a new variable to the scene object that is a string 
# then assigns it the default value "sometext" 
bpy.types.Scene.myVariable = bpy.props.StringProperty(name = "myVariable", default = "sometext")

# Later on you can access the variable like this:
print(bpy.context.scene.myVariable) 

# Of course you can also modify it just like any other 
# variable
bpy.context.scene.myVariable = "Some other text" 
print(bpy.context.scene.myVariable) 

Lemme know if you need to know anything else tried to explain it the best I could!

$\endgroup$

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.