I used to own a very sweet but rather grumpy cat named Bacon. Like most cats he felt the house was his and he went wherever he wanted. He had a habit of walking on me and my husband while we were in bed. However I noticed that once my husband was sick he would jump over my husband's belly rather than walk on him. He was a cat but somehow he just knew my husband's stomach was tender and he should not walk on him. Animals just know when we aren't well.
If you are having surgery you may want to consider how to manage your pets during your recovery. Recently I was helping a client come home after surgery. Her dogs came to the door excited and immediately began jumping when they saw her. Since she had had abdominal surgery it was critical that the dogs did not jump up and put their paws on her belly. I immediately pulled the dogs away from her and put them outside until she was settled in bed. Fortunately I was able to easily wrangle them away from her but it was clear they really wanted to be by her side. They were just following their animal instinct to be by their owner. The problem is they could have inadvertently done real harm to her if I had not been there to get them away.
Even for minor surgeries you may want to consider having your pets boarded or stay with a friend during the first days of your post surgery recovery. It may be less upsetting for them to be away. Alternatively you may be able to plan to have your pet kept in a separate room, at least at first. Remember that it will likely be difficult for you to take care of yourself so taking care of another living creature could be too much. Plan to have someone set up to walk your dog and feed your pets for the first few days even if you expect your recovery to be quick.
Our four legged friends love us. They know when something is wrong. Unfortunately after surgery they may sometimes get in the way and cause headaches in an attempt to show their love and affection. So it's best to create a plan for their care while you are recovering. It's best for them and for you.