I've used the following lines in a very similar situation.
yeah we could smell it that's why we left. Are you kidding me? I don't want to be around that shit I hate how it smells.
Back then though cops were not automatically allowed to search you for a weapon during ANY encounter like they seem to be able to do now. Trying to sell that BS in your situation would have probably gotten you more jammed up than you ended up. What happened after you gave up your rights and emptied your pockets? The cop felt the outsides of your pockets really well right? Had you missed pulling something out and the cop felt it she would then have cause to go into your pockets independent of any "I was checking for weapons" claims.
The problem with the question "Was this search legal?" is even if any of us bongside lawyers gives you the right answer is none of us is the SINGLE judge who would actually make that legal decision. One guy in a robe with some waked complex that makes him bang on his desk with a wooden hammer gets to make that call. Unless you have some damn good evidence you can physically hit him in the face with chances are he believes whatever the cop tells him.
I feel VERY strongly that we all need to stand our ground when it comes to our rights but you have to pick your battles. I have had a state trooper KNOW what he was feeling in my front jeans pocket was a 1/4 oz and a pipe and with him BARKING at me "WHATS IN YOUR POCKET? WHAT IS THIS?" I did two things. Refused to answer his questions as to what was in my pocket, "I have no idea officer I have a lot of stuff in there, keys, change...I don't know what you're feeling" and two I refused to allow him permission to search my person. I DID however consent to his searching my car.
I always advocated keeping anything you don't want found ON YOUR PERSON. I'm not so sure that's still the best way to go but hear me out.
Yes keeping anything you don't want found on your person ties it directly to you that's 100% correct but if LEO finds anything stashed in other property belonging to you it's yours. Argue all you want, claim you don't know how it got there...whatever none of it works. Unless someone else tells them it's theirs and the cops believe them it's your contraband. Your property does not always have the same protection from rights as you do meaning there are times when LEO can search your property even though they may not have probable cause to search you. He smells pot coming from the car and searches it. Anything he finds he's going to put on you OR he's going to pressure you all till someone cracks and cops to it so it's pointless worrying about "if he finds it ON me he knows its mine". If he finds it you're probably screwed anyway and this way you can make it quite clear you do NOT consent to a search of your person so that if the cop IS in the wrong maybe you have a case to get it tossed. It also prevents something like the cop finding it near where someone else was sitting and that person being threatened with being charged unless they talk.
The is ONE person you can trust in life and that's YOU. Dipshits might still get you pinched but the less put them into situations where they can be squeezed the better.
Also remember that anything in plain sight, sitting out in the open where anyone could see it is fair game. So anything you toss to get rid of or you homer simpsoned and forgot about...if it was in your pocket, he would have needed a real reason to go in there and find it. If you toss something and they find it they don't care they will charge you all with possession if nobody owns up...even if they didn't see you toss it. God forbid you're ever standing around when a cop just happens to find contraband.
Keeping it on you and then defending your right not to be searched is the best defense you have. Don't try to stop a cop from searching you and NEVER TOUCH a cop. Even an inadvertent gesture that makes contact with an officer can be construed as an assault. Other than looking the officer directly in the eye and making it crystal clear verbally that you do not consent to his search of your person or property KEEP YOUR MOUTH SHUT! Don't even answer yes or no questions, any word out of your mouth will be used as an admission of guilt or if proven that you lied to the officer further charges could be brought and they WILL try to use this against YOU to get you give up what you know about others.