2
$\begingroup$

From a script, I create a bunch of circles with bpy.ops.mesh.primitive_circle_add() and I need to name the newly created object. But I get a Type error message at the circle.name line:

TypeError: bpy_struct: item.attr = val: Object.name expected an int type, not str

 def makecircle(uid, id, ring, rad, vert, sthick, origin):
    bpy.ops.mesh.primitive_circle_add(
        radius = rad,
        vertices = vert,
        location = origin,
        rotation = (0,0,1.5707/2))
    circle = bpy.context.object 

    bpy.types.Object.time = bpy.props.FloatProperty(name="Time")
    bpy.types.Object.duration = bpy.props.FloatProperty(name="Duration")
    bpy.types.Object.pause = bpy.props.FloatProperty(name="Pause")
    bpy.types.Object.interval = bpy.props.FloatProperty(name="Interval")
    bpy.types.Object.target_rotation = bpy.props.FloatVectorProperty(name="Target Rotation")
    bpy.types.Object.ring = bpy.props.IntProperty(name="Ring")
    bpy.types.Object.event = bpy.props.IntProperty(name="Event")
    bpy.types.Object.uid = bpy.props.IntProperty(name="Uid")        

    circle.name = "Tile" + str(ring) + "." + str(id)

When all is done I would expect the objects to be named on the pattern "Tile0.0, Tile0.1, ...".

Why won't Blender let me give the objects a name?

$\endgroup$
1
$\begingroup$

This works fine for me with repeated calls to

makecircle(2, 3, 10, 10, 10, 1, (0,0,0))  

I end up with "Tile10.3" and then a whole lot of "Tile10.001" using the blender naming system.

You need define a blender object property once, all the bpy.types.Object.xxxx = IntProperty(...) should be moved to a register method, called once.

The error you are getting is almost certainly that you have defined

bpy.types.Object.name = IntProperty(...)

elsewhere in your code. As long as that code has been run ONCE somewhere while the file is open, the name property of an object is expected to be an Integer and will remain throwing that error if you try and set it to a string. If I run the offensive little bit of code then check the console

>>>C.object.name
0

which isn't really what we want.

import bpy
from bpy.props import IntProperty, FloatProperty, FloatVectorProperty
from math import radians

def makecircle(uid, id, ring, rad, vert, sthick, origin):
    bpy.ops.mesh.primitive_circle_add(
        radius = rad,
        vertices = vert,
        location = origin,
        rotation = (0,0,radians(45)))
    circle = bpy.context.object 
    circle.ring = ring

    circle.name = "Tile" + str(ring) + "." + str(id)

def register():
    bpy.types.Object.time = FloatProperty(name="Time")
    bpy.types.Object.duration = FloatProperty(name="Duration")
    bpy.types.Object.pause = FloatProperty(name="Pause")
    bpy.types.Object.interval = FloatProperty(name="Interval")
    bpy.types.Object.target_rotation = FloatVectorProperty(name="Target Rotation")
    bpy.types.Object.ring = IntProperty(name="Ring")
    bpy.types.Object.event = IntProperty(name="Event")
    bpy.types.Object.uid = IntProperty(name="Uid")  
    # will produce the error you are getting.    
    #bpy.types.Object.name = IntProperty()
register()

makecircle(2,3,10,10,10,1, (0,0,0))
$\endgroup$
  • $\begingroup$ Thanks! But I have not defined this property anywhere else. These lines run once at the beginning of the scene. But I'll split the register section into its own function and see what it does. $\endgroup$ – Michel Dec 17 '15 at 16:28

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.