0
$\begingroup$

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

$\endgroup$
0
$\begingroup$
  • If you show some object (a torus) by user action it seems like a [game] description. You may want to search Blenders game engine or some other game engine.

  • The hide_render and hide Python attributes, the eye and the camera icon in the outliner window can be set to True or False to assist you in your goal. Or you may insert keyframes or individual as scripts opposed to frame change handlers.

enter image description here

  • BSE Link Show and Hide child objects along with Parent

  • Your code in your question may benefit by placing all the items in a (group) which can source of an iteration in Python (for loop) and used with other ideas later in this proposed answer. A small number of copy/paste actions can be done manually with regard to your patience.

  • If you want to an animation with no user control I suggest you see the topic of [particles]. Particles have location and render visibility which may interest you or allow you to think about your task differently. You may employ 1 or more Particle Modifiers.

  • If you do not want to use particles then with a script I suggest you use a frame change handler.

  • Please search frame_change_post and frame_change_pre

and modify the render visibility to True or False.

This proposed answer are high level remarks to guide you in the correct direction and (not) a tutorial.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.