# How to write python to show Toruses from an imported txt file containing center coordinates

I have a question about Python. By the way, I'm beginner of Python and English. I wrote a script showing Toruses from an imported txt file containing center coordinates, but I got an error at

bpy.ops.mesh.primitive_torus_add(location=(int(temp[0]), int(temp[1]), int(temp[2])), rotation=(math.pi/2, math.pi/2, math.pi/2))


I would appreciate if you tell me how to solve it.
Here is my script

import bpy
import math

lines=[]

for line in f:
lines.append(line.strip())

for p in lines:
temp=p.split(',')
bpy.ops.mesh.primitive_torus_add(location=(int(temp[0]), int(temp[1]), int(temp[2])), 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)

f.close()


Here is some lines from my txt file. Actually, my file is Atom's coordinates, can I make Toruses from this??

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

– Ratt
Dec 21 '18 at 0:43
• Thank you for replying. I put some lines from the txt file. Although it is Atom's coordinates, I want to show Toruses on Blender by using it. Is it possible?? Dec 21 '18 at 1:15

Below is based on on some old code but is still functional for blender 2.79. This will add a tool panel in the view3d viewport that will allow for file selection before generating a generic torus since the atom data only provides x,y,z coordinates as relevant info.

import bpy
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:
location=obj[0])
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(
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()

• Thank you so much for your kindness. It worked very well. I really appreciate it. I have to study computer programs more and more to be able to write it like you. Dec 21 '18 at 7:45