-
Notifications
You must be signed in to change notification settings - Fork 1
/
arx_object.py
406 lines (313 loc) · 9.79 KB
/
arx_object.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# ##### 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 #####
import bpy
from . arx_base import Base_Material
import bmesh
from mathutils import Vector
from math import radians
import math
class OP_Align_To_Camera(bpy.types.Operator):
bl_idname="object.align_to_camera"
bl_label="align_to_camera"
def execute(self,context):
for object in bpy.context.selected_objects:
x = object.location.x
y = object.location.y
z = object.location.z
camera = bpy.data.objects['Camera']
object.matrix_world = camera.matrix_world
object.location.x = x
object.location.y = y
object.location.z = z
return {'FINISHED'}
def invoke(self,context,event):
return self.execute(context)
class OP_Align_To_Sun(bpy.types.Operator):
bl_idname="object.align_to_sun"
bl_label="align_to_sun"
def execute(self,context):
for object in bpy.context.selected_objects:
x = object.location.x
y = object.location.y
z = object.location.z
sun = bpy.data.objects['Lamp']
object_matrix = object.matrix_basis
object_euler = object_matrix.to_euler()
euler = sun.matrix_basis.to_euler()
euler[0]=math.pi/2.0
matrix = euler.to_matrix()
new = matrix.to_4x4()
object.matrix_basis = new
object.location.x = x
object.location.y = y
object.location.z = z
return {'FINISHED'}
def invoke(self,context,event):
return self.execute(context)
# UnBuild Shadow
class OP_UnBuild_Shadow(Base_Material,bpy.types.Operator):
bl_idname="object.unbuild_shadow"
bl_label="unbuild_shadow"
# Mesh Must Have Exactly 2 Polygons
@classmethod
def poll(self, context):
obj = context.active_object
if obj != None and obj.type == 'MESH':
me = obj.data
p = me.polygons
if(len(p)==2):
return 1
else:
return 0
else:
return 0
def exec(self,context):
ob=bpy.context.active_object
me=ob.data
p=me.polygons
# EDIT MODE
if not (ob.mode == 'EDIT'):
bpy.ops.object.mode_set(mode='EDIT')
# Delete
bpy.ops.mesh.delete()
# Update Mesh
bpy.ops.object.mode_set(mode='OBJECT')
me.update()
p = me.polygons
p[0].select = 1
return {'FINISHED'}
def invoke(self,context,event):
return self.exec(context)
# Build Shadow
class OP_Build_Shadow(Base_Material,bpy.types.Operator):
bl_idname="object.build_shadow"
bl_label="build_shadow"
bl_description="build shadow"
@classmethod
# Mesh Must Have Exactly 1 Polygon
def poll(self, context):
obj = context.active_object
if obj != None and obj.type == 'MESH':
me = obj.data
p = me.polygons
if(len(p)==1):
return 1
else:
return 0
else:
return 0
def execute(self,context):
ob=bpy.context.active_object
me=ob.data
p=me.polygons
# EDIT MODE
is_editmode = (ob.mode == 'EDIT')
if not is_editmode:
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(me)
uv_layer = bm.loops.layers.uv.verify()
bm.faces.layers.tex.verify() # currently blender needs both layers.
# Vectors
v0=[0,0,0]
v1=[0,0,0]
shadow_mat=0
has_shadow_mat=0
# Get UVs
for f in bm.faces:
for l in f.loops:
luv = l[uv_layer]
# Get (0,0)
if(luv.uv[0] == 0.0 and luv.uv[1] == 0.0):
v0=l.vert.co.xyz
# Get (1,0)
elif(luv.uv[0] == 0.0 and luv.uv[1] == 1.0):
v1=l.vert.co.xyz
#bmesh.update_edit_mesh(me)
# Build Vector
vv0 = Vector(v0)
vv1 = Vector(v1)
vv = vv1 - vv0
# Apply Rotation Matrix
rot = ob.rotation_euler
vv.rotate(rot)
# Check For Shadow Material
for mat in me.materials:
if mat.arx_material_is_shadow:
shadow_material = mat
has_shadow_mat = 1
# Build default Shadow Material
if not has_shadow_mat:
shadow_mat = self.build_shadow_material(self,ob)
# Duplicate
bpy.ops.mesh.duplicate()
bpy.ops.transform.rotate(value=radians(90.0),axis=vv)
# Update Mesh
bpy.ops.object.mode_set(mode='OBJECT')
me.update()
# Set Shadow Material
p = ob.data.polygons
p[1].material_index = 1
return {'FINISHED'}
def invoke(self,context,event):
return self.execute(context)
class OP_Build_Shadow_m(Base_Material,bpy.types.Operator):
bl_idname="object.build_shadow_m"
bl_label="build_shadow_m"
bl_description="build shadow"
@classmethod
def poll(self, context):
obj = context.active_object
return obj != None and obj.type == 'MESH'
def execute(self,context):
print("execute")
objs = bpy.context.selected_objects
for ob in objs:
bpy.context.scene.objects.active = ob
print("active:"+ob.name)
bpy.ops.object.build_shadow()
return {'FINISHED'}
def invoke(self, context,event):
print("invoke!")
return self.execute(context)
# SET DISPLAY OPTIONS
class OP_Set_Options(bpy.types.Operator):
bl_idname = "object.set_options"
bl_label = "Set Materials"
bl_description = "Set materials"
def invoke(self,context,event):
for ob in bpy.data.objects:
if ob.type == 'MESH':
mesh = ob.data
if bpy.context.scene.arx_wire_do:
if bpy.context.scene.arx_wire:
ob.show_wire = True
else:
ob.show_wire = False
if bpy.context.scene.arx_normals_do:
if bpy.context.scene.arx_normals:
mesh.show_normal_face = True
else:
mesh.show_normal_face = False
if bpy.context.scene.arx_length_do:
if bpy.context.scene.arx_length:
mesh.show_extra_edge_length = True
else:
mesh.show_extra_edge_length = False
if bpy.context.scene.arx_name_do:
if bpy.context.scene.arx_name:
ob.show_name = True
else:
ob.show_name = False
return {'FINISHED'}
# APPLY MODIFIERS
class OP_Apply_all_modifiers(bpy.types.Operator):
bl_idname = "object.apply_all_modifiers"
bl_label = "Set Materials"
bl_description = "Set materials"
def invoke(self,context,event):
for ob in bpy.data.objects:
if ob.type == 'MESH':
bpy.context.scene.objects.active = ob
mods = ob.modifiers
for m in mods:
if bpy.context.scene.arx_only_array == True:
if m.type == "ARRAY":
bpy.ops.object.modifier_apply(apply_as='DATA',modifier=m.name)
else:
bpy.ops.object.modifier_apply(apply_as='DATA',modifier=m.name)
return {'FINISHED'}
# RENAME OBJECTS
class OP_Rename_selected(bpy.types.Operator):
bl_idname = "object.rename_selected"
bl_label = "Set Materials"
bl_description = "Set materials"
i = 0
def invoke(self,context,event):
i=0
objs=bpy.context.selected_objects
for ob in objs:
ob.name = bpy.context.scene.arx_rename + "_" + str(self.i)
self.i+=1
return {'FINISHED'}
# PANEL OBJECT
class RVBA_Display_Panel(bpy.types.Panel):
bl_space_type='PROPERTIES'
bl_region_type='WINDOW'
bl_context="object"
bl_label="Object Tools"
bpy.types.Scene.arx_wire = bpy.props.BoolProperty("arx_wire",default=False)
bpy.types.Scene.arx_wire_do = bpy.props.BoolProperty("arx_wire_do",default=False)
bpy.types.Scene.arx_normals = bpy.props.BoolProperty("arx_normals",default=False)
bpy.types.Scene.arx_normals_do = bpy.props.BoolProperty("arx_normals_do",default=False)
bpy.types.Scene.arx_length = bpy.props.BoolProperty("arx_length",default=False)
bpy.types.Scene.arx_length_do = bpy.props.BoolProperty("arx_length_do",default=False)
bpy.types.Scene.arx_name = bpy.props.BoolProperty("arx_name",default=False)
bpy.types.Scene.arx_name_do = bpy.props.BoolProperty("arx_name_do",default=False)
bpy.types.Scene.arx_only_array = bpy.props.BoolProperty("arx_only_array",default=False)
bpy.types.Scene.arx_rename = bpy.props.StringProperty("arx_rename")
def draw(self,context):
# DISPLAY
col=self.layout.column(align=True)
col.label("Display",icon='MODIFIER')
col.separator()
row=col.row()
row.alignment = 'LEFT'
row.prop(context.scene,"arx_wire_do",text="")
row.prop(context.scene,"arx_wire",text="wire")
row.operator(OP_Set_Options.bl_idname,text="Set Options")
row=col.row()
row.alignment = 'LEFT'
row.prop(context.scene,"arx_normals_do",text="")
row.prop(context.scene,"arx_normals",text="normals")
row=col.row()
row.alignment = 'LEFT'
row.prop(context.scene,"arx_length_do",text="")
row.prop(context.scene,"arx_length",text="length")
row=col.row()
row.alignment = 'LEFT'
row.prop(context.scene,"arx_name_do",text="")
row.prop(context.scene,"arx_name",text="name")
col.separator()
# MODIFIERS
col=self.layout.column(align=True)
col.label("Modifier",icon='MODIFIER')
col.separator()
row = col.row()
row.prop(context.scene,"arx_only_array",text="only array")
row.operator(OP_Apply_all_modifiers.bl_idname,text="Apply all modifiers")
col.separator()
# NAMES
col=self.layout.column(align=True)
col.label("Names",icon='MODIFIER')
col.separator()
row = col.row()
row.prop(context.scene,"arx_rename",text="")
row = col.row()
row.operator(OP_Rename_selected.bl_idname,text="Rename selected")
# SHADOW
col=self.layout.column(align=True)
col.label("Shadow",icon='MODIFIER')
col.separator()
row = col.row()
row.operator(OP_Build_Shadow.bl_idname,text="Build Shadow")
row.operator(OP_UnBuild_Shadow.bl_idname,text="UnBuild Shadow")
row = col.row()
row.operator(OP_Build_Shadow_m.bl_idname,text="Build ShadowS")
row = col.row()
row.operator(OP_Align_To_Camera.bl_idname,text="Align To Camera")
row.operator(OP_Align_To_Sun.bl_idname,text="Align To Sun")