I changed your code snippet a bit and made some comment. I hope that solves your problem.
import bpy
class MyOperator(bpy.types.Operator):
"""Operate"""
bl_idname = "object.myoperator"
bl_label = "Operate"
# make the variable a property of the operator
myvar = bpy.props.StringProperty()
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
print(self.myvar)
# I added this here because otherwise it will through an exception at the end
return {"FINISHED"}
# this is how you invoke the operator, but you have to register it before
bpy.ops.object.myoperator("INVOKE_DEFAULT", myvar = "Hello World")
Unfortunally you can't do the same thing with tuples (except you have numbers)...
Here is some more information on how to define properties: https://www.blender.org/api/blender_python_api_2_76_2/bpy.props.html
About tuples as parameters:
When the tuple only consists of numbers or booleans, the tuple always has the same length <= 32, you can use either a BoolVectorProperty, IntVectorProperty or FloatVectorProperty.
A more generic solution would be to use the CollectionProperty.
Therefor you first have to make a property group that contains the structure of the individual elements in the collection.
In your case you only have a tuple with multiple numbers, so the PropertyGroup would look like so:
import bpy
from bpy.props import *
class SingleNumberProperty(bpy.types.PropertyGroup):
value = FloatProperty()
In your operator you can define the collection property that uses this type.
class MyOperator(bpy.types.Operator):
bl_idname = "my_operator.my_operator"
bl_label = "My Operator"
numbers_collection= CollectionProperty(type = SingleNumberProperty)
In the execute function you can unpack this collection in the following way:
def execute(self, context):
my_numbers = []
for item in self.numbers_collection:
my_numbers.append(item.value)
return {"FINISHED"}
The part that was the most difficult to find out is how to pass the tuple as collection. Basicly you have to create a list of dictionaries. Each of these dictionaries need to have a "name" key. In addition to that they can contain the data you want to set on the properties ("value" in this case).
The list of dicts can look like so:
[ {"name" : "", "value" : 12},
{"name" : "", "value" : 15},
{"name" : "", "value" : 20} ]
So lets say you have a list of numbers and want to transform it to this data structure:
numbers = [12, 15, 20]
# long way
packed_numbers = []
for number in numbers:
packed_numbers.append({"name" : "", "value" : number})
# more pythonic way
packed_numbers = [{"name" : "", "value" : number} for number in numbers]
The final step is to call the operator and pass these packed numbers as parameter to the collection property:
bpy.ops.my_operator.my_operator(numbers_collection = packed_numbers)