python 3.x - Set position according part of an objectname -
i'm trying script in blender3d using python. i've got bunch of objects in scene , want translate them using numerical part of objectname.
first of collect objects scene matching part of name.
root_obj = [obj obj in scene.objects if fnmatch.fnmatchcase(obj.name, "*_root")]
this gives me list with:[bpy.data.objects['01_root'],bpy.data.objects['02_root'],bpy.data.objects['03_root'],bpy.data.objects['00_root']]
my goal move these objects 15x corresponding part of name. '00_root' doesnt have move, '01_root' has move 15 blender units , '02_root' 30 blender units.
how exctract numberpart of names , use them translation values.
i'm pretty newb python appreciate can get.
a string list of characters, each character can accessed index starting 0, first character name[0]
, second name[1]
. list can use slicing portion of list. if value first 2 characters can value name[:2]
can them turn integer int()
or float float()
. combined becomes,
val = int(name[:2])
you have number can calculate new location with.
obj.location.x = val * 15
if number of digits in name might vary can use split()
break string on specific separating character. returns list of items between specified character, if want first item turn integer.
name = '02_item' val = int(name.split('_')[0])
using split allows multiple values in name.
name = '2_12_item' val1 = int(name.split('_')[0]) val2 = int(name.split('_')[1])
Comments
Post a Comment