Retrieve Asset Filename from an entry, inside a matrix field I must simply be blanking this morning, but for whatever reason, this has me stumped... I have a Structure section I use for the different sections of my site which sets titles for each page, etc.

Additionally, I have another section called "Widgets". These widgets are stored in the template folder and each widget is linked up to a dedicated entry in an Asset field. Finally, each Structure entry (take the home page for example) has a Matrix field. This allows me to rapidly add new pages from templates I have already built. Unfortunately this time around I am having trouble pulling the filename for the widgets, though I seem to be able to grab the asset IDs. Following is the code. Thank you in advance for the help; I am sure it is simple but I've been banging my head against my desk for a little too long now...

                {% block contentPrimary %}
                {% switch entry.type %}
                    {% case "mainContentLeft" %}
                        {% for block in entry.contentPrimary %}
                            {% switch block.type %}
                                {% case "text" %}
                                    {{ block.text }}
                                {% case "widget" %}
                                    {% if block.widgetPrimary|length and block.entry[0].widgets|length %}
                                        {{ block.widgetPrimary[0].widgets[0].filename }}
                                    {% endif %} 
                            {% endswitch %}
                        {% endfor %}
                    {% case "mainContentRight" %}   
                        Main Content Right
                    {% case "singleColumn" %}   
                        Single Column
                {% endswitch %}
            {% endblock contentPrimary %}


After trying a few different things, I've seen a couple different errors, but the error I see at present states:


  Craft\MatrixBlockModel and its behaviors do not have a method or closure named "widgetPrimary".


Thank you again, in advance!
I would first check if you actually set up the Field handles you are currently using in your template:

Matrix Field handle: 'contentPrimary'
Block Type handle: 'widget'
Entries Field handle: 'widgetPrimary'
Assets Field handle: 'widgets'  

Then try to use the first() method instead of direct access to the array.
Or use direct access only after adding find() method that creates an array out of your ElementCriteriaModel. Edit: Direct access like this: myArray[0] should also work with an ElementCriteriaModel, but throws errors if no first elements exists (→ read Brandon's answer on this topic for more info).

{% for block in entry.contentPrimary %}
    {% switch block.type %}
        {% case "widget" %}
            {% set relatedEntry = block.widgetPrimary.first() %}
            {% if relatedEntry %}
                {% set asset = relatedEntry.widgets.first() %}
                {% if asset %}
                    {{ asset.title }}
                {% endif %}
            {% endif %}
    {% endswitch %}
{% endfor %}