Welcome to mirror list, hosted at ThFree Co, Russian Federation.

oscurart_constellation.py « delaunay_voronoi « add_advanced_objects - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: adde96c0430ac5f1591c52ac9f0c5ab05e288f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

bl_info = {
    "name": "Mesh: Constellation",
    "author": "Oscurart",
    "version": (1, 1, 1),
    "blender": (2, 67, 0),
    "location": "Add > Mesh > Constellation",
    "description": "Create a new Mesh From Selected",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh"}

# Note the setting is moved to __init__ search for
# the adv_obj and advanced_objects patterns

import bpy
from bpy.types import Operator
from bpy.props import FloatProperty
from math import sqrt


def VertDis(a, b):
    dst = sqrt(pow(a.co.x - b.co.x, 2) +
               pow(a.co.y - b.co.y, 2) +
               pow(a.co.z - b.co.z, 2))
    return(dst)


def OscConstellation(limit):
    actobj = bpy.context.object
    vertlist = []
    edgelist = []
    edgei = 0

    for ind, verta in enumerate(actobj.data.vertices[:]):
        for vertb in actobj.data.vertices[ind:]:
            if VertDis(verta, vertb) <= limit:
                vertlist.append(verta.co[:])
                vertlist.append(vertb.co[:])
                edgelist.append((edgei, edgei + 1))
                edgei += 2

    mesh = bpy.data.meshes.new("rsdata")
    obj = bpy.data.objects.new("rsObject", mesh)
    bpy.context.scene.objects.link(obj)
    mesh.from_pydata(vertlist, edgelist, [])


class Oscurart_Constellation(Operator):
    bl_idname = "mesh.constellation"
    bl_label = "Constellation"
    bl_description = ("Create a Constellation Mesh - Cloud of Vertices\n"
                      "Note: can produce a lot of geometry\n"
                      "Needs an existing Active Mesh Object")
    bl_options = {'REGISTER', 'UNDO'}

    limit = FloatProperty(
            name="Threshold",
            description="Edges will be created only if the distance\n"
                        "between vertices is smaller than this value",
            default=2,
            min=0
            )

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return (obj and obj.type == "MESH")

    def invoke(self, context, event):
        adv_obj = context.scene.advanced_objects
        self.limit = adv_obj.constellation_limit

        return self.execute(context)

    def draw(self, context):
        layout = self.layout

        layout.prop(self, "limit")

    def execute(self, context):
        try:
            OscConstellation(self.limit)
        except Exception as e:
            print("\n[Add Advanced Objects]\nOperator: mesh.constellation\n{}".format(e))

            self.report({"WARNING"},
                        "Constellation Operation could not be Completed (See Console for more Info)")

            return {"CANCELLED"}

        return {'FINISHED'}


# Register

def register():
    bpy.utils.register_class(Oscurart_Constellation)


def unregister():
    bpy.utils.unregister_class(Oscurart_Constellation)


if __name__ == "__main__":
    register()