I am trying to execute a line of code, such as: bpy.ops.object.modifier_add(type='SUBSURF')
from a string of text.
My code is:
import bpy
import math
print("~~~Separator~~~")
obj_list = []
task_list = []
class ExportSomeData(bpy.types.Operator):
"""Test exporter which just writes hello world"""
bl_idname = "export.some_data"
bl_label = "Export Some Data"
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
@classmethod
def poll(cls, context):
if not obj_list:
return False
else:
return True
def callMethod(o, name):
getattr(o, name)()
def execute(self, context):
global task_list
file = open(self.filepath, 'r+')
for line in file:
task_list.append(line)
callMethod("I don't know what to put here", line)
print(str(task_list))
file.close()
return {'FINISHED'}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
class SelectOperator(bpy.types.Operator):
"""Adds """
bl_idname = "scene.select_option"
bl_label = "Multi Select Operator"
@classmethod
def poll(cls, context):
if not bpy.context.selected_objects:
return False
else:
return True
def execute(self, context):
global obj_list
obj_list = []
for obj in bpy.context.selected_objects:
obj_list.append(obj)
print(str(obj))
print("Objects in the active list: " + str(obj_list))
return {'FINISHED'}
class TaskOperator(bpy.types.Operator):
"""Subdivide and scale a mesh."""
bl_idname = "scene.task_option"
bl_label = "Multi Task Operator"
@classmethod
def poll(cls, context):
global obj_list
if not obj_list:
return False
else:
return True
def execute(self, context):
global obj_list
return {'FINISHED'}
class SelectionPanel(bpy.types.Panel):
bl_label = "Multi Selection Options"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
def draw(self, context):
layout = self.layout
row = layout.row()
split = layout.split()
col = split.column(align=True)
'row.operator("mesh.primitive_uv_sphere_add")'
col.operator("scene.select_option", text = "Set Objects", icon = "MESH_CUBE")
'row.operator("scene.task_option", text = "Do Task", icon = "LONGDISPLAY")'
col.operator("export.some_data", text = "Open And Do Task", icon = "FILE")
def register():
bpy.utils.register_class(SelectionPanel)
bpy.utils.register_class(SelectOperator)
bpy.utils.register_class(TaskOperator)
bpy.utils.register_class(ExportSomeData)
def unregister():
bpy.utils.unregister_class(SelectionPanel)
bpy.utils.unregister_class(SelectOperator)
bpy.utils.unregister_class(TaskOperator)
bpy.utils.unregister_class(ExportSomeData)
if __name__ == "__main__":
register()
Below is what is causing me trouble:
def callMethod(o, name):
getattr(o, name)()
def execute(self, context):
global task_list
file = open(self.filepath, 'r+')
for line in file:
task_list.append(line)
callMethod("I don't know what to put here", line)
print(str(task_list))
file.close()
return {'FINISHED'}
line is bpy.ops.object.modifier_add(type='SUBSURF')
, but I don't know the object to put for the first parameter. If I am going the wrong direction with this, please correct me.