I created cylinders using the bpy.ops.mesh.primitive_cylinder_add() command (I used this example). How can I update their radius (or any other property) using python script, instead of deleting the objects and recreate them? I'm using those cylinders to display connection between areas in the brain, as part of an addon I'm implementing. I want to let the user to pick the radius of the cylinders (for rendering purposes), or to automatically change them according to the connections' strength (which may vary).
An example using a custom property with an update method.
import bpy from bpy.props import FloatProperty context = bpy.context scene = context.scene def bond_strength(self, context): self.scale.x = self.scale.y = self.bond_strength return None # create a custom property on all objects bpy.types.Object.bond_strength = FloatProperty(name="Strength", default = 1.0, precision = 4, update=bond_strength) # adds a slider to UI object properties panel. # would be in a custom Panel if True: # only need to run this once def show_strength(self, context): self.layout.prop(context.object, "bond_strength") bpy.types.OBJECT_PT_context_object.remove(show_strength) # put in unregister bpy.types.OBJECT_PT_context_object.append(show_strength) # add a primitive bpy.ops.mesh.primitive_cylinder_add(radius=1.0) #create more cyl = context.active_object # newly created cylinder for i in range(4): cyl = cyl.copy() scene.objects.link(cyl) cyl.bond_strength *= 0.5 cyl.location.x += 2
You can set the scale of any of the XYZ axis in Python which can effectively change the radius.
You can also consider shape keys.
Since any mesh object can be edited we do have a worst case scenario. Near Worse case scenario might be all vertices deleted except 1 vertex.