2
$\begingroup$

My Blender version is 2.76. I used the python command

bpy.ops.mesh.primitive_cone_add(vertices=16, radius1=R1, radius2=R2, depth=L)

to create a truncated cone. In order to animate my scene, I need to increase or decrease the top radius (radius2) of the cone. How can I set its value from within my python script?

$\endgroup$
4
  • $\begingroup$ Do you need to identify and select the rings via python, or just change their diameter? I mean does everything need to be automated via python from object creation to top ring animation? Or can anything be done manually? $\endgroup$ – Todd McIntosh Jun 13 '16 at 21:23
  • $\begingroup$ Do you want to edit the parameter itself after creating the object? or scaling the top vertices would work for you? $\endgroup$ – Georges D Jun 13 '16 at 21:56
  • $\begingroup$ Thanks, Todd McIntosh and Georges, for your questions. I do not know what you mean with "rings". I use the truncated cone as a whole mesh, and I want to vary its top radius2 between 0 and radius1 so that the truncated cone changes its shape between a pure cone and a cylinder $\endgroup$ – Klaus Vielhaber Jun 14 '16 at 15:30
  • $\begingroup$ Given your new snippet of info re cone to cylinder edited answer. $\endgroup$ – batFINGER Jun 14 '16 at 19:09
1
$\begingroup$

Cylinder to Cone via shape key.

Here is a take on this using shape keys, Can't use 0 as the radius on the cone tip as it will not be able to be joined as shapes.

import bpy
context = bpy.context
scene = context.scene 
# no active object
scene.objects.active = None
R1 = 4
R2 = 4
L = 10
# create cone
bpy.ops.mesh.primitive_cone_add(vertices=16,
                                radius1=R1,
                                radius2=R2,
                                depth=L)
cone1 = scene.objects.active                                
# create opposite cone
bpy.ops.mesh.primitive_cone_add(vertices=16,
                                radius1=0.001, # can't use 0
                                radius2=R2,
                                depth=L)
cone1.select = True                                
# join as shapes
bpy.ops.object.join_shapes()
# remove cone1
scene.objects.unlink(cone1)
bpy.data.objects.remove(cone1)

Change the R1, R2 for the second cone to suit your needs. Altering the shapekey value will give cone at 0.0, and cylinder at 1.0

context.object.data.shape_keys.key_blocks['Cone.001'].value = 1.0

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks, batFINGER, for your proposal. I tested it and found, that only "Cone.001" is remaining in the Scene. I do not see any influence of cone1 on the result. My intention however is to maintain the identity (name, location, rotation etc) of an existing truncated cone (cone1 in your example) and just to vary its smaller radius. $\endgroup$ – Klaus Vielhaber Jun 14 '16 at 19:42
  • $\begingroup$ Moving the shapekey slider from 0 to 1 changes the mesh from cone to cylinder. This value can be animated or changed via a script. The shapekey panel, as illustrated in answer is on the mesh (data) properties panel... hmmm perhaps I need to make an animate gif. $\endgroup$ – batFINGER Jun 14 '16 at 19:53
  • $\begingroup$ Correcton: Thanks, batFINGER, for your proposal. I tested it and found that only "Cone.001" is remaining in the Scene. I do not see any influence of cone1 on the result. My intention however is to maintain the identity (name, location, rotation etc) of an existing truncated cone (cone1 in your example) and just to vary its smaller radius. Can you give me an additional advice? If I also use your last line of code (context.object.data.shape_keys ...), Python reports a KeyError: 'bpy_prop_collection[key]: key "Cone.001" not found'. Should I add another line of Python code to avoid this error? $\endgroup$ – Klaus Vielhaber Jun 14 '16 at 20:06
  • $\begingroup$ The shape key may be called "Cone", I prob. had another cone in scene when testing. I'll upload a blend file with the value animated.. $\endgroup$ – batFINGER Jun 14 '16 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.