forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_meshtex_pattern.py
204 lines (153 loc) · 6.59 KB
/
op_meshtex_pattern.py
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
import bpy
import math
from . import utilities_ui
from . import settings
class op(bpy.types.Operator):
bl_idname = "uv.textools_meshtex_pattern"
bl_label = "Create Pattern"
bl_description = "Create a certain Mesh pattern"
bl_options = {'REGISTER', 'UNDO'}
mode : bpy.props.EnumProperty(items=
[('hexagon', 'Hexagons', ''),
('triangle', 'Triangles', ''),
('diamond', 'Diamonds', ''),
('rectangle', 'Rectangles', ''),
('stripe', 'Stripes', ''),
('brick', 'Bricks', '')],
name = "Mode",
default = 'brick'
)
size : bpy.props.IntProperty(
name = "Size",
description = "Size X and Y of the repetition",
default = 4,
min = 1,
max = 128
)
scale : bpy.props.FloatProperty(
name = "Scale",
description = "Scale of the mesh pattern",
default = 1,
min = 0
)
@classmethod
def poll(cls, context):
if bpy.context.active_object and bpy.context.active_object.mode != 'OBJECT':
return False
return True
def draw(self, context):
layout = self.layout
layout.prop(self, "mode")
layout.prop(self, "size")
layout.prop(self, "scale")
def execute(self, context):
create_pattern(self, self.mode, self.size, self.scale)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def AddArray(name, offset_x, offset_y, count):
modifier = bpy.context.object.modifiers.new(name=name, type='ARRAY')
# modifier = bpy.context.object.modifiers.new(name="{}_{}".format(name,count), type='ARRAY')
modifier.relative_offset_displace[0] = offset_x
modifier.relative_offset_displace[1] = offset_y
modifier.count = count
modifier.show_expanded = False
return modifier
def create_pattern(self, mode, size, scale):
# bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
context_override = None
if bpy.context.area.type != 'VIEW_3D':
context_override = utilities_ui.GetContextView3D()
if not context_override:
self.report({'ERROR_INVALID_INPUT'}, "This tool requires an available View3D view.")
return {'CANCELLED'}
if mode == 'hexagon':
bpy.ops.mesh.primitive_circle_add(vertices=6, radius=scale, fill_type='NGON')
bpy.ops.object.mode_set(mode = 'EDIT')
if context_override:
if settings.bversion >= 3.2:
with bpy.context.temp_override(**context_override):
bpy.ops.transform.rotate(value=math.pi*0.5, orient_axis='Z')
else:
bpy.ops.transform.rotate(context_override, value=math.pi*0.5, orient_axis='Z')
else:
bpy.ops.transform.rotate(value=math.pi*0.5, orient_axis='Z')
bpy.ops.object.mode_set(mode = 'OBJECT')
AddArray("Array0", 0.75,-0.5,2)
AddArray("Array1", 0,-0.66666666666,size)
AddArray("Array2", 1 - (0.5/3.5),0,int(size*0.66))
elif mode == 'triangle':
bpy.ops.mesh.primitive_circle_add(vertices=3, radius=scale, fill_type='NGON')
bpy.ops.object.mode_set(mode = 'EDIT')
if context_override:
if settings.bversion >= 3.2:
with bpy.context.temp_override(**context_override):
bpy.ops.transform.translate(value=(0, scale*0.5, 0), constraint_axis=(False, True, False))
else:
bpy.ops.transform.translate(context_override, value=(0, scale*0.5, 0), constraint_axis=(False, True, False))
else:
bpy.ops.transform.translate(value=(0, scale*0.5, 0), constraint_axis=(False, True, False))
bpy.ops.object.mode_set(mode = 'OBJECT')
modifier = bpy.context.object.modifiers.new(name="Mirror", type='MIRROR')
modifier.use_axis[0] = False
modifier.use_axis[1] = True
modifier.show_expanded = False
AddArray("Array0", 0.5,-0.5,2)
AddArray("Array1", 1-1/3.0,0,size)
AddArray("Array1", 0,-(1-1/3.0),int(size*0.66))
elif mode == 'rectangle':
bpy.ops.mesh.primitive_plane_add(size=scale)
AddArray("Array0", 1,0,size)
AddArray("Array1", 0,-1,size)
elif mode == 'diamond':
bpy.ops.mesh.primitive_plane_add(size=scale)
bpy.ops.object.mode_set(mode = 'EDIT')
if context_override:
if settings.bversion >= 3.2:
with bpy.context.temp_override(**context_override):
bpy.ops.transform.rotate(value=math.pi*0.25, orient_axis='Z')
else:
bpy.ops.transform.rotate(context_override, value=math.pi*0.25, orient_axis='Z')
else:
bpy.ops.transform.rotate(value=math.pi*0.25, orient_axis='Z')
bpy.ops.object.mode_set(mode = 'OBJECT')
AddArray("Array0", 0.5,-0.5,2)
AddArray("Array1", 1-1/3,0,size)
AddArray("Array2", 0,-(1-1/3),size)
elif mode == 'brick':
bpy.ops.mesh.primitive_plane_add(size=scale)
bpy.ops.object.mode_set(mode = 'EDIT')
if context_override:
if settings.bversion >= 3.2:
with bpy.context.temp_override(**context_override):
bpy.ops.transform.resize(value=(1, 0.5, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
else:
bpy.ops.transform.resize(context_override, value=(1, 0.5, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
else:
bpy.ops.transform.resize(value=(1, 0.5, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.object.mode_set(mode = 'OBJECT')
AddArray("Array0", 0.5,-1,2)
AddArray("Array1", 1-(1/3),0,size)
AddArray("Array2", 0,-1,size)
elif mode == 'stripe':
bpy.ops.mesh.primitive_plane_add(size=1)
bpy.ops.object.mode_set(mode = 'EDIT')
if context_override:
if settings.bversion >= 3.2:
with bpy.context.temp_override(**context_override):
bpy.ops.transform.resize(value=(0.5, size/2, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.transform.resize(value=(scale, scale, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.transform.translate(value=(0, (-size/2)*scale, 0), constraint_axis=(False, True, False))
else:
bpy.ops.transform.resize(context_override, value=(0.5, size/2, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.transform.resize(context_override, value=(scale, scale, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.transform.translate(context_override, value=(0, (-size/2)*scale, 0), constraint_axis=(False, True, False))
else:
bpy.ops.transform.resize(value=(0.5, size/2, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.transform.resize(value=(scale, scale, 1), constraint_axis=(True, True, False), orient_type='GLOBAL')
bpy.ops.transform.translate(value=(0, (-size/2)*scale, 0), constraint_axis=(False, True, False))
bpy.ops.object.mode_set(mode = 'OBJECT')
AddArray("Array0", 1,0, size)
if bpy.context.object:
bpy.context.object.name = "pattern_{}".format(mode)