blender scripting 编写

这篇具有很好参考价值的文章主要介绍了blender scripting 编写。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、查看ui按钮对应的代码

blender scripting 编写,blender

二、查看或修改对象名称

blender scripting 编写,blender

三、案例:渲染多张图片并导出对应的相机参数

注:通过ui交互都设置好,如果ui能渲染,该代码就能运行成功。文章来源地址https://www.toymoban.com/news/detail-762303.html

import bpy
import math
import os
from mathutils import Vector, Matrix
from math import radians
import bmesh
import datetime
import time
import numpy as np
import os
#from os import path

scenenumber='baseline_scene6_clean'

#basefolder=os.path.join(os.path.expanduser('~'), 'Documents', 'Zantis', scenenumber)
scene = bpy.data.scenes["Scene"]
#scene.camera.rotation_mode='YZX'
def length():
    leg=0.00
    p1=[0.0,0.0]
    temp=0
    ob = bpy.context.object # active object
    for p in ob.data.splines.active.bezier_points:
        p2=p1
        p1=[p.co.x,p.co.y]
        if(temp==0):
            temp=1
            continue
        distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )
        leg=leg+distance
    return leg

item=0
multiply=40
camera = bpy.data.objects['Camera']
target=bpy.data.objects['mesh.001']
my_obj=[]


#my_obj.append("019")
#my_obj.append("021")
#my_obj.append("029")
#my_obj.append("030")
#my_obj.append("031")
#my_obj.append("032")
#my_obj.append("033")
#my_obj.append("034")
my_obj.append("040")
#my_obj.append("042")

t1 = time.time()

#import bpy

#for ob in bpy.context.selected_editable_objects:
  #  ob.active_material_index = 0
 #   for i in range(len(ob.material_slots)):
#        bpy.ops.object.material_slot_remove({'object': ob})

for item in my_obj:
    #break
    # basefolder=os.path.join(os.path.expanduser('~'), 'Desktop', 'Urban_Enviroment_Texturing','Mesh_Texturing_Pipeline', 'Scenes', '005', 'images')
    basefolder = os.path.join('E:\\', 'chromeDownload', 'mvs_test', 'scenes', '006', 'images')
    t2 = time.time()
    for ob in bpy.context.selected_objects:
        ob.select_set(False)
    curv='Curve.'+item
    path = bpy.data.objects[curv]
    camera.select_set(True)
    path.select_set(True)
    # bpy.context.view_layer.objects.active = path # parent
    # bpy.ops.object.parent_set(type='FOLLOW') # follow path
    # camera.location=path.matrix_world @ bpy.context.object.data.splines.active.bezier_points[0].co
    # target.location=path.matrix_world @ bpy.context.object.data.splines.active.bezier_points[1].co
    # direc=target.location-camera.location
    frames=(int)(length()*multiply)	
    # bpy.data.scenes["Scene"].frame_end=frames
    # bpy.context.object.data.path_duration=frames
    frame_is=0
    frames=frames+1
    print(frames)
    count=0
    for frame_is in range(scene.frame_start, scene.frame_end + 1):
    # for frame_is in range(frames - 3):
        t3 = time.time()
        scene.frame_current=frame_is
        #print(scene.frame_currents
#        s=str(scene.objects['Camera'].matrix_world[0][3])+"_"+str(scene.objects['Camera'].matrix_world[1][3])+"_"+str(scene.objects['Camera'].matrix_world[2][3])+"_"
#        s=s+str(direc[0])+"_"+str(direc[1])+"_"+str(direc[2])+".jpg"
        s = str(scene.frame_current).zfill(5)+".jpg"
        image_filepath=os.path.join(basefolder,s)
        #print(s)
        bpy.data.scenes['Scene'].render.filepath = image_filepath
#        bpy.ops.render.render(write_still=True)
        elapsedTime = time.time() - t3
        print("Frame time passed {hours:d}:{minutes:d}:{seconds:d}"
        .format(hours=int((elapsedTime / 60 ** 2) % (60 ** 2)), minutes=int((elapsedTime / 60) % (60)),
            seconds=int(elapsedTime % 60)))
        
        
        
