Bug Fix in drivers
[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, 6, 3),
24     "location": "Object > Futurism",
25     "description": "Adds a new Mesh Object",
26     "warning": "",
27     "wiki_url": "",
28     "tracker_url": "",
29     "category": "Add Mesh"}
30
31
32 import bpy
33
34 def object_osc_futurism (self, context,STEP, HOLD):
35     ACTOBJ=bpy.context.active_object # OBJETO ACTIVO
36     FS=bpy.context.scene.frame_start # FRAME START
37     FE=bpy.context.scene.frame_end # FRAME END
38     OBJLIST=[] # LISTA PARA OBJETOS ????
39     FC=FS # FRAME CURRENT
40     OBJNUMBER=1 # SUFIJO DE NUMERO PARA OBJETOS
41     STEPINC=0 # NUMERO PARA EVALUAR LOS PASOS        
42     bpy.context.scene.frame_set(FS)  # SETEO EL FRAME CURRENT    
43     OBACT = bpy.context.active_object # SETEO EL OBJETO ACTIVO
44     
45     ## CREO EMPTY
46     bpy.ops.object.add()
47     bpy.context.active_object.name = "FuturismContainer"
48     EMPTY = bpy.context.active_object   
49     
50     # SUMO PARAMETERS AL EMPTY
51     EMPTY["FUTURISM_HOLDIN"] = 0
52     EMPTY["FUTURISM_HOLDOUT"] = 0    
53     
54     bpy.context.scene.objects.active = OBACT  # RECUPERO OBJETO ACTIVO 
55     
56     for OBJETO in range((FE+1)-FS):
57         if STEPINC == STEP:
58             # CREO UN MESH A PARTIR DE OBJETO
59             MESH=ACTOBJ.to_mesh(bpy.context.scene, True, 'PREVIEW')
60             # CREO OBJETO
61             OBJECT=bpy.data.objects.new(ACTOBJ.name[:3]+str(FC), MESH)
62             # CONECTO A LA ESCENA
63             bpy.context.scene.objects.link(OBJECT)
64             # SETEO FRAME CURRENT
65             bpy.context.scene.frame_set(FC)
66             # MARCO EXPRESIONES PARA VIEW
67             OBJECT.driver_add("hide")
68             OBJECT.animation_data.drivers[0].driver.variables.new()
69             OBJECT.animation_data.drivers[0].driver.variables.new()
70             OBJECT.animation_data.drivers[0].driver.variables.new()
71             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))
72             OBJECT.animation_data.drivers[0].driver.variables[0].targets[0].id_type = 'SCENE'
73             OBJECT.animation_data.drivers[0].driver.variables[0].targets[0].id= bpy.context.scene
74             OBJECT.animation_data.drivers[0].driver.variables[0].targets[0].data_path = "current_frame"
75             OBJECT.animation_data.drivers[0].driver.variables[1].targets[0].id_type = 'OBJECT'
76             OBJECT.animation_data.drivers[0].driver.variables[1].targets[0].id= EMPTY
77             OBJECT.animation_data.drivers[0].driver.variables[1].targets[0].data_path = '["FUTURISM_HOLDIN"]'  
78             OBJECT.animation_data.drivers[0].driver.variables[2].targets[0].id_type = 'OBJECT'
79             OBJECT.animation_data.drivers[0].driver.variables[2].targets[0].id= EMPTY
80             OBJECT.animation_data.drivers[0].driver.variables[2].targets[0].data_path = '["FUTURISM_HOLDOUT"]'     
81             
82             # MARCO EXPRESIONES PARA RENDER           
83             OBJECT.driver_add("hide_render")
84             OBJECT.animation_data.drivers[1].driver.variables.new()
85             OBJECT.animation_data.drivers[1].driver.variables.new()
86             OBJECT.animation_data.drivers[1].driver.variables.new()         
87             OBJECT.animation_data.drivers[1].driver.expression= "False if frame >= %s+5 and frame <= %s else True" % (str(FC),str(FC+HOLD))
88             OBJECT.animation_data.drivers[1].driver.variables[0].targets[0].id_type = 'SCENE'
89             OBJECT.animation_data.drivers[1].driver.variables[0].targets[0].id= bpy.context.scene
90             OBJECT.animation_data.drivers[1].driver.variables[0].targets[0].data_path = "current_frame"   
91             OBJECT.animation_data.drivers[1].driver.variables[1].targets[0].id_type = 'OBJECT'
92             OBJECT.animation_data.drivers[1].driver.variables[1].targets[0].id= EMPTY
93             OBJECT.animation_data.drivers[1].driver.variables[1].targets[0].data_path = '["FUTURISM_HOLDIN"]'  
94             OBJECT.animation_data.drivers[1].driver.variables[2].targets[0].id_type = 'OBJECT'
95             OBJECT.animation_data.drivers[1].driver.variables[2].targets[0].id= EMPTY
96             OBJECT.animation_data.drivers[1].driver.variables[2].targets[0].data_path = '["FUTURISM_HOLDOUT"]'                       
97             # RESETEO STEPINC
98             STEPINC=0
99             # COPIAMOS S R T
100             OBJECT.matrix_world=ACTOBJ.matrix_world
101             #EMPARENTO
102             OBJECT.parent=EMPTY
103         # AVANZO STEP Y FRAME
104         FC+=1
105         STEPINC+=1    
106
107 # CLASE PARA OPERADOR
108 class Oscurart_futurism (bpy.types.Operator):
109     bl_idname = "object.duplicate_futurism"
110     bl_label = "Duplicate Futurism"
111     bl_description = "Duplicate object per frame"
112     bl_options = {'REGISTER', 'UNDO'}
113
114     scale = bpy.props.IntProperty(name='Step',default=1, min=1, max=1000)
115     
116     hold = bpy.props.IntProperty(name='Hold', default=0, min=0)
117
118     @classmethod
119     def poll(cls, context):
120         return(bpy.context.active_object.type == "MESH" ) 
121
122     def execute(self, context):
123         object_osc_futurism(self, context, self.scale, self.hold)
124
125         return {'FINISHED'}
126
127
128 # Registration
129
130 def add_osc_futurism_button(self, context):
131     self.layout.operator(
132         Oscurart_futurism.bl_idname,
133         text="Futurism",
134         icon="PLUGIN")
135
136
137 def register():
138     bpy.utils.register_class(Oscurart_futurism)
139     bpy.types.VIEW3D_MT_object.append(add_osc_futurism_button)
140
141
142 def unregister():
143     bpy.utils.unregister_class(Oscurart_futurism)
144     bpy.types.VIEW3D_MT_object.remove(add_osc_futurism_button)
145
146
147 if __name__ == '__main__':
148     register()