forked from artistCDMJ/artist_paint_panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.py
1984 lines (1624 loc) · 73.8 KB
/
ops.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf8 -*-
#
# ##### 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 #####
# <pep8 compliant>
import bpy, os, platform, math
from bpy.props import *
from bpy.types import Operator
from .props import main_canvas_data, get_addon_preferences
from .utils import *
MASK_MESSAGE = "Name the mask, please..."
CURVE_MESSAGE = "Name the curve, please..."
#######################
# Classes #
#######################
#-------------------------------------------------------CHANGE TO GLSL VIEW MODE
class GLSLViewMode(Operator):
bl_description = "GLSL Mode"
bl_idname = "ez_draw.glsl"
bl_label = "GLSL View Mode"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(cls, context):
A = context.scene.viewmode_toggle
return not A
def execute(self, context):
context.scene.game_settings.material_mode = 'GLSL'
A = context.scene.viewmode_toggle
context.scene.viewmode_toggle = False if A else True
return {'FINISHED'}
#-----------------------------------------------CHANGE TO MULTITEXTURE VIEW MODE
class MTViewMode(Operator):
bl_description = "Multitexture Mode"
bl_idname = "ez_draw.multitexture"
bl_label = "Multi-Texture View Mode - Single Texture View"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(cls, context):
A = context.scene.viewmode_toggle
return A
def execute(self, context):
context.scene.game_settings.material_mode = 'MULTITEXTURE'
A = context.scene.viewmode_toggle
context.scene.viewmode_toggle = False if A else False
return {'FINISHED'}
#---------------------------------------------------------------LOAD MAIN CANVAS
class ImageLoad(Operator):
bl_description = "Load the Main Canvas to Paint"
bl_idname = "ez_draw.image_load"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
def invoke(self, context, event):
OBJ = context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
scene = context.scene
filePATH = self.filepath
fileName = os.path.split(filePATH)[-1]
fileDIR = os.path.dirname(filePATH)
#os.path.realpath
#Init
scene.ezdraw.clear()
bpy.ops.view3d.snap_cursor_to_center()
bpy.ops.ez_draw.image_to_canvas(\
files=[{"name":fileName,"name":fileName}],
directory=fileDIR,
filter_image=True,
filter_movie=True,
use_transparency=True)
data = context.active_object.data
select_mat = data.materials[0].texture_slots[0].texture.image.size[:]
main_canvas = scene.ezdraw.add()
main_canvas.filename = fileName
main_canvas.path = fileDIR
main_canvas.dimX = select_mat[0]
main_canvas.dimY = select_mat[1]
scene.maincanvas_is_empty = False
for main_canvas in scene.ezdraw:
print(main_canvas.filename)
print(main_canvas.path)
print(str(main_canvas.dimX))
print(str(main_canvas.dimY))
#set the cursor snap on object faces
userpref = context.user_preferences
userpref.view.use_mouse_depth_cursor = True
return {'FINISHED'}
#-------------------------------------------------------------------RELOAD IMAGE
class ImageReload(Operator):
"""Reload Image Last Saved State"""
bl_description = "Reload selected slot's image"
bl_idname = "ez_draw.reload_saved_state"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
A = obj is not None
if A:
B = obj.type == 'MESH'
return B
def execute(self, context):
original_type = context.area.type
context.area.type = 'IMAGE_EDITOR'
obdat = context.active_object.data
ima = obdat.materials[0].texture_slots[0].texture.image
context.space_data.image = ima
bpy.ops.image.reload() #return image to last saved state
context.area.type = original_type
return {'FINISHED'}
#---------------------------------------------------------------------IMAGE SAVE
class SaveImage(Operator):
"""Overwrite Image to Disk"""
bl_description = ""
bl_idname = "ez_draw.save_current"
bl_label = "Save Image Current"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
A = obj is not None
if A:
B = obj.type == 'MESH'
return B
def execute(self, context):
obj = context.active_object
#save the original state
original_type = context.area.type
context.area.type = 'IMAGE_EDITOR'
ima = obj.data.materials[0].texture_slots[0].texture.image
context.space_data.image = ima
bpy.ops.image.save_as() #save as
#reinstall the original state
context.area.type = original_type
return {'FINISHED'}
#---------------------------------------------------------------------IMAGE SAVE
class SaveIncremImage(Operator):
"""Save Incremential Images - MUST SAVE SESSION FILE FIRST"""
bl_description = ""
bl_idname = "ez_draw.save_increm"
bl_label = "Save incremential Image Current"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
A = obj is not None
if A:
B = obj.type == 'MESH'
return B
def execute(self, context):
scene =context.scene
main_canvas = main_canvas_data(self, context)
if main_canvas[0] != '': #if main canvas isn't erased
for obj in scene.objects:
if obj.name == main_canvas[0] : #if mainCanvas Mat exist
scene.objects.active = obj
break
else:
return {'FINISHED'}
#init
obj = context.active_object
original_type = context.area.type
context.area.type = 'IMAGE_EDITOR'
"""
~/.config/blender/Brushes/13_Tâche_de_café/Cafeina (1).png
/media/patrinux/Autre/VISTA/Patrick/Pictures/1_CAPTURES
realpath corrige the path à la racine!
/home/patrinux/.config/blender/Brushes/13_Tâche_de_café/Cafeina (1).png
/media/patrinux/Autre/VISTA/Patrick/Pictures/1_CAPTURES
"""
#verify the brushname
_tempName = [main_canvas[0] + '_001' + main_canvas[1]]
_Dir = os.path.realpath(main_canvas[2])
l = os.listdir(_Dir)
brushesName = [ f for f in l if os.path.isfile(os.path.join(_Dir,f)) ]
brushesName = sorted(brushesName)
i = 1
for x in _tempName:
for ob in brushesName:
if ob == _tempName[-1]:
i += 1
_tempName = _tempName + [main_canvas[0] + '_' + \
'{:03d}'.format(i) + main_canvas[1]]
#return image to last saved state
filepath = os.path.join(_Dir,_tempName[-1])
ima = obj.data.materials[0].texture_slots[0].texture.image
context.space_data.image = ima
bpy.ops.image.save_as(filepath = filepath,
check_existing=False,
relative_path=True)
context.area.type = original_type
return {'FINISHED'}
#-------------------------------------------------------------------CREATE BRUSH
class BrushMakerScene(Operator):
"""Create Brush Scene"""
bl_description = ""
bl_idname = "ez_draw.create_brush_scene"
bl_label = "Create Scene for Image Brush Maker"
bl_options = {'REGISTER', 'UNDO'}
scene_name = bpy.props.StringProperty(name="Scene Name", default="Brush")
@classmethod
def poll(self, context):
for sc in bpy.data.scenes:
if sc.name == self.scene_name:
return False
return context.area.type=='VIEW_3D'
def execute(self, context):
for sc in bpy.data.scenes:
if sc.name == self.scene_name:
return {'FINISHED'}
bpy.ops.scene.new(type='NEW') #add new scene & name it 'Brush'
context.scene.name = self.scene_name
#add lamp and move up 4 units in z
bpy.ops.object.lamp_add(
type = 'POINT',
radius = 1,
view_align = False,
location = (0, 0, 4)
)
#add camera to center and move up 4 units in Z
bpy.ops.object.camera_add(
view_align=False,
enter_editmode=False,
location=(0, 0, 4),
rotation=(0, 0, 0)
)
context.object.name="Tex Camera" #rename selected camera
#change scene size to 1K
_RenderScene = context.scene.render
_RenderScene.resolution_x=1024
_RenderScene.resolution_y=1024
_RenderScene.resolution_percentage = 100
#save scene size as preset
bpy.ops.render.preset_add(name = "1K Texture")
#change to camera view
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
override = bpy.context.copy()
override['area'] = area
bpy.ops.view3d.viewnumpad(override, type = 'CAMERA')
break # this will break the loop after the first ran
return {'FINISHED'}
#-------------------------------------------------------------FRONT OF CCW15 ROT
class FrontOfCCW(Operator):
"""front of face CCW15 rotate"""
bl_description = ""
bl_idname = "ez_draw.frontof_ccw"
bl_label = "Front Of CCW15 rotation"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
if obj is not None:
A = context.mode == 'PAINT_TEXTURE'
B = obj.type == 'MESH'
return A and B
def execute(self, context):
#init
paint = bpy.ops.paint
addon_prefs = get_addon_preferences()
CustomAngle = math.radians(addon_prefs.customAngle)
paint.texture_paint_toggle() #return in object mode
bpy.ops.transform.rotate(value=-CustomAngle,
constraint_orientation='NORMAL')
paint.texture_paint_toggle() #return in paint mode
return {'FINISHED'}
#-----------------------------------------------------------------FRONT OF PAINT
class FrontOfPaint(Operator):
"""fast front of face view paint"""
bl_description = ""
bl_idname = "ez_draw.frontof_paint"
bl_label = "Front Of Paint"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
if obj is not None:
A = context.mode == 'PAINT_TEXTURE'
B = obj.type == 'MESH'
return A and B
def execute(self, context):
#init
paint = bpy.ops.paint
object = bpy.ops.object
contextObj = context.object
context.space_data.viewport_shade = 'TEXTURED' #texture draw
paint.texture_paint_toggle()
object.editmode_toggle()
bpy.ops.view3d.viewnumpad(type='TOP', align_active=True)
object.editmode_toggle()
paint.texture_paint_toggle()
contextObj.data.use_paint_mask = True
return {'FINISHED'}
#--------------------------------------------------------------FRONT OF CW15 ROT
class FrontOfCW(Operator):
"""fast front of face CW15 rotate"""
bl_description = ""
bl_idname = "ez_draw.frontof_cw"
bl_label = "Front Of CW15 rotation"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
if obj is not None:
A = context.mode == 'PAINT_TEXTURE'
B = obj.type == 'MESH'
return A and B
def execute(self, context):
#init
paint = bpy.ops.paint
addon_prefs = get_addon_preferences()
CustomAngle = math.radians(addon_prefs.customAngle)
paint.texture_paint_toggle() #return in object mode
bpy.ops.transform.rotate(value=+CustomAngle,
constraint_orientation='NORMAL')
paint.texture_paint_toggle() #return in paint mode
return {'FINISHED'}
#---------------------------------------------------------------CAMERAVIEW PAINT
class CameraviewPaint(Operator):
"""Create a front-of camera in painting mode"""
bl_description = ""
bl_idname = "ez_draw.cameraview_paint"
bl_label = "Cameraview Paint"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
scene = context.scene
#Init
obj = context.active_object
empty = scene.maincanvas_is_empty
main_canvas = main_canvas_data(self, context)
if empty or main_canvas[0] == '': #if no main canvas!
return False
if obj is None: #If no active object!
return False
return obj.name == main_canvas[0]
def execute(self, context):
scene = context.scene
#init
main_canvas = main_canvas_data(self, context)
if main_canvas[0] != '' and scene.camera_is_setup == False:
_camName = "Camera_" + main_canvas[0]
for obj in scene.objects:
if obj.name == main_canvas[0] :
scene.objects.active = obj
break
else:
scene.camera_is_setup = False
return {'FINISHED'}
obj = context.active_object
context.space_data.viewport_shade = 'TEXTURED' #texture draw option
context.object.active_material.use_shadeless = True #shadeless option
for cam in bpy.data.objects:
if cam.name == _camName:
prefix = 'Already found a camera for this image : '
bpy.ops.error.message('INVOKE_DEFAULT',
message = prefix + _camName,
confirm ="error.ok0" )
return {'FINISHED'}
bpy.ops.view3d.snap_cursor_to_center() #Cursor to center of world
bpy.ops.view3d.snap_selected_to_cursor(use_offset=False)
#add camera
bpy.ops.object.camera_add(view_align=False,\
enter_editmode=False,\
location=(0, 0, 0),\
rotation=(0, 0, 0),\
layers=(True, False, False, False, False,\
False, False, False, False, False,\
False, False, False, False, False,\
False, False, False, False, False))
context.scene.render.resolution_percentage = 100 #ratio full
context.object.name = _camName #name it
bpy.ops.view3d.object_as_camera() #switch to camera view
context.object.data.type = 'ORTHO' #ortho view 4 cam
context.object.data.dof_object= obj
#move cam up in Z by 1 unit
bpy.ops.transform.translate(value=(0, 0, 1),
constraint_axis=(False, False, True),
constraint_orientation='GLOBAL',
mirror=False,
proportional='DISABLED',
proportional_edit_falloff='SMOOTH',
proportional_size=1)
#resolution
rnd = scene.render
rndx = rnd.resolution_x = main_canvas[3]
rndy = rnd.resolution_y = main_canvas[4]
print(rndx)
print(rndy)
#orthoscale
orthoscale = (rndx / rndy) if (rndx >= rndy) else 1
context.object.data.ortho_scale = orthoscale
scene.camera_is_setup = True #Camera is setup & movement are authorized
bpy.ops.object.select_all(action='TOGGLE')
bpy.ops.object.select_all(action='DESELECT') #init Selection
#select canvas
obj.select = True
context.scene.objects.active = obj
bpy.ops.paint.texture_paint_toggle()
scene.game_settings.material_mode = 'GLSL'
context.space_data.lock_camera = False
return {'FINISHED'}
#-----------------------------------------------------------------BORDER CROP ON
class BorderCrop(Operator):
"""Turn on Border Crop in Render Settings"""
bl_description = "Border Crop ON"
bl_idname = "ez_draw.border_crop"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
rs = context.scene.render
rs.use_border = True
rs.use_crop_to_border = True
return {'FINISHED'}
#----------------------------------------------------------------BORDER CROP OFF
class BorderUnCrop(Operator):
"""Turn off Border Crop in Render Settings"""
bl_description = "Border Crop OFF"
bl_idname = "ez_draw.border_uncrop"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
def execute(self, context):
rs = context.scene.render
rs.use_border = False
rs.use_crop_to_border = False
return {'FINISHED'}
#-------------------------------------------------------------BORDER CROP TOGGLE
class BorderCropToggle(Operator):
"""Set Border Crop in Render Settings"""
bl_description = "Border Crop On/Off TOGGLE"
bl_idname = "ez_draw.border_toggle"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
return poll_apt(self, context)
def execute(self, context):
scene = context.scene
rs = context.scene.render
if not(scene.prefs_are_locked):
if rs.use_border and rs.use_crop_to_border:
bpy.ops.ez_draw.border_uncrop()
scene.bordercrop_is_activated = False
else:
bpy.ops.ez_draw.border_crop()
scene.bordercrop_is_activated = True
return {'FINISHED'}
#------------------------------------------------------------------CAMERA GUIDES
class CamGuides(Operator):
"""Turn on Camera Guides"""
bl_description = "Camera Guides On/Off Toggle"
bl_idname = "ez_draw.guides_toggle"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
return poll_apt(self, context)
def execute(self, context):
scene = context.scene
_bool03 = scene.prefs_are_locked
main_canvas = main_canvas_data(self, context)
if main_canvas[0] != '': #if main canvas isn't erased
_camName = "Camera_" + main_canvas[0]
else:
return {'FINISHED'}
for cam in bpy.data.objects :
if cam.name == _camName:
if not(_bool03):
if not(scene.guides_are_activated): #True= if no guides
cam.data.show_guide = {'CENTER', 'THIRDS', 'CENTER_DIAGONAL'}
scene.guides_are_activated = True
else:
cam.data.show_guide = set() #False=> if guides
scene.guides_are_activated = False
return {'FINISHED'}
#------------------------------------------------------------PREFS TOOGLE BUTTON
class PrefsLockToggle(Operator):
"""Lock bordercrop & guides preferences in viewport"""
bl_description = "Prefs lock On/Off TOGGLE"
bl_idname = "ez_draw.prefs_lock_toggle"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
return poll_apt(self, context)
def execute(self, context):
addon_prefs = get_addon_preferences()
scene = context.scene
A = scene.prefs_are_locked
bordercrop_is_activated = scene.bordercrop_is_activated
guides_are_activated = scene.guides_are_activated
main_canvas = main_canvas_data(self, context)
if main_canvas[0] != '': #if main canvas isn't erased
_camName = "Camera_" + main_canvas[0]
else:
return {'FINISHED'}
if addon_prefs.bordercrop:
bpy.ops.ez_draw.border_crop()
else:
bpy.ops.ez_draw.border_uncrop()
for cam in bpy.data.objects :
if cam.name == _camName:
if not(guides_are_activated) and addon_prefs.guides:
cam.data.show_guide = {'CENTER', 'THIRDS', 'CENTER_DIAGONAL'}
scene.guides_are_activated = True
else:
cam.data.show_guide = set() #False = guides visible
scene.guides_are_activated = False
break
scene.prefs_are_locked = False if A else True
return {'FINISHED'}
#----------------------------------------------------GPENCIL TO MASK IN ONE STEP
class TraceSelection(Operator):
"""Mesh mask from gpencil lines"""
bl_idname = "ez_draw.trace_selection"
bl_label = "Make Mesh Mask from Gpencil's drawing"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
if obj is not None:
A = context.mode == 'PAINT_TEXTURE'
B = obj.type == 'MESH'
C = False
GP = bpy.data.grease_pencil
for lay0 in GP:
if lay0.name == 'GPencil':
if GP['GPencil'].layers.find('GP_Layer')!= -1:
C = True
break
break
return A and B and C
mask_name = StringProperty(name="Mask name")
def invoke(self, context, event):
global MASK_MESSAGE
self.mask_name = MASK_MESSAGE
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
scene = context.scene
tool_settings = scene.tool_settings
objOPS = bpy.ops.object
gpencilOPS = bpy.ops.gpencil
paintOPS = bpy.ops.paint
meshOPS = bpy.ops.mesh
cvOPS = bpy.ops.curve
main_canvas = main_canvas_data(self, context)
#----------------------------------------------INIT MAIN CANVAS
if main_canvas[0] != '': #if main canvas isn't erased
for obj in scene.objects:
if obj.name == main_canvas[0] : #if mainCanvas Mat exist
scene.objects.active = obj
break
else:
return {'FINISHED'}
_mkName = self.mask_name
#----------------------------------------------CONVERT TO CURVE
obj = context.active_object #save the main Canvas
objRz = obj.rotation_euler[2]
gpencilOPS.convert(type='CURVE', use_timing_data=True)
gpencilOPS.data_unlink()
paintOPS.texture_paint_toggle() #return object mode
lrs = []
for cvP in bpy.data.objects:
if cvP.name.find('GP_Layer') != -1:
if cvP.type == "CURVE":
lrs.append(cvP)
cv = lrs[-1] #select 'GP_Layer'curve
scene.objects.active = cv #active the curve
cv.name = "msk_"+ _mkName #name the curve here
objOPS.editmode_toggle() #return in edit mode
cvOPS.cyclic_toggle() #invert normals
cv.data.dimensions = '2D' #transform line to face
objOPS.editmode_toggle() #return in Object mode
#----------------------------------------DUPLICAT-PARENT 2x curves
context.space_data.layers[19] = True #layer20 temporary visible
obj.select = False
cv.select = True
objOPS.duplicate_move()
cvDupli = context.object
cvDupli.name = 'cvs_' + _mkName
#parent curveDupli to Canvas
cvDupli.select = True
scene.objects.active = obj #select the Canvas
objOPS.parent_set(type='OBJECT', keep_transform=False) #parent Curve to Canvas
objOPS.move_to_layer(layers=(False, False, False, False,\
False, False, False, False, False,\
False, False, False, False, False,\
False, False, False, False, False,\
True)) #move to layer20
context.space_data.layers[19] = False #layer20 stay invisible
cvDupli.select = False
#parent curve to Canvas
cv.select = True
scene.objects.active = obj #select the Canvas
objOPS.parent_set(type='OBJECT', keep_transform=False)#parent curve to Canvas
#------------------------------------------------------MESH MASK UV
scene.objects.active = cv
objOPS.convert(target='MESH') #convert to mesh
scene.objects.active = obj #select the canvas
#init rotation
bpy.ops.transform.rotate(value=-objRz,
axis=(0, 0, 1),
constraint_axis=(False, False, True),
constraint_orientation='GLOBAL')
scene.objects.active = cv #select the Mask
objOPS.editmode_toggle() #return in edit mode
meshOPS.select_all(action='TOGGLE') #select points
meshOPS.dissolve_faces() #dissolve faces
meshOPS.normals_make_consistent(inside=False)#Normals ouside
bpy.ops.uv.project_from_view(camera_bounds=True,
correct_aspect=False,
scale_to_bounds=False)#uv cam unwrap
for mat in bpy.data.materials: #Material and texture
if mat.name == main_canvas[0] : #if mainCanvas Mat exist
cv.data.materials.append(mat) #add main canvas mat
paintOPS.add_texture_paint_slot(type='DIFFUSE_COLOR',
name=cv.name,
width=main_canvas[3],
height=main_canvas[4],
color=(1, 1, 1, 0),
alpha=True,
generated_type='BLANK',
float=False)
break #add a texture
objOPS.editmode_toggle() #return in object mode
scene.objects.active = obj #select the Canvas
#return to rotation state
bpy.ops.transform.rotate(value=objRz,
axis=(0, 0, 1),
constraint_axis=(False, False, True),
constraint_orientation='GLOBAL')
#----------------------------------------------------------------OPTIONS
scene.objects.active = cv #return on the mask
if context.mode != 'PAINT_TEXTURE':
paintOPS.texture_paint_toggle() #return in paint mode
context.object.data.use_paint_mask = True
tool_settings.image_paint.use_occlude = False
tool_settings.image_paint.use_backface_culling = False
tool_settings.image_paint.use_normal_falloff = False
tool_settings.image_paint.seam_bleed = 0
return {'FINISHED'}
#-----------------------------------------------------------CURVE BEZIER TO POLY
class CurvePoly2d(Operator):
"""Curve added and made poly 2d Macro"""
bl_description = "Create 2D Poly Vector Mask"
bl_idname = "ez_draw.curve_2dpoly"
bl_label = "Create 2D Bezier Vector Mask"
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
A = poll_apt(self, context)
B = context.mode == 'PAINT_TEXTURE'
return A and B
curve_name = StringProperty(name="Curve name")
def invoke(self, context, event):
global CURVE_MESSAGE
self.curve_name = CURVE_MESSAGE
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
obj = context.active_object #selected canvas object
objRz = math.degrees(obj.rotation_euler[2])
#Operators
objOPS = bpy.ops.object
cvOPS = bpy.ops.curve
paintOPS = bpy.ops.paint
paintOPS.texture_paint_toggle() #return object mode
bpy.ops.view3d.snap_cursor_to_center() #center the cursor
cvOPS.primitive_bezier_curve_add(rotation=(0, 0, objRz),
layers=(True, False, False, False,
False, False, False, False, False,
False, False, False, False, False,
False, False, False, False, False,
False)) #add curve
cv = context.object #save original curve
objOPS.editmode_toggle() #toggle curve edit
cvOPS.spline_type_set(type= 'POLY') #change to poly spline
cv.data.dimensions = '2D' #change to 2d
cvOPS.delete(type='VERT') #delete vertice
objOPS.editmode_toggle() #return in object mode
context.scene.objects.active = obj #select mainCanvas
objOPS.parent_set(type='OBJECT',
xmirror=False,
keep_transform=False) #parent Mask to canvas
#Name the curve with "+ Mask.xxx" or "+ Mask"(no mask)
context.scene.objects.active = cv #return on the curve
_cvName = self.curve_name
cv.name = "cvs_" + _cvName #name it
objOPS.editmode_toggle() #toggle curve edit
cvOPS.vertex_add() #first: add a vertice
cvOPS.handle_type_set(type='VECTOR')
context.space_data.show_manipulator = True
return {'FINISHED'}
#---------------------------------------------------------CLOSE, MESH AND UNWRAP
class CloseCurveUnwrap(Operator):
"""Close the curve, set to mesh and unwrap"""
bl_description = "Close the curve, set to mesh and unwrap"
bl_idname = "ez_draw.curve_unwrap"
bl_label = ""
bl_options = {'REGISTER','UNDO'}
@classmethod
def poll(self, context):
obj = context.active_object
if obj is not None and obj.name is not None:
A = obj.type == 'CURVE'
B = obj.mode == 'EDIT'
C = obj.name.find('cvs')!=-1
return A and B and C
return False
def execute(self, context):
scene = context.scene
tool_settings = scene.tool_settings
cv = context.active_object #the vector curve
_cvName = cv.name[4:] #type "cvs_xxxxxx"
obj = cv.parent #the main canvas
objRz = obj.rotation_euler[2] #if mainCanvas rotated
#Operators
cvOPS = bpy.ops.curve
objOPS = bpy.ops.object
meshOPS = bpy.ops.mesh
paintOPS = bpy.ops.paint
main_canvas = main_canvas_data(self, context)
#-------------------------------------------------------------------INIT
if main_canvas[0] == '': #if main canvas is erased
return {'FINISHED'}
#------------------------------------------------------------------CURVE
cvOPS.select_all(action='TOGGLE') #Init selection
cvOPS.select_all(action='TOGGLE') #select points
cvOPS.cyclic_toggle() #close spline 'create faces
cv.data.dimensions = '2D' #change the space
objOPS.editmode_toggle() #return to object mode
#---------------------------------------------DUPLICAT-PARENTt 2x curves
obj.select = False
cv.select = True
if cv.layers[0]==False:
objOPS.move_to_layer(layers=(True, False, False, False,
False, False, False, False, False,
False, False, False, False, False,
False, False, False, False, False,
False)) #move to layer1
objOPS.duplicate_move()
cvDupli = context.object
#parent curveDupli to Canvas
cv.select = False
cvDupli.select = True
scene.objects.active = obj #select the Canvas
objOPS.parent_set(type='OBJECT',
keep_transform=True) #parent Curve to Canvas
objOPS.move_to_layer(layers=(False, False, False, False,
False, False, False, False, False,
False, False, False, False, False,
False, False, False, False, False,
True)) #move to layer20
context.space_data.layers[19] = False #layer20 invisible
cvDupli.select = False
#parent curve to Canvas
cv.select = True
scene.objects.active = obj #select the Canvas
objOPS.parent_set(type='OBJECT',
xmirror=False,
keep_transform=True) #parent curve to Canvas
#-------------------------------------------------- NEW MESH MASK
scene.objects.active = cv
objOPS.convert(target='MESH') #convert to mesh
mk = context.object #overwrite cv with new mask
mk.name = "msk_" + _cvName #name mask with curve name
scene.objects.active = obj
#init rotation
bpy.ops.transform.rotate(value=-objRz,
axis=(0, 0, 1),
constraint_orientation='GLOBAL')
scene.objects.active = mk
objOPS.editmode_toggle() #mask in edit mode
meshOPS.select_all(action='TOGGLE') #select all
bpy.ops.mesh.edge_face_add()
meshOPS.normals_make_consistent(inside=False) #Normals outside
bpy.ops.uv.project_from_view(camera_bounds=True,
correct_aspect=False,
scale_to_bounds=False) #uv cam unwrap
for mat in bpy.data.materials:
if mat.name == main_canvas[0] : #if mainCanvas Mat exist
cv.data.materials.append(mat) #add main canvas mat
paintOPS.add_texture_paint_slot(type='DIFFUSE_COLOR',
name=mk.name,
width= main_canvas[3],
height= main_canvas[4],
color=(1, 1, 1, 0),
alpha=True,
generated_type='BLANK',
float=False)
break
objOPS.editmode_toggle() #return in object mode
scene.objects.active = obj #Select the maincanvas
#return to rotation state
bpy.ops.transform.rotate(value=objRz,
axis=(0, 0, 1),
constraint_orientation='GLOBAL')
#------------------------------------------------------ OPTIONS
cvDupli.name = "cvs_" + _cvName
scene.objects.active = mk
if context.mode != 'PAINT_TEXTURE':
paintOPS.texture_paint_toggle() #return in Paint mode
context.object.data.use_paint_mask = True
tool_settings.image_paint.use_occlude = False
tool_settings.image_paint.use_backface_culling = False
tool_settings.image_paint.use_normal_falloff = False
tool_settings.image_paint.seam_bleed = 0
return {'FINISHED'}
#-------------------------------------------Invert all mesh mask
class CurvePolyInvert(Operator):
"""Invert Mesh Mask in Object mode only"""
bl_idname = "ez_draw.inverted_mask"
bl_description = "Invert Mesh Mask in Object mode only"
bl_label = "Inverted mesh Mask"
bl_options = {'REGISTER','UNDO'}