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

ant_landscape_refresh.py « ant_landscape - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae3710de97f52cef4a624a6c405b23475b0e4126 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# ##### 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 #####

# Another Noise Tool - Landscape  Redraw - Regenerate
# Jim Hazevoet


# ------------------------------------------------------------
# import modules
import bpy

from .ant_functions import (
        noise_gen,
        grid_gen,
        sphere_gen,
        create_mesh_object,
        store_properties,
        )

# ------------------------------------------------------------
# Do refresh - redraw
class AntLandscapeRefresh(bpy.types.Operator):
    bl_idname = "mesh.ant_landscape_refresh"
    bl_label = "Refresh"
    bl_description = "Refresh landscape with current settings"
    bl_options = {'REGISTER', 'UNDO'}


    @classmethod
    def poll(cls, context):
        ob = bpy.context.active_object
        return (ob.ant_landscape and not ob.ant_landscape['sphere_mesh'])


    def execute(self, context):
        # turn off undo
        undo = bpy.context.user_preferences.edit.use_global_undo
        bpy.context.user_preferences.edit.use_global_undo = False

        # ant object items
        obj = bpy.context.active_object

        bpy.ops.object.mode_set(mode = 'EDIT')
        bpy.ops.object.mode_set(mode = 'OBJECT')

        
        if obj and obj.ant_landscape.keys():
            obi = obj.ant_landscape.items()
            #print("Refresh A.N.T. Landscape Grid")
            #for k in obi.keys():
            #    print(k, "-", obi[k])
            prop = []
            for i in range(len(obi)):
                prop.append(obi[i][1])

            # redraw verts
            mesh = obj.data
            for v in mesh.vertices:
                v.co[2] = 0
                v.co[2] = noise_gen(v.co, prop)
            mesh.update()
        else:
            pass

        # restore pre operator undo state
        context.user_preferences.edit.use_global_undo = undo

        return {'FINISHED'}


# ------------------------------------------------------------
# Do regenerate
class AntLandscapeRegenerate(bpy.types.Operator):
    bl_idname = "mesh.ant_landscape_regenerate"
    bl_label = "Regenerate"
    bl_description = "Regenerate landscape with current settings"
    bl_options = {'REGISTER', 'UNDO'}


    @classmethod
    def poll(cls, context):
        return bpy.context.active_object.ant_landscape


    def execute(self, context):

        # turn off undo
        undo = bpy.context.user_preferences.edit.use_global_undo
        bpy.context.user_preferences.edit.use_global_undo = False

        scene = bpy.context.scene
        # ant object items
        obj = bpy.context.active_object

        if obj and obj.ant_landscape.keys():
            ob = obj.ant_landscape
            obi = ob.items()
            #print("Regenerate A.N.T. Landscape Grid")
            #for k in obi.keys():
            #    print(k, "-", obi[k])
            ant_props = []
            for i in range(len(obi)):
                ant_props.append(obi[i][1])

            new_name = ob.ant_terrain_name

            # Main function, create landscape mesh object
            if ob["sphere_mesh"]:
                # sphere
                verts, faces = sphere_gen(
                        ob["subdivision_y"],
                        ob["subdivision_x"],
                        ob["tri_face"],
                        ob["mesh_size"],
                        ant_props,
                        False,
                        0.0
                        )
                new_ob = create_mesh_object(context, verts, [], faces, new_name).object
            else:
                # grid
                verts, faces = grid_gen(
                        ob["subdivision_x"],
                        ob["subdivision_y"],
                        ob["tri_face"],
                        ob["mesh_size_x"],
                        ob["mesh_size_y"],
                        ant_props,
                        False,
                        0.0
                        )
                new_ob = create_mesh_object(context, verts, [], faces, new_name).object

            new_ob.select = True

            if ob["smooth_mesh"]:
                bpy.ops.object.shade_smooth()

            # Landscape Material
            if ob["land_material"] != "" and ob["land_material"] in bpy.data.materials:
                mat = bpy.data.materials[ob["land_material"]]
                bpy.context.object.data.materials.append(mat)

            # Water plane
            if ob["water_plane"]:
                if ob["sphere_mesh"]:
                    # sphere
                    verts, faces = sphere_gen(
                            ob["subdivision_y"],
                            ob["subdivision_x"],
                            ob["tri_face"],
                            ob["mesh_size"],
                            ant_props,
                            ob["water_plane"],
                            ob["water_level"]
                            )
                    wobj = create_mesh_object(context, verts, [], faces, new_name+"_plane").object
                else:
                    # grid
                    verts, faces = grid_gen(
                            2,
                            2,
                            ob["tri_face"],
                            ob["mesh_size_x"],
                            ob["mesh_size_y"],
                            ant_props,
                            ob["water_plane"],
                            ob["water_level"]
                            )
                    wobj = create_mesh_object(context, verts, [], faces, new_name+"_plane").object

                wobj.select = True

                if ob["smooth_mesh"]:
                    bpy.ops.object.shade_smooth()

                # Water Material
                if ob["water_material"] != "" and ob["water_material"] in bpy.data.materials:
                    mat = bpy.data.materials[ob["water_material"]]
                    bpy.context.object.data.materials.append(mat)

            # Loc Rot Scale
            if ob["water_plane"]:
                wobj.location = obj.location
                wobj.rotation_euler = obj.rotation_euler
                wobj.scale = obj.scale
                wobj.select = False


            new_ob.location = obj.location
            new_ob.rotation_euler = obj.rotation_euler
            new_ob.scale = obj.scale
     
            # Store props
            new_ob = store_properties(ob, new_ob)

            # Delete old object
            new_ob.select = False
            
            obj.select = True
            scene.objects.active = obj
            bpy.ops.object.delete(use_global=False)
            #scene.update()

            # Select landscape and make active
            new_ob.select = True
            scene.objects.active = new_ob

            # restore pre operator undo state
            context.user_preferences.edit.use_global_undo = undo

        return {'FINISHED'}

'''
# ------------------------------------------------------------
# Register:

def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()
'''