I have a question about Python. By the way, I'm a beginner of English and Python. I have a script to show Toruses from an imported txt file containing center coordinates. Thanks to this website, it was made and worked really well. But I'm facing another problem. Although all of Toruses are showed by the script, I would like to show one of them by user's choices and erase it and show another one if the user choose the another one. Or I would like to show the Toruses one by one by using the function of Timer or Animation. Sorry for this mess explanation, but I would really appreciate if someone helped me. Here is the script and some lines(Atom`s coordinates) from my txt file.
import bpy
import math
from bpy.types import Operator
from bpy.types import Panel
class MeshGenTool():
@classmethod
def main(cls, context):
scene = context.scene
objs = importFile(scene['File'])
for obj in objs:
bpy.ops.mesh.primitive_torus_add(
location=obj[0], rotation=(math.pi/2, math.pi/2, math.pi/2))
obj=bpy.context.scene.objects.active
mat=bpy.data.materials.new('Torus')
mat.diffuse_color=(1.0, 0, 0)
mat.alpha=0.8
obj.data.materials.append(mat)
MyMesh = context.object
Mymesh.name = "MyMesh"
class MeshGenButton(bpy.types.Operator):
"""Execute generation"""
bl_idname = "object.simple_operator"
bl_label = "Generate"
@classmethod
def poll(cls, context):
scene = context.scene
filevalid = (scene.File is not "")
return filevalid is True
def execute(self, context):
MeshGenTool.main(context)
return {'FINISHED'}
def importFile(File):
obj_loc = ()
obj_dia = 1
obj_data = ()
objs = []
f = open(File, 'r')
for line in f:
if line.startswith('ATOM'):
words = line.split()
obj_loc = (float(words[6]), float(words[7]), float(words[8]))
obj_data = (obj_loc, obj_dia)
objs.append(obj_data)
return objs
class VIEW3D_PT_Blank1_Blank2(Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = "Import Atom Coords"
bl_context = "objectmode"
bl_label = "Import Coords"
def draw(self, context):
layout = self.layout
scene = context.scene
row = layout.row()
col = layout.column(align=True)
col.prop(scene, "File")
col.operator(
"object.simple_operator", text="Add items",
icon='MESH_TORUS')
def register():
bpy.utils.register_class(VIEW3D_PT_Blank1_Blank2)
bpy.utils.register_class(MeshGenButton)
bpy.types.Scene.File = bpy.props.StringProperty(
name="File",
description="Path of File to read from",
default="",
subtype='FILE_PATH')
def unregister():
bpy.utils.unregister_class(VIEW3D_PT_Blank1_Blank2)
bpy.utils.unregister_class(MeshGenButton)
del bpy.types.Scene.File
if __name__ == "__main__":
register()
CRYST1 65.961 65.961 65.961 90.00 90.00 90.00 P 1 1
ATOM 1 N GLN X 1 32.110 46.350 52.070 1.00 0.00
ATOM 2 H1 GLN X 1 32.470 45.850 52.870 1.00 0.00
ATOM 3 H2 GLN X 1 32.931 46.641 51.559 1.00 0.00
ATOM 4 H3 GLN X 1 31.568 47.112 52.451 1.00 0.00
ATOM 5 CA GLN X 1 31.200 45.480 51.230 1.00 0.00
ATOM 6 HA GLN X 1 30.402 45.041 51.829 1.00 0.00
ATOM 7 CB GLN X 1 30.390 46.250 50.180 1.00 0.00
ATOM 8 HB1 GLN X 1 30.811 46.160 49.179 1.00 0.00
