Skip to content
Snippets Groups Projects
primitives.py 79.2 KiB
Newer Older
  • Learn to ignore specific revisions
  •                             plane_search = False
    
                        if word == 'box':
                            box_search = True
                            name_search = False
                        if box_search:
                            cache.append(word)
                            if cache[-1] == '}':
                                try:
                                    x0 = float(cache[2])
                                    y0 = float(cache[3])
                                    z0 = float(cache[4])
                                    x1 = float(cache[5])
                                    y1 = float(cache[6])
                                    z1 = float(cache[7])
    
                                    # imported_corner_1=(x0, y0, z0)
                                    # imported_corner_2 =(x1, y1, z1)
                                    center = (
                                        (x0 + x1) / 2,
                                        (y0 + y1) / 2,
                                        (z0 + z1) / 2,
                                    )
    
                                    bpy.ops.pov.addbox()
                                    ob = context.object
                                    ob.location = center
    
                                    mat_search(cache)
    
                                box_search = False
    
                        if word == 'cylinder':
                            cylinder_search = True
                            name_search = False
                        if cylinder_search:
                            cache.append(word)
                            if cache[-1] == '}':
                                try:
                                    x0 = float(cache[2])
                                    y0 = float(cache[3])
                                    z0 = float(cache[4])
                                    x1 = float(cache[5])
                                    y1 = float(cache[6])
                                    z1 = float(cache[7])
    
                                    imported_cyl_loc = (x0, y0, z0)
                                    imported_cyl_loc_cap = (x1, y1, z1)
    
                                    vec = Vector(imported_cyl_loc_cap) - Vector(
                                        imported_cyl_loc
                                    )
    
                                    rot = Vector((0, 0, 1)).rotation_difference(
                                        vec
                                    )  # Rotation from Z axis.
                                    trans = rot @ Vector(
                                        (0, 0, depth / 2)
                                    )  # Such that origin is at center of the base of the cylinder.
                                    # center = ((x0 + x1)/2,(y0 + y1)/2,(z0 + z1)/2)
                                    scaleZ = (
                                        sqrt(
                                            (x1 - x0) ** 2
                                            + (y1 - y0) ** 2
                                            + (z1 - z0) ** 2
                                        )
                                        / 2
                                    )
                                    bpy.ops.pov.addcylinder(
                                        R=r,
                                        imported_cyl_loc=imported_cyl_loc,
                                        imported_cyl_loc_cap=imported_cyl_loc_cap,
                                    )
    
                                    ob = context.object
                                    ob.location = (x0, y0, z0)
                                    ob.rotation_euler = rot.to_euler()
    
                                    ob.scale = (1, 1, scaleZ)
    
                                    # scale data rather than obj?
    
                                    # bpy.ops.object.mode_set(mode='EDIT')
                                    # bpy.ops.mesh.reveal()
                                    # bpy.ops.mesh.select_all(action='SELECT')
    
                                    # bpy.ops.transform.resize(value=(1,1,scaleZ), orient_type='LOCAL')
    
                                    # bpy.ops.object.mode_set(mode='OBJECT')
    
                                    mat_search(cache)
    
    
                                except (ValueError):
                                    pass
                                cache = []
                                cylinder_search = False
                        if word == 'sphere':
                            sphere_search = True
                            name_search = False
                        if sphere_search:
                            cache.append(word)
                            if cache[-1] == '}':
    
                                x = y = z = r = 0
    
                                try:
                                    x = float(cache[2])
                                    y = float(cache[3])
                                    z = float(cache[4])
                                    r = float(cache[5])
    
                                except:
                                    x = y = z = float(cache[2])
                                    r = float(cache[3])
                                bpy.ops.pov.addsphere(R=r, imported_loc=(x, y, z))
                                ob = context.object
    
                                ob.location = (x, y, z)
                                ob.scale = (r, r, r)
    
                                mat_search(cache)
    
                                sphere_search = False
    
                        ##################End Primitives Import##################
    
                        if word == '#declare':
                            name_search = True
                        if name_search:
                            cache.append(word)
                            if word == 'mesh2':
                                name_search = False
                                if cache[-2] == '=':
                                    name = cache[-3]
                                else:
    
                            if word in {'texture', ';'}:
    
                                name_search = False
                                cache = []
                        if word == 'vertex_vectors':
    
                            cache.append(word)
    
                                cache.pop()
                                cache.pop(0)
                                cache.pop(0)
                                cache.pop(0)
                                for i in range(int(lenverts)):
    
                                    x = i * 3
                                    y = (i * 3) + 1
                                    z = (i * 3) + 2
                                    verts.append(
                                        (
                                            float(cache[x]),
                                            float(cache[y]),
                                            float(cache[z]),
                                        )
                                    )
    
                                cache = []
    
                        # if word == 'face_indices':
                        # faces_search = True
                        if word == 'texture_list':  ########
                            tex_search = True  #######
                        if tex_search:  #########
                            if (
                                word
                                not in {
                                    'texture_list',
                                    'texture',
                                    '{',
                                    '}',
                                    'face_indices',
                                }
                                and word.isdigit() == False
                            ):  ##############
                                povMats.append(word)  #################
    
                            tex_search = False  ################
    
                            cache.append(word)
    
                            if word == '}':
                                faces_search = False
                                lenfaces = cache[2]
                                cache.pop()
                                cache.pop(0)
                                cache.pop(0)
                                cache.pop(0)
                                lf = int(lenfaces)
    
                                var = int(len(cache) / lf)
    
                                        v0 = i * 3
                                        v1 = i * 3 + 1
                                        v2 = i * 3 + 2
                                        faces.append(
                                            (
                                                int(cache[v0]),
                                                int(cache[v1]),
                                                int(cache[v2]),
                                            )
                                        )
    
                                        v0 = i * 4
                                        v1 = i * 4 + 1
                                        v2 = i * 4 + 2
                                        m = i * 4 + 3
    
                                        faces.append(
                                            (
                                                int(cache[v0]),
                                                int(cache[v1]),
                                                int(cache[v2]),
                                            )
                                        )
    
                                        v0 = i * 6
                                        v1 = i * 6 + 1
                                        v2 = i * 6 + 2
                                        m0 = i * 6 + 3
                                        m1 = i * 6 + 4
                                        m2 = i * 6 + 5
                                        materials.append(
                                            (
                                                int(cache[m0]),
                                                int(cache[m1]),
                                                int(cache[m2]),
                                            )
                                        )
                                        faces.append(
                                            (
                                                int(cache[v0]),
                                                int(cache[v1]),
                                                int(cache[v2]),
                                            )
                                        )
                                # mesh = pov_define_mesh(None, verts, [], faces, name, hide_geometry=False)
                                # ob = object_utils.object_data_add(context, mesh, operator=None)
    
                                me = bpy.data.meshes.new(name)  ########
                                ob = bpy.data.objects.new(name, me)  ##########
                                bpy.context.collection.objects.link(ob)  #########
                                me.from_pydata(verts, [], faces)  ############
    
                                for mat in bpy.data.materials:  ##############
                                    blendMats.append(mat.name)  #############
                                for mName in povMats:  #####################
                                    if mName not in blendMats:  ###########
                                        povMat = bpy.data.materials.new(
                                            mName
                                        )  #################
    
                                        mat_search(cache)
    
                                    ob.data.materials.append(
                                        bpy.data.materials[mName]
                                    )  ###################
                                if materials:  ##################
                                    for i, val in enumerate(
                                        materials
                                    ):  ####################
                                        try:  ###################
                                            ob.data.polygons[
                                                i
                                            ].material_index = (
                                                val
                                            )  ####################
                                        except TypeError:  ###################
                                            ob.data.polygons[
                                                i
                                            ].material_index = int(
                                                val[0]
                                            )  ##################
    
                                blendMats = []  #########################
                                povMats = []  #########################
                                materials = []  #########################
    
                                if name in matrixes and self.import_at_cur == False:
                                    global_matrix = Matrix.Rotation(
                                        pi / 2.0, 4, 'X'
                                    )
    
                                    matrix = ob.matrix_world
                                    v = matrixes[name]
    
                                    matrix[0][0] = v[0]
                                    matrix[1][0] = v[1]
                                    matrix[2][0] = v[2]
                                    matrix[0][1] = v[3]
                                    matrix[1][1] = v[4]
                                    matrix[2][1] = v[5]
                                    matrix[0][2] = v[6]
                                    matrix[1][2] = v[7]
                                    matrix[2][2] = v[8]
                                    matrix[0][3] = v[9]
                                    matrix[1][3] = v[10]
                                    matrix[2][3] = v[11]
    
                                    matrix = global_matrix * ob.matrix_world
    
                                    ob.matrix_world = matrix
                                verts = []
                                faces = []
    
                        # if word == 'pigment':
    
                        # try:
                        # #all indices have been incremented once to fit a bad test file
                        # r,g,b,t = float(S[2]),float(S[3]),float(S[4]),float(S[5])
                        # color = (r,g,b,t)
    
                        # except (IndexError):
                        # #all indices have been incremented once to fit alternate test file
                        # r,g,b,t = float(S[3]),float(S[4]),float(S[5]),float(S[6])
                        # color = (r,g,b,t)
                        # except UnboundLocalError:
                        # # In case no transmit is specified ? put it to 0
                        # r,g,b,t = float(S[2]),float(S[3]),float(S[4],0)
                        # color = (r,g,b,t)
    
                        # except (ValueError):
                        # color = (0.8,0.8,0.8,0)
                        # pass
    
                        # if colors == [] or (colors != [] and color not in colors):
                        # colors.append(color)
                        # name = ob.name+"_mat"
                        # matNames.append(name)
                        # mat = bpy.data.materials.new(name)
                        # mat.diffuse_color = (r,g,b)
                        # mat.alpha = 1-t
                        # if mat.alpha != 1:
                        # mat.use_transparency=True
                        # ob.data.materials.append(mat)
                        # print (colors)
                        # else:
                        # for i in range(len(colors)):
                        # if color == colors[i]:
                        # ob.data.materials.append(bpy.data.materials[matNames[i]])
    
    
            ##To keep Avogadro Camera angle:
    
            # for obj in bpy.context.view_layer.objects:
    
            # if obj.type == "CAMERA":
            # track = obj.constraints.new(type = "TRACK_TO")
            # track.target = ob
            # track.track_axis ="TRACK_NEGATIVE_Z"
            # track.up_axis = "UP_Y"
            # obj.location = (0,0,0)
    
            return {'FINISHED'}
    
    classes = (
        POVRAY_OT_lathe_add,
        POVRAY_OT_superellipsoid_add,
        POVRAY_OT_superellipsoid_update,
        POVRAY_OT_supertorus_add,
        POVRAY_OT_supertorus_update,
        POVRAY_OT_loft_add,
        POVRAY_OT_plane_add,
        POVRAY_OT_box_add,
        POVRAY_OT_cylinder_add,
        POVRAY_OT_cylinder_update,
        POVRAY_OT_sphere_add,
        POVRAY_OT_sphere_update,
        POVRAY_OT_cone_add,
        POVRAY_OT_cone_update,
        POVRAY_OT_isosurface_box_add,
        POVRAY_OT_isosurface_sphere_add,
        POVRAY_OT_sphere_sweep_add,
        POVRAY_OT_blob_add,
        POVRAY_OT_rainbow_add,
        POVRAY_OT_height_field_add,
        POVRAY_OT_torus_add,
        POVRAY_OT_torus_update,
        POVRAY_OT_prism_add,
        POVRAY_OT_parametric_add,
        POVRAY_OT_parametric_update,
        POVRAY_OT_shape_polygon_to_circle_add,
        ImportPOV,
    )
    
    
    def register():
    
        # from bpy.utils import register_class
    
    
        for cls in classes:
            register_class(cls)
    
    
    def unregister():
        from bpy.utils import unregister_class
    
        for cls in classes:
            unregister_class(cls)