Python bpy.context() Examples

The following are 30 code examples of bpy.context(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bpy , or try the search function .
Example #1
Source File: ui.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
        ob = data
        slot = item
        col = layout.row(align=True)
        if item.name not in ["NO ACTION","Restpose"]:
            col.label(icon="ACTION")
            col.prop(item,"name",emboss=False,text="")
        elif item.name == "NO ACTION":
            col.label(icon="RESTRICT_SELECT_ON")    
            col.label(text=item.name)
        elif item.name == "Restpose":
            col.label(icon="ARMATURE_DATA")    
            col.label(text=item.name)
        
        if context.scene.coa_nla_mode == "NLA" and item.name not in ["NO ACTION","Restpose"]:    
            col = layout.row(align=False)
            op = col.operator("coa_operator.create_nla_track",icon="NLA",text="")
            op.anim_collection_name = item.name

### Custom template_list look for event lists 
Example #2
Source File: DynamicTensionMap.py    From Modeling-Cloth with MIT License 6 votes vote down vote up
def get_coords(ob=None, proxy=False):
    '''Creates an N x 3 numpy array of vertex coords. If proxy is used the
    coords are taken from the object specified with modifiers evaluated.
    For the proxy argument put in the object: get_coords(ob, proxy_ob)'''
    if ob is None:
        ob = bpy.context.object
    if proxy:
        mesh = proxy.to_mesh(bpy.context.scene, True, 'PREVIEW')
        verts = mesh.vertices
    else:
        verts = ob.data.vertices
    v_count = len(verts)
    coords = np.zeros(v_count * 3, dtype=np.float32)
    verts.foreach_get("co", coords)
    if proxy:
        bpy.data.meshes.remove(mesh)
    return coords.reshape(v_count, 3) 
Example #3
Source File: __init__.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.alignment = "LEFT"
        row.prop(self,"enable_spritesheets",text="")
        row.prop(self,"enable_spritesheets",icon="ERROR",emboss=False)
        #row.label(text="",icon="ERROR")
        layout.prop(self, "show_donate_icon")
        layout.prop(self,"json_export")
        layout.prop(self,"dragon_bones_export")
        layout.prop(self,"sprite_import_export_scale")
        layout.prop(self,"sprite_thumb_size")
        layout.prop(self,"alpha_update_frequency")
        
        
        addon_updater_ops.update_settings_ui(self,context) 
Example #4
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def change_slot_mesh_data(context,obj):
    if len(obj.coa_slot) > 0:
        slot_len = len(obj.coa_slot)-1
        obj["coa_slot_index"] = min(obj.coa_slot_index,max(0,len(obj.coa_slot)-1))
        
        idx = max(min(obj.coa_slot_index,len(obj.coa_slot)-1),0)
        
        slot = obj.coa_slot[idx]
        obj = slot.id_data
        obj.data = slot.mesh
        set_alpha(obj,context,obj.coa_alpha)
        for slot2 in obj.coa_slot:
            if slot != slot2:
                slot2["active"] = False
            else:
                slot2["active"] = True 
        if "coa_base_sprite" in obj.modifiers:
            if slot.mesh.coa_hide_base_sprite:
                obj.modifiers["coa_base_sprite"].show_render = True
                obj.modifiers["coa_base_sprite"].show_viewport = True
            else:
                obj.modifiers["coa_base_sprite"].show_render = False
                obj.modifiers["coa_base_sprite"].show_viewport = False 
Example #5
Source File: DynamicTensionMap.py    From Modeling-Cloth with MIT License 6 votes vote down vote up
def dynamic_tension_handler(scene):
    stretch = bpy.context.scene.dynamic_tension_map_max_stretch / 100
    items = data.items()
    if len(items) == 0:
        for i in bpy.data.objects:
            i.dynamic_tension_map_on = False
        
        for i in bpy.app.handlers.scene_update_post:
            if i.__name__ == 'dynamic_tension_handler':
                bpy.app.handlers.scene_update_post.remove(i)
    
    for i, value in items:    
        if i in bpy.data.objects:
            update(ob=bpy.data.objects[i], max_stretch=stretch, bleed=0.2)   
        else:
            del(data[i])
            break 
Example #6
Source File: DynamicTensionMap.py    From Modeling-Cloth with MIT License 6 votes vote down vote up
def reassign_mats(ob=None, type=None):
    '''Resets materials based on stored face indices'''
    if ob is None:
        ob = bpy.context.object
    if type == 'off':
        ob.data.polygons.foreach_set('material_index', data[ob.name]['mat_index'])
        ob.data.update()
        idx = ob.data.materials.find(data[ob.name]['material'].name)        
        if idx != -1:    
            ob.data.materials.pop(idx, update_data=True)
        bpy.data.materials.remove(data[ob.name]['material'])
        del(data[ob.name])    
    if type == 'on':
        idx = ob.data.materials.find(data[ob.name]['material'].name)
        mat_idx = np.zeros(len(ob.data.polygons), dtype=np.int32) + idx
        ob.data.polygons.foreach_set('material_index', mat_idx)
        ob.data.update() 
Example #7
Source File: DynamicTensionMap.py    From Modeling-Cloth with MIT License 6 votes vote down vote up
def get_bmesh(ob=None):
    '''Returns a bmesh. Works either in edit or object mode.
    ob can be either an object or a mesh.'''
    obm = bmesh.new()
    if ob is None:
        mesh = bpy.context.object.data
    if 'data' in dir(ob):
        mesh = ob.data
        if ob.mode == 'OBJECT':
            obm.from_mesh(mesh)
        elif ob.mode == 'EDIT':
            obm = bmesh.from_edit_mesh(mesh)    
    else:
        mesh = ob
        obm.from_mesh(mesh)
    return obm 
Example #8
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def set_modulate_color(obj,context,color):
    r_engine = context.scene.render.engine
    if r_engine in ["BLENDER_INTERNAL","BLENDER_GAME"]:
        if obj.type == "MESH":
            if not obj.material_slots[0].material.use_object_color:
                obj.material_slots[0].material.use_object_color = True
            obj.color[:3] = color
    elif r_engine in ["CYCLES"]:
        if obj.type == "MESH":
            node_color_modulate = None
            node_tree = obj.active_material.node_tree
            if node_tree != None:
                for node in node_tree.nodes:
                    if "coa_modulate_color" in node:
                        node_color_modulate = node
                        break
            if node_color_modulate != None:
                node_color_modulate.outputs[0].default_value[:3] = color 
Example #9
Source File: DynamicTensionMap.py    From Modeling-Cloth with MIT License 6 votes vote down vote up
def get_key_coords(ob=None, key='Basis', proxy=False):
    '''Creates an N x 3 numpy array of vertex coords.
    from shape keys'''
    if key is None:
        return get_coords(ob)
    if proxy:
        mesh = proxy.to_mesh(bpy.context.scene, True, 'PREVIEW')
        verts = mesh.data.shape_keys.key_blocks[key].data
    else:
        verts = ob.data.shape_keys.key_blocks[key].data
    v_count = len(verts)
    coords = np.zeros(v_count * 3, dtype=np.float32)
    verts.foreach_get("co", coords)
    if proxy:
        bpy.data.meshes.remove(mesh)
    return coords.reshape(v_count, 3) 
Example #10
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def set_uv_default_coords(context,obj):
    uv_coords = obj.data.uv_layers[obj.data.uv_layers.active.name].data
    ### add uv items
    for i in range(len(uv_coords)-len(obj.coa_uv_default_state)):
        item = obj.coa_uv_default_state.add()
    ### remove unneeded uv items    
    if len(uv_coords) < len(obj.coa_uv_default_state):
        for i in range(len(obj.coa_uv_default_state) - len(uv_coords)):
            obj.coa_uv_default_state.remove(0)

    ### set default uv coords
    frame_size = Vector((1 / obj.coa_tiles_x,1 / obj.coa_tiles_y))
    pos_x = frame_size.x * (obj.coa_sprite_frame % obj.coa_tiles_x)
    pos_y = frame_size.y *  -int(int(obj.coa_sprite_frame) / int(obj.coa_tiles_x))
    frame = Vector((pos_x,pos_y))
    offset = Vector((0,1-(1/obj.coa_tiles_y)))
    
    
    for i,coord in enumerate(uv_coords):    
        uv_vec_x = (coord.uv[0] - frame[0]) * obj.coa_tiles_x 
        uv_vec_y = (coord.uv[1] - offset[1] - frame[1]) * obj.coa_tiles_y
        uv_vec = Vector((uv_vec_x,uv_vec_y)) 
        obj.coa_uv_default_state[i].uv = uv_vec 
Example #11
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def set_view(screen,mode):
    if mode == "2D":
        active_space_data = bpy.context.space_data
        if active_space_data != None:
            if hasattr(active_space_data,"region_3d"):
                region_3d = active_space_data.region_3d
                bpy.ops.view3d.viewnumpad(type='FRONT')
                if region_3d.view_perspective != "ORTHO":
                    bpy.ops.view3d.view_persportho()  
    elif mode == "3D":
        active_space_data = bpy.context.space_data
        if active_space_data != None:
            if hasattr(active_space_data,"region_3d"):
                region_3d = active_space_data.region_3d
                if region_3d.view_perspective == "ORTHO":
                    bpy.ops.view3d.view_persportho() 
Example #12
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def create_armature(context):
    obj = bpy.context.active_object
    sprite_object = get_sprite_object(obj)
    armature = get_armature(sprite_object)
    
    if armature != None:
        context.scene.objects.active = armature
        armature.select = True
        return armature
    else:
        amt = bpy.data.armatures.new("Armature")
        armature = bpy.data.objects.new("Armature",amt)
        armature.parent = sprite_object
        context.scene.objects.link(armature)
        context.scene.objects.active = armature
        armature.select = True
        amt.draw_type = "BBONE"
        return armature 
Example #13
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def create_action(context,item=None,obj=None):
    sprite_object = get_sprite_object(context.active_object)
    
    if len(sprite_object.coa_anim_collections) < 3:
        bpy.ops.coa_tools.add_animation_collection()
    
    if item == None:
        item = sprite_object.coa_anim_collections[sprite_object.coa_anim_collections_index]
    if obj == None:
        obj = context.active_object
        
    action_name = item.name + "_" + obj.name
    
    if action_name not in bpy.data.actions:
        action = bpy.data.actions.new(action_name)
    else:
        action = bpy.data.actions[action_name]
        
    action.use_fake_user = True
    if obj.animation_data == None:
        obj.animation_data_create()
    obj.animation_data.action = action
    context.scene.update() 
Example #14
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def check_region(context,event):
    in_view_3d = False
    if context.area != None:
        if context.area.type == "VIEW_3D":
            t_panel = context.area.regions[1]
            n_panel = context.area.regions[3]
            
            view_3d_region_x = Vector((context.area.x + t_panel.width, context.area.x + context.area.width - n_panel.width))
            view_3d_region_y = Vector((context.region.y, context.region.y+context.region.height))
            
            if event.mouse_x > view_3d_region_x[0] and event.mouse_x < view_3d_region_x[1] and event.mouse_y > view_3d_region_y[0] and event.mouse_y < view_3d_region_y[1]:
                in_view_3d = True
            else:
                in_view_3d = False
        else:
            in_view_3d = False
    return in_view_3d 
Example #15
Source File: ui.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def draw(self, context):
        #addon_updater_ops.check_for_update_background()
        
        layout = self.layout
        
        if get_addon_prefs(context).show_donate_icon:
            row = layout.row()
            row.alignment = "CENTER"
            
            pcoll = preview_collections["main"]
            donate_icon = pcoll["donate_icon"]
            twitter_icon = pcoll["twitter_icon"]
            row.operator("coa_operator.coa_donate",text="Show Your Love",icon_value=donate_icon.icon_id,emboss=True)
            row = layout.row()
            row.alignment = "CENTER"
            row.scale_x = 1.75
            op = row.operator("coa_operator.coa_tweet",text="Tweet",icon_value=twitter_icon.icon_id,emboss=True)
            op.link = "https://www.youtube.com/ndee85"
            op.text = "Check out CutoutAnimation Tools Addon for Blender by Andreas Esau."
            op.hashtags = "b3d,coatools"
            op.via = "ndee85"
        
        addon_updater_ops.update_notice_box_ui(self, context) 
Example #16
Source File: functions.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def remove_base_mesh(obj):
    bpy.ops.object.mode_set(mode="EDIT")
    bm = bmesh.from_edit_mesh(obj.data)
    bm.verts.ensure_lookup_table()
    verts = []
        
    if "coa_base_sprite" in obj.vertex_groups:
        v_group_idx = obj.vertex_groups["coa_base_sprite"].index
        for i,vert in enumerate(obj.data.vertices):
            for g in vert.groups:
                if g.group == v_group_idx:
                    verts.append(bm.verts[i])
                    break

    bmesh.ops.delete(bm,geom=verts,context=1)
    bm = bmesh.update_edit_mesh(obj.data) 
    bpy.ops.object.mode_set(mode="OBJECT") 
Example #17
Source File: ui.py    From coa_tools with GNU General Public License v3.0 6 votes vote down vote up
def change_weight_mode(self,context,mode):
        if self.sprite_object.coa_edit_weights:
            armature = get_armature(self.sprite_object)
            armature.select = True
            bpy.ops.view3d.localview()
            bpy.context.space_data.viewport_shade = 'TEXTURED'

            ### zoom to selected mesh/sprite
            for obj in bpy.context.selected_objects:
                obj.select = False
            obj = bpy.data.objects[context.active_object.name]    
            obj.select = True
            context.scene.objects.active = obj
            bpy.ops.view3d.view_selected()
            
            ### set uv image
            set_uv_image(obj)
        
            
            bpy.ops.object.mode_set(mode=mode) 
Example #18
Source File: __init__.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def register_keymaps():
    addon = bpy.context.window_manager.keyconfigs.addon
    km = addon.keymaps.new(name = "3D View", space_type = "VIEW_3D")
    # insert keymap items here
    kmi = km.keymap_items.new("wm.call_menu_pie", type = "F", value = "PRESS")
    kmi.properties.name = "view3d.coa_pie_menu"
    addon_keymaps.append(km) 
Example #19
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def set_nla_mode(self,context):
        sprite_object = get_sprite_object(context.active_object)
        children = get_children(context,sprite_object,ob_list=[])
        if self.coa_nla_mode == "NLA":
            for child in children:
                if child.animation_data != None:
                    child.animation_data.action = None
            context.scene.frame_start = context.scene.coa_frame_start
            context.scene.frame_end = context.scene.coa_frame_end        
            
            for child in children:
                if child.animation_data != None:
                    for track in child.animation_data.nla_tracks:
                        track.mute = False
        else:
            if len(sprite_object.coa_anim_collections) > 0:
                anim_collection = sprite_object.coa_anim_collections[sprite_object.coa_anim_collections_index]
                context.scene.frame_start = anim_collection.frame_start
                context.scene.frame_end = anim_collection.frame_end
                set_action(context)            
                for obj in context.visible_objects:
                    if obj.type == "MESH" and "coa_sprite" in obj:
                        update_uv(context,obj)
                        set_alpha(obj,bpy.context,obj.coa_alpha)
                        set_z_value(context,obj,obj.coa_z_value)
                        set_modulate_color(obj,context,obj.coa_modulate_color)
                for child in children:
                    if child.animation_data != None:
                        for track in child.animation_data.nla_tracks:
                            track.mute = True
        
        bpy.ops.coa_tools.toggle_animation_area(mode="UPDATE") 
Example #20
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def set_actions(self,context):
        scene = context.scene
        sprite_object = get_sprite_object(context.active_object)
        
        if context.scene.coa_nla_mode == "ACTION":
            scene.frame_start = sprite_object.coa_anim_collections[sprite_object.coa_anim_collections_index].frame_start
            scene.frame_end = sprite_object.coa_anim_collections[sprite_object.coa_anim_collections_index].frame_end
            set_action(context)
        for obj in context.visible_objects:
            if obj.type == "MESH" and "coa_sprite" in obj:
                update_uv(context,obj)
                set_alpha(obj,bpy.context,obj.coa_alpha)
                set_z_value(context,obj,obj.coa_z_value)
                set_modulate_color(obj,context,obj.coa_modulate_color)
        
        ### set export name
        if scene.coa_nla_mode == "ACTION":
            action_name = sprite_object.coa_anim_collections[sprite_object.coa_anim_collections_index].name    
            if action_name in ["Restpose","NO ACTION"]:
                action_name = ""
            else:
                action_name += "_"
            path = context.scene.render.filepath.replace("\\","/")
            dirpath = path[:path.rfind("/")]
            final_path = dirpath + "/" + action_name
            context.scene.render.filepath = final_path 
Example #21
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def invoke(self,context,event):
        self.sprite_object = get_sprite_object(context.active_object)
        self.armature = get_armature(self.sprite_object)
        
        if self.sprite_object.coa_edit_mesh:
            obj = bpy.data.objects[self.ob_name]
            obj.hide = False
        
        if self.sprite_object != None:
            self.change_weight_mode(context,"OBJECT")
        
        if self.sprite_object != None:
            if context.active_object != None and context.active_object.type == "ARMATURE" and context.active_object.mode == "EDIT" and event.alt:
                obj = bpy.data.objects[self.ob_name]
                if obj.type == "MESH":
                    set_weights(self,context,obj)
                    msg = '"'+self.ob_name+'"' + " has been bound to selected Bones."
                    self.report({'INFO'},msg)
                else:
                    self.report({'WARNING'},"Can only bind Sprite Meshes to Bones")
                return{"FINISHED"}
            
            if event.shift and not self.sprite_object.coa_edit_weights:
                self.shift_select_child(context,event)
            if not self.sprite_object.coa_edit_weights or ( self.sprite_object.coa_edit_weights and bpy.data.objects[self.ob_name].type == "MESH"):
                if not event.ctrl and not event.shift:
                    if self.mode == "bone":
                        for bone in bpy.data.objects[self.ob_name].data.bones:
                            bone.select = False
                            bone.select_head = False
                            bone.select_tail = False
                if not event.shift:
                    self.select_child(context,event)
                    
            if self.sprite_object.coa_edit_weights:     
                create_armature_parent(context)
            self.change_weight_mode(context,"WEIGHT_PAINT")    
        else:
            self.shift_select_child(context,event)
                        
        return{'FINISHED'} 
Example #22
Source File: functions.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def get_children(context,obj,ob_list=[]):
    if obj != None:
        for child in obj.children:
            ob_list.append(child)
            if len(child.children) > 0:
                get_children(context,child,ob_list)
    return ob_list 
Example #23
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def change_object_mode(self,context):
        obj = context.active_object
        if obj != None and self.ob_name != obj.name:
            if obj.mode == "EDIT" and obj.type == "MESH":
                bpy.ops.object.mode_set(mode="OBJECT")
            elif obj.mode == "EDIT" and obj.type == "ARMATURE":
                bpy.ops.object.mode_set(mode="POSE") 
Example #24
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
        ob = data
        slot = item
        # col = layout.column(align=False)
        box = layout.box()
        col = box.column(align=False)

        row = col.row(align=False)
        # row.label(text="", icon="TIME")
        if item.collapsed:
            row.prop(item,"collapsed",emboss=False, text="", icon="TRIA_RIGHT")
        else:
            row.prop(item, "collapsed", emboss=False, text="", icon="TRIA_DOWN")
        row.prop(item, "frame", emboss=True, text="Frame")
        op = row.operator("coa_tools.remove_timeline_event", text="", icon="PANEL_CLOSE", emboss=False)
        op.index = index


        # row = col.row(align=True)
        if not item.collapsed:
            row = col.row(align=True)
            # row.alignment = "RIGHT"
            op = row.operator("coa_tools.add_event", icon="ZOOMIN", text="Add new Event", emboss=True)
            op.index = index
            for i, event in enumerate(item.event):
                row = col.row(align=True)
                row.prop(event, "type",text="")
                row.prop(event, "value",text="")
                op = row.operator("coa_tools.remove_event", icon="PANEL_CLOSE", text="", emboss=True)
                op.index = index
                op.event_index = i
        
        


######################################################################################################################################### Select Child Operator 
Example #25
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def update_filter(self,context):
        pass 
Example #26
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def update_stroke_distance(self,context):
        mult = bpy.context.space_data.region_3d.view_distance*.05
        if self.coa_distance_constraint:
            context.scene.coa_distance /= mult
        else:
            context.scene.coa_distance *= mult 
Example #27
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def lock_view(self,context):
        screen = context.screen
        if "-nonnormal" in self.name:
            bpy.data.screens[context.screen.name.split("-nonnormal")[0]].coa_view = self.coa_view
        if screen.coa_view == "3D":
            set_middle_mouse_move(False)
            set_view(screen,"3D")
        elif screen.coa_view == "2D":
            set_middle_mouse_move(True)
            set_view(screen,"2D") 
Example #28
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def select_shapekey(self,context):
        if self.data.shape_keys != None:
            self.active_shape_key_index = int(self.coa_selected_shapekey) 
Example #29
Source File: functions.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def set_z_value(context,obj,z):
    scale = get_addon_prefs(context).sprite_import_export_scale
    obj.location[1] = -z  * scale 
Example #30
Source File: ui.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def get_shapekeys(self,context):
        SHAPEKEYS = []
        obj = context.active_object
        if obj.data.shape_keys != None:
            for i,shape in enumerate(obj.data.shape_keys.key_blocks):
                SHAPEKEYS.append((str(i),shape.name,shape.name,"SHAPEKEY_DATA",i))
        return SHAPEKEYS