This code works well. I used it with a point shapefile (Multi Part) for getting its convexHull Polygon.
layer = iface.activeLayer()
feat = layer.selectedFeatures()
geom = feat.geometry()
convexhull = geom.convexHull()
#Extract CRS from layer
CRS = layer.crs().postgisSrid()
URI = "Polygon?crs=epsg:"+str(CRS)+"&field=id:integer""&index=yes"
#Create polygon layer for convexHull
mem_layer = QgsVectorLayer(URI,
#add Map Layer to Registry
#Prepare mem_layer for editing
#Set feature for convexHull
feat2 = QgsFeature()
#Set geometry for convexHull
#set attributes values for convexHull
#stop editing and save changes
Before running the code in the Python Console (selected features in default yellow color):
After running the code:
Convexhull is a memory layer and it can be saved as shapefile in the directory that you prefer or be used to do a buffer in the same script. Automatically, convexhull becomes the active layer.