#        print('path to create: ', os.path.join(os.path.expanduser('~'), 'Desktop', 'Data','Depth_data',scenenumber+'_'+item))
        if not os.path.exists(os.path.join(os.path.expanduser('~'), basefolder)):
            os.mkdir(os.path.join(os.path.expanduser('~'), basefolder))
        
        if not os.path.exists(os.path.join(os.path.expanduser('~'), basefolder, 'RTm/')):
            os.mkdir(os.path.join(os.path.expanduser('~'), basefolder, 'RTm/'))
           
        # Set up rendering of depth map:
        bpy.context.scene.use_nodes = True
        tree = bpy.context.scene.node_tree
        links = tree.links

        # clear default nodes
        for n in tree.nodes:
            tree.nodes.remove(n)

        # create input render layer node
        rl = tree.nodes.new('CompositorNodeRLayers')
        
        RGB2BW = tree.nodes.new(type="CompositorNodeRGBToBW")
        links.new(rl.outputs[0], RGB2BW.inputs[0])
        
        map = tree.nodes.new(type="CompositorNodeMapValue")
        # Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
        map.size = [0.05]
        map.use_min = True
        map.min = [0]
        map.use_max = True
        map.max = [1]
        links.new(rl.outputs[2], map.inputs[0])
        
        mix_multi = tree.nodes.new(type="CompositorNodeMixRGB")
        # Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
        mix_multi.blend_type = 'MULTIPLY'
        mix_multi.inputs[2].default_value[0] = 0.5
        mix_multi.inputs[2].default_value[1] = 0.5
        mix_multi.inputs[2].default_value[2] = 0.5
#        mix_multi.inputs[2].default = [0.5 , 0.5, 0.5, 1.0]
        links.new(rl.outputs[3], mix_multi.inputs[1])
        
        mix_multi2 = tree.nodes.new(type="CompositorNodeMixRGB")
        # Size is chosen kind of arbitrarily, try out until you're satisfied with resulting depth map.
        mix_multi2.blend_type = 'ADD'
        mix_multi2.inputs[2].default_value[0] = 0.5
        mix_multi2.inputs[2].default_value[1] = 0.5
        mix_multi2.inputs[2].default_value[2] = 0.5
        links.new(mix_multi.outputs[0], mix_multi2.inputs[1])

        invert = tree.nodes.new(type="CompositorNodeInvert")
        links.new(map.outputs[0], invert.inputs[1])

        # The viewer can come in handy for inspecting the results in the GUI
        depthViewer = tree.nodes.new(type="CompositorNodeViewer")
        links.new(invert.outputs[0], depthViewer.inputs[0])
        # Use alpha from input.
        links.new(rl.outputs[1], depthViewer.inputs[1])
        # Normal map
        # The viewer can come in handy for inspecting the results in the GUI
        depthViewer2 = tree.nodes.new(type="CompositorNodeViewer")
        links.new(rl.outputs[1], depthViewer2.inputs[1])
        links.new(mix_multi2.outputs[0], depthViewer2.inputs[0])
        
#        fileOutput = tree.nodes.new(type="CompositorNodeOutputFile")
#        fileOutput.base_path = os.path.join(os.path.expanduser('~'), 'Desktop', 'Data','Depth_data',scenenumber+'_'+item, 'depth')
##        fileOutput.file_slots[0].path = str(scene.frame_current).zfill(5)
#        links.new(invert.outputs[0], fileOutput.inputs[0])
#        
#        fileOutput2 = tree.nodes.new(type="CompositorNodeOutputFile")
#        fileOutput2.base_path = os.path.join(os.path.expanduser('~'), 'Desktop', 'Data','Depth_data',scenenumber+'_'+item,'normal')
##        fileOutput2.file_slots[0].path = str(scene.frame_current).zfill(5)
#        links.new(mix_multi2.outputs[0], fileOutput2.inputs[0])
#        
#        fileOutput3 = tree.nodes.new(type="CompositorNodeOutputFile")
#        fileOutput3.base_path = os.path.join(os.path.expanduser('~'), 'Desktop', 'Data','Depth_data',scenenumber+'_'+item,'panoramas_BW')
##        fileOutput3.file_slots[0].path = str(scene.frame_current).zfill(5)
#        links.new(RGB2BW.outputs[0], fileOutput3.inputs[0])

        bpy.ops.render.render(write_still=True)
        
        location, rotation = scene.objects['Camera'].matrix_world.decompose()[0:2]
        R_world2bcam = rotation.to_matrix().transposed()
        T_world2bcam = -1*R_world2bcam @ location
        
        Camera_RT_matrix =  Matrix((
        R_world2bcam[0][:] + (T_world2bcam[0],),
        R_world2bcam[1][:] + (T_world2bcam[1],),
        R_world2bcam[2][:] + (T_world2bcam[2],),
        [0,0,0,1],))
        
        np.savetxt(os.path.join(os.path.expanduser('~'), basefolder, 'RTm')+'/'+str(frame_is).zfill(6)+"_RTm.txt",np.array(Camera_RT_matrix))
        # break
        
    scene.frame_current=0
    path.select_set(False)
    bpy.ops.object.parent_clear(type='CLEAR')
    # camera.select_set(False)
