[Edit Linked Library] Fixes based on code review from Campbell
[blender-addons-contrib.git] / oscurart_futurism.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20     "name": "Futurism",
21     "author": "Oscurart",
22     "version": (1, 2),
23     "blender": (2, 63, 0),
24     "location": "Object > Futurism",
25     "description": "Adds a new Mesh Object",
26     "warning": "",
27     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
28         "Scripts/Object/Oscurart_Futurism",
29     "tracker_url": "https://projects.blender.org/tracker/index.php?"\
30                    "unc=detail&aid=31911",
31     "category": "Add Mesh"}
32
33
34 import bpy
35
36 def object_osc_futurism (self, context,STEP, HOLD):
37     ACTOBJ=bpy.context.active_object # OBJETO ACTIVO
38     FS=bpy.context.scene.frame_start # FRAME START
39     FE=bpy.context.scene.frame_end # FRAME END
40     OBJLIST=[] # LISTA PARA OBJETOS ????
41     FC=FS # FRAME CURRENT
42     OBJNUMBER=1 # SUFIJO DE NUMERO PARA OBJETOS
43     STEPINC=0 # NUMERO PARA EVALUAR LOS PASOS        
44     bpy.context.scene.frame_set(FS)  # SETEO EL FRAME CURRENT    
45     OBACT = bpy.context.active_object # SETEO EL OBJETO ACTIVO
46     
47     ## CREO EMPTY
48     bpy.ops.object.add()
49     bpy.context.active_object.name = "FuturismContainer"
50     EMPTY = bpy.context.active_object   
51     
52     # SUMO PARAMETERS AL EMPTY
53     EMPTY["FUTURISM_HOLDIN"] = 0
54     EMPTY["FUTURISM_HOLDOUT"] = 0    
55     
56     bpy.context.scene.objects.active = OBACT  # RECUPERO OBJETO ACTIVO 
57     
58     for OBJETO in range((FE+1)-FS):
59         if STEPINC == STEP:
60             # CREO UN MESH A PARTIR DE OBJETO
61             MESH=ACTOBJ.to_mesh(bpy.context.scene, True, 'PREVIEW')
62             # CREO OBJETO
63             OBJECT=bpy.data.objects.new(ACTOBJ.name[:3]+str(FC), MESH)
64             # CONECTO A LA ESCENA
65             bpy.context.scene.objects.link(OBJECT)
66             # SETEO FRAME CURRENT
67             bpy.context.scene.frame_set(FC)
68             # MARCO EXPRESIONES PARA VIEW
69             OBJECT.driver_add("hide")
70             OBJECT.animation_data.drivers[0].driver.variables.new()
71             OBJECT.animation_data.drivers[0].driver.variables.new()
72             OBJECT.animation_data.drivers[0].driver.variables.new()
73             OBJECT.animation_data.drivers[0].driver.expression= "False if frame >= %s+var_001 and frame <= %s+var_002 else True" % (str(FC),str(FC+HOLD))
74             OBJECT.animation_data.drivers[0].driver.variables[0].targets[0].id_type = 'SCENE'
75             OBJECT.animation_data.drivers[0].driver.variables[0].targets[0].id= bpy.context.scene
76             OBJECT.animation_data.drivers[0].driver.variables[0].targets[0].data_path = "current_frame"
77             OBJECT.animation_data.drivers[0].driver.variables[1].targets[0].id_type = 'OBJECT'
78             OBJECT.animation_data.drivers[0].driver.variables[1].targets[0].id= EMPTY
79             OBJECT.animation_data.drivers[0].driver.variables[1].targets[0].data_path = '["FUTURISM_HOLDIN"]'  
80             OBJECT.animation_data.drivers[0].driver.variables[2].targets[0].id_type = 'OBJECT'
81             OBJECT.animation_data.drivers[0].driver.variables[2].targets[0].id= EMPTY
82             OBJECT.animation_data.drivers[0].driver.variables[2].targets[0].data_path = '["FUTURISM_HOLDOUT"]'     
83             
84             # MARCO EXPRESIONES PARA RENDER           
85             OBJECT.driver_add("hide_render")
86             OBJECT.animation_data.drivers[1].driver.variables.new()
87             OBJECT.animation_data.drivers[1].driver.variables.new()
88             OBJECT.animation_data.drivers[1].driver.variables.new()         
89             OBJECT.animation_data.drivers[1].driver.expression= "False if frame >= %s+5 and frame <= %s else True" % (str(FC),str(FC+HOLD))
90             OBJECT.animation_data.drivers[1].driver.variables[0].targets[0].id_type = 'SCENE'
91             OBJECT.animation_data.drivers[1].driver.variables[0].targets[0].id= bpy.context.scene
92             OBJECT.animation_data.drivers[1].driver.variables[0].targets[0].data_path = "current_frame"   
93             OBJECT.animation_data.drivers[1].driver.variables[1].targets[0].id_type = 'OBJECT'
94             OBJECT.animation_data.drivers[1].driver.variables[1].targets[0].id= EMPTY
95             OBJECT.animation_data.drivers[1].driver.variables[1].targets[0].data_path = '["FUTURISM_HOLDIN"]'  
96             OBJECT.animation_data.drivers[1].driver.variables[2].targets[0].id_type = 'OBJECT'
97             OBJECT.animation_data.drivers[1].driver.variables[2].targets[0].id= EMPTY
98             OBJECT.animation_data.drivers[1].driver.variables[2].targets[0].data_path = '["FUTURISM_HOLDOUT"]'                       
99             # RESETEO STEPINC
100             STEPINC=0
101             # COPIAMOS S R T
102             OBJECT.matrix_world=ACTOBJ.matrix_world
103             #EMPARENTO
104             OBJECT.parent=EMPTY
105         # AVANZO STEP Y FRAME
106         FC+=1
107         STEPINC+=1    
108
109 # CLASE PARA OPERADOR
110 class Oscurart_futurism (bpy.types.Operator):
111     bl_idname = "object.duplicate_futurism"
112     bl_label = "Duplicate Futurism"
113     bl_description = "Duplicate object per frame"
114     bl_options = {'REGISTER', 'UNDO'}
115
116     scale = bpy.props.IntProperty(name='Step',default=1, min=1, max=1000)
117     
118     hold = bpy.props.IntProperty(name='Hold', default=0, min=0)
119
120     @classmethod
121     def poll(cls, context):
122         return(bpy.context.active_object.type == "MESH" ) 
123
124     def execute(self, context):
125         object_osc_futurism(self, context, self.scale, self.hold)
126
127         return {'FINISHED'}
128
129
130 # Registration
131
132 def add_osc_futurism_button(self, context):
133     self.layout.operator(
134         Oscurart_futurism.bl_idname,
135         text="Futurism",
136         icon="PLUGIN")
137
138
139 def register():
140     bpy.utils.register_class(Oscurart_futurism)
141     bpy.types.VIEW3D_MT_object.append(add_osc_futurism_button)
142
143
144 def unregister():
145     bpy.utils.unregister_class(Oscurart_futurism)
146     bpy.types.VIEW3D_MT_object.remove(add_osc_futurism_button)
147
148
149 if __name__ == '__main__':
150     register()