How to use environment variables in blender

六月ゝ 毕业季﹏ 提交于 2021-01-28 15:31:12

问题


I added a enviroment variable writing in the ~/.bashrc file this two line

var="stuff.." 
export var

using the python interpreter in a normal terminal this two lines of code works

import os
print(os.environ['var'])

but in a blender python console it generate a KeyError so printing os.environ list i can see that there isn't a item with 'var' as key

So i think that is a problem with the environment settings in unix system. Can anyone help me and explain how export the environment variables for the other processes? Thanks and sorry for the english


回答1:


The .bachrc file (and similar such as .cshrc) is read when your shell is started, similarly when you start a GUI desktop the shell rc files are read at the time of it starting and the variables at that time are then part of the environment passed onto any GUI apps, changes made while running do not get read in as you start a new app. You can find ways of setting environment variables for different desktops.

One way of passing environment variables into blender is to start it from a terminal window. The rc files will be read when you open the terminal, you can also manually set environment variables before starting blender.

Another way to set environment variables for blender is to start it from a script, this may be one called myblender that will be found in your $PATH or it can also be named blender if it will be found before the real blender. In this script you can set variables before starting blender and any changes will be in effect when you run it.

#!/bin/bash

var="stuff.." 
export var

exec /usr/local/bin/blender "$@"



回答2:


After updating ~/.bashrc you either have to source ~/.bashrc in the terminal where you launch blender or log out and log back in to your system, where the variable should then be in the environment.

If you need to get environment variables that may or may not be available, you can also do something like os.getenv('var', 'default value')



来源:https://stackoverflow.com/questions/46025259/how-to-use-environment-variables-in-blender

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!