elapsedTime = time.time() - t1
print("Total time passed {hours:d}:{minutes:d}:{seconds:d}"
    .format(hours=int((elapsedTime / 60 ** 2) % (60 ** 2)), minutes=int((elapsedTime / 60) % (60)),
            seconds=int(elapsedTime % 60)))
print("Finished")

到了这里,关于blender scripting 编写的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【blender渲染】blender流体模拟基础

    各位新年好哇,最近在做demo的时候,为了更好的效果,开始摸索一点离线渲染的东西。像这种后续渲染的处理,由于3ds max是更偏向于建模的dcc,有点不那么好使(没有说看不起vray的意思哈)。 像在实时渲染里面,即便是通过高分辨率的mesh来做水体,也常有很多限制。毕竟

    2024年01月19日
    浏览(30)
  • 【blender】blender mac 快捷键备注

    外接 键盘 我最近就是 很多键按不出来 直接外接了一个 因为mac还没有数字键盘 有时候视图都不好调 一定记住 买常规的 别买mac或者蓝牙那种便携的 会和电脑一样按不出 [1.1]物体对象相关操作 添加物体:Shift+A 全选物体: A 框选物体: B 反选物体: Ctrl+I 删除物体: X 重复上一步操

    2024年02月10日
    浏览(27)
  • Blender Game Engine (BGE) 是 Blender 3D内置游戏引擎

    Blender Game Engine (BGE) 是 Blender 3D 建模和动画软件的一个内置游戏引擎。它可以创建游戏,模拟和交互式内容。 安装: Blender Game Engine 已经随 Blender 软件一起发布,因此只需下载并安装 Blender 即可使用 BGE。您可以在Blender官网下载最新版本。 使用: 打开 Blender 软件。 在左侧面

    2024年02月14日
    浏览(37)
  • Blender 所有常用快捷键速查表:掌握 Blender 键盘快捷键

    Blender 是一款功能强大的开源 3D 和 2D 动画程序或计算机图形软件,任何人都可以免费使用。 无论您是动画师、建模师、VFX 艺术家还是游戏开发者,Blender 都是最佳选择。2000 年,Blender 2.0 推出。 二十年后,Blender 3.0 到来,标志着开源 2D 和 3D 内容创作时代的新篇章。 无论您

    2024年02月08日
    浏览(43)
  • Blender个人使用记录【导出FBX格式】【导入Unity中出现白模问题(Blender向)】

    以下说明仅用于记录个人使用Blender和Unity时遇到的问题及其解决方法(并不一定适用所有人),通过B站、CSDN、知乎等地方搜索了一系列有关解决模型导入Unity出现白模的问题,发现不少对新手而言讲解的不明白不清楚的,亦或是不实用的。故将自己遇到的问题记录下来以供参

    2024年02月01日
    浏览(54)
  • Lumeical Script------Script Prompt 中的两种输出方式

    有时候,和众多编程语言一样,我们需要在 Script Prompt 中打印一些我们已经得到的数据,这样可以方便我们调试代码和查看代码中是否有错误。关于在 Script Prompt 中打印数据,Lumerical Script 提供了两种方法。 如果大家觉得有用,就请点个赞吧~

    2024年02月03日
    浏览(25)
  • UE4/5:通过Blender制作BlendShape导入【UE4/5曲线、变形目标,blender形态键】

    UE4/5里面,我们经常可以在一些骨骼模型上面看到相关的曲线,如Metahuman里面就是通过这个曲线来改变人物的脸部表情。 而这里笔者将教导如何去制作这种曲线。 这种曲线都是存在于骨骼模型上的,所以我们要么直接制作骨骼模型导入ue,要么做一个普通的模型,然后导入

    2024年02月03日
    浏览(49)
  • blender UV基础

    1 选中一个物体,选择最上方UVEditing即可进入UV界面 2 注意进入UV界面后,舞台也会自动进入编辑模式 3 只有在编辑模式里才能操作UV 4 选择一个面来单独查看这个面的UV 5 进入UV编辑界面后,左右两个窗口上方都会有UV按钮,但这两个按钮并不相同 6 所有的自带物体都是展好U

    2024年02月16日
    浏览(39)
  • Blender里复制对象动画

    假设在Blender里有2个对象,其中一个添加了动画,另外一个没有添加动画,那么如何把已有的动画拷贝到没有动画的对象上呢? 分为2步: 先选中没有动画的对象,再按shift键选中有动画的对象,此时2个对象同时被选中,选中顺序不能错 按Ctrl+L,在弹出的框里选择Link Animati

    2024年02月10日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包