美文网首页
blender 2.8 python

blender 2.8 python

作者: 皿卜土 | 来源:发表于2019-05-18 12:14 被阅读0次

小代码块

# link other collection under collection's children
bpy.data.collections["Collection1"].children.link(bpy.data.collections["Collection2"])


# Create new collection
collection = bpy.data.collections.new(name="MyCollection")

# Add collection to scene
scene.collection.children.link(collection)

# Create collection instance in scene
object = bpy.data.objects.new(name="MyObject", object_data=None)
object.instance_collection = collection
scene.collection.objects.link(object)


# unlink all the child collections from the scene's master collection.
import bpy
context = bpy.context
scene = context.scene

for c in scene.collection.children:
    scene.collection.children.unlink(c)

# Running this after removes the orphan collection
for c in bpy.data.collections:
    if not c.users:
        bpy.data.collections.remove(c)

# remove the collections objects from blender file with
for o in c.objects:
    bpy.data.objects.remove(o)

    

# get an active object
obj = bpy.context.object

# create new collection
newCol = bpy.data.collections.new('Yammy')
#.... or find the existing collection
newCol = bpy.data.collections['Yammy']

# link the newCol to the scene
bpy.context.scene.collection.children.link(newCol)

# link the object to collection
newCol.objects.link(obj)
# ... or link through bpy.data
bpy.data.collections['Yammy'].objects.link(obj)

创建mesh

import bpy

verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]

mymesh = D.meshes.new("Plane")
myobject = D.objects.new("Plane", mymesh)

D.collections[0].objects.link(myobject)

mymesh.from_pydata(verts,[],[])
mymesh.update()

#根据物体中心创建mesh
import bpy

D = bpy.data
locs = []
name = "loctionsA"
exist = False

for i in D.meshes:
    if(i.name == name):
        exist = True
        print("Exist")
        break

if(exist == False):
    objs = D.collections['temp'].objects
    for i in objs:
        locs.append(i.location[:])
        

    mymesh = mymesh = D.meshes.new(name)
    myobject = D.objects.new(name, mymesh)

    D.collections[0].objects.link(myobject)

    mymesh.from_pydata(locs,[],[])
    mymesh.update()
    print("Create")
>>> v
Vector((1.0,2.0,3.0))
>>>v[:]
(1.0,2.0,3.0)

删除数据块

import bpy

# Select objects by type
for o in bpy.context.scene.objects:
    if o.type == 'MESH':
        o.select_set(True)
    else:
        o.select_set(False)

# Call the operator only once
bpy.ops.object.delete()

# Save and re-open the file to clean up the data blocks
bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)


#或者
for block in bpy.data.meshes:
    if block.users == 0:
        bpy.data.meshes.remove(block)

for block in bpy.data.materials:
    if block.users == 0:
        bpy.data.materials.remove(block)

for block in bpy.data.textures:
    if block.users == 0:
        bpy.data.textures.remove(block)

for block in bpy.data.images:
    if block.users == 0:
        bpy.data.images.remove(block)

曲线的长度是通过选择曲线运行此脚本得到的:

import bpy
import bmesh
orig = bpy.context.active_object.name
bpy.ops.object.duplicate_move()
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
bpy.ops.object.convert(target='MESH', keep_original=False)
bm = bmesh.new()
bm.from_mesh(bpy.context.active_object.data)
edge_length = 0
for edge in bm.edges:
    edge_length += edge.calc_length()
bpy.context.window_manager.clipboard = str(round(edge_length,3))
bpy.ops.object.delete()
bpy.context.scene.objects.active = bpy.context.scene.objects[orig]
bpy.context.object.select = True

#驱动程序表达式为:-2 * curve_length / diameter *(eval_time / eval_max):

重命名

import bpy

wcol = bpy.data.collections.get("Water")
if wcol:
    for i, o in enumerate(sorted(wcol.objects[:], key=lambda o: o.name)):
        o.name = "Water%d" % (i + 1) # to start from 1
import bpy

wcol = bpy.data.collections.get("Water")
if wcol:
    for i, o in enumerate(wcol.objects):
        o.name = "Water%d" % i
for obj in bpy.context.scene.objects:
    if obj.type == 'MESH' and obj.name.lower().startswith("c")
        obj.name = "newName"
        obj.data.name = "newName"

相关文章

网友评论

      本文标题:blender 2.8 python

      本文链接:https://www.haomeiwen.com/subject/ozqjaqtx.html