搅拌机随机主题

时间:2014-07-30 23:52:17

标签: python xml themes blender

下面写的是用于形成随机十六进制字符串的脚本。手头的问题是它在调用时始终打印相同的结果。 Hex标记不会定期更改randinteger指示。 如何解决?

import bpy
from random import randint

a=["a","b","c","d","e","f","1","2","3","4","5","6","7","8","9","0"]
b=["a","b","c","d","e","f","1","2","3","4","5","6","7","8","9","0"]
c=["a","b","c","d","e","f","1","2","3","4","5","6","7","8","9","0"]
d=["a","b","c","d","e","f","1","2","3","4","5","6","7","8","9","0"]
e=["a","b","c","d","e","f","1","2","3","4","5","6","7","8","9","0"]
f=["a","b","c","d","e","f","1","2","3","4","5","6","7","8","9","0"]


hex='"'+"#"+str(a[randint(0,15)])+str(b[randint(0,15)])+str(c[randint(0,15)])+str(d[randint(0,15)])+str(e[randint(0,15)])+str(f[randint(0,15)])+'"'

def xmlScript():
    global hex
    print("<bpy>")
    print("  <Theme>")
    print("    <view_3d>")
    print("      <ThemeView3D object_active="+hex)   
    print("                   editmesh_active="+hex)
    print("                   act_spline="+hex)
    print("                   handle_align="+hex)
    print("                   handle_sel_align="+hex)
    print("                   handle_auto="+hex)

xmlScript()

1 个答案:

答案 0 :(得分:1)

这是一种生成随机24位数字的有趣的方法。当然问题是你在模块级别生成hex一次,而不是在需要时生成。

尝试这样的事情:

from random import randint

def rand_24bit():
     return '"#{:x}"'.format(randint(0,16**6-1))

这使用string formatting将随机24位整数格式化为十六进制。演示:

rand_24bit()
Out[22]: '"#118656"'

rand_24bit()
Out[23]: '"#abb6c4"'

rand_24bit()
Out[24]: '"#1c2e5c"'