我正在尝试对计数器的使用进行一些分组,这样它会相应地增加..如果我的标题给出了错误的印象,我很抱歉......
无论如何,当我有2个或更多选择时,当我执行我的代码时,而不是看到命名的组,如 - TEST_1_GRP,TEST_2_GRP,TEST_3_GRP等,我得到TEST_1_GRP,TEST_1_GRP1,TEST_1_GRP2作为我的结果......
我尝试在计数器部分周围放置一些线(比如句子内/之前),我无处可去......
以下是我的代码:
def fix_shapes():
all_geos = cmds.ls(sl = True)
for geo in all_geos:
shapes = cmds.listRelatives(geo, fullPath=True, shapes=True)
if len(shapes) == 1:
continue
new_listing = []
listing.append(shapes[:1])
# pop out the first shape, since we don't have to fix it
multi_shapes = shapes[1:]
for multi_shape in multi_shapes:
new_transform = cmds.duplicate(multi_shape, parentOnly=True)
new_geos = cmds.parent(multi_shape, new_transform, addObject=True, shape=True)
listing.append(new_geos)
# remove the shape from its original transform
cmds.parent(multi_shape, removeObject=True, shape=True)
# counter to 'version' up new_geos group naming
counter = 0
new_group_name = cmds.group(em=True, name = 'TEST_' + str(counter + 1) + '_GRP')
for item in new_listing:
counter = counter + 1
new_geos_parent_name = cmds.listRelatives(item, parent = True)
cmds.parent(new_geos_parent_name, new_group_name)
答案 0 :(得分:0)
按如下方式移动您的计数器,您没有在最后一个循环中使用计数器,因此将其从中删除。检查这可以做你想要的吗?
def fix_shapes():
all_geos = cmds.ls(sl = True)
counter = 0
for geo in all_geos:
shapes = cmds.listRelatives(geo, fullPath=True, shapes=True)
if len(shapes) == 1:
continue
new_listing = []
listing.append(shapes[:1])
# pop out the first shape, since we don't have to fix it
multi_shapes = shapes[1:]
for multi_shape in multi_shapes:
new_transform = cmds.duplicate(multi_shape, parentOnly=True)
new_geos = cmds.parent(multi_shape, new_transform, addObject=True, shape=True)
listing.append(new_geos)
# remove the shape from its original transform
cmds.parent(multi_shape, removeObject=True, shape=True)
# counter to 'version' up new_geos group naming
new_group_name = cmds.group(em=True, name = 'TEST_' + str(counter + 1) + '_GRP')
for item in new_listing:
new_geos_parent_name = cmds.listRelatives(item, parent = True)
cmds.parent(new_geos_parent_name, new_group_name)
答案 1 :(得分:0)
我相信这可以解决你的问题。请记住,第一次执行代码时,它将按预期工作并生成名为error_log('UserName: ' . $username);
的组等。但是,第二次将其命名为TEST_1_GRP TEST_2_GRP
等因为您没有保存或检查名称是否存在。要解决此问题,您可以存储全局变量,在执行期间检查名称,或将计数值存储在场景文件或其他位置。我会留给你的。
TEST_1_GRP1 TEST_2_GRP1
我用###标记了这些更改并附带了一些注释。
答案 2 :(得分:0)
def fix_shapes():
all_geos = cmds.ls(sl = True)
counter = 0
for geo in all_geos:
shapes = cmds.listRelatives(geo, fullPath=True, shapes=True)
if len(shapes) == 1:
continue
new_listing = []
new_listing.append(shapes[:1])
# pop out the first shape, since we don't have to fix it
multi_shapes = shapes[1:]
for multi_shape in multi_shapes:
new_transform = cmds.duplicate(multi_shape, parentOnly=True)
new_geos = cmds.parent(multi_shape, new_transform, addObject=True, shape=True)
new_listing.append(new_geos)
# remove the shape from its original transform
cmds.parent(multi_shape, removeObject=True, shape=True)
# counter to 'version' up new_geos group naming
new_grp_id = "TEST_{0}_GRP".format(counter)
if not cmds.objExists(new_grp_id):
new_group_name = cmds.group(em=True, name = new_grp_id)
counter += 1
else:
for o in reversed(xrange(100)):
if cmds.objExists("TEST_{0}_GRP".format(o)):
counter = o + 1
break
new_group_name = cmds.group(em=True, name = "TEST_{0}_GRP".format(counter))
counter += 1
for item in new_listing:
new_geos_parent_name = cmds.listRelatives(item, parent = True)
cmds.parent(new_geos_parent_name, new_group_name)
对于第二次迭代,你需要像max range counter这样的东西, 所以我们已经构建了一些检查....所以现在它会创建grps并向其中添加项目,通过下一次迭代,它将找到“TEST_n_GRP”的最大值并增加它,认为它会找到最大的int,而不是整数之间,例如:在场景:grp_1和grp_3,下一个最大的数字是grp_4,但它找不到grp_2!你可以删除反向运算符。 使用反向运算符,您会发现最大/最后整数非常快,但不会发现整数之间