Is it possible to select color attributes in blender with python? Idea is to do the same like clicking on the vertex color in viewport. The goal is to make the colors visible in the viewport.
My current approach looks like this:
# accessing color attributes
test_1 = bpy.data.meshes['Cube'].color_attributes['test_1']
test_2 = bpy.data.meshes['Cube'].color_attributes['test_2']
# try to change selection
bpy.ops.geometry.color_attribute_render_set(name="test_2")
Unfortunately this is not working. Is there an easy approach to solve this? Thanks in advance.
Solved it by creating a material like this:
def create_mat():
mat = bpy.data.materials.new(name="test")
obj.data.materials.append(mat)
mat.use_nodes = True
mat.node_tree.nodes.new(type="ShaderNodeVertexColor")
mat.node_tree.nodes["Color Attribute"].layer_name = "test_1"
input = mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"]
output = mat.node_tree.nodes["Color Attribute"].outputs["Color"]
mat.node_tree.links.new(input, output)
def change_mat():
mat = bpy.data.materials["test"]
mat.node_tree.nodes["Color Attribute"].layer_name = "test_2"
create_mat()
change_mat()
Related
I have a situation, where I decided to distinguish my markers slightly by adding the MarkerCluster to the existing Marker.
The clustering works, although isn't synchronized. It means, that if I switch one layer off, just the first markercluster disappears, whereas the second criterion defined as CircleMarker still appears like shown below.
My code is:
df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="OR",overlay = True)
openreach_cluster = MarkerCluster(name="OR").add_to(map)
openreach_status = MarkerCluster(control=False,
visible=True
).add_to(map)
for i,row in df.iterrows():
lat =df.at[i, 'lat']
lng = df.at[i, 'lng']
sp = df.at[i, 'sp']
stat = df.at[i,'status']
popup = df.at[i,'sp'] +'<br>' + str(df.at[i, 'street']) + '<br>' + str(df.at[i, 'post code']) + '<br>{}'.format(style)
or_marker = folium.Marker(location=[lat,lng], tooltip='<strong>Job details</strong>', popup=popup, icon = folium.Icon(
color='blue', icon='glyphicon-calendar'))
or_stat_marker = folium.CircleMarker(
location=[lat,lng],
radius=10,
color=or_color(stat),
fill_color=or_color(stat),
fill_opacity=0.5)
openreach_cluster.add_child(or_marker)
openreach_status.add_child(or_stat_marker)
Is there any way to combine these markerClusters together or sync them?
UPDATE:
The first approach from the answer below, unfortunately, doubles the jobs up and the user can't see them until clicks on any of them shown below:
This option would be fantastic if the behavior could be the same as in the image above.
UPDATE II:
The second approach is still not what I am looking for, because the clusters are doubled again and after clicking the circlemarker falls almost in the opposite direction as presented in the image above.
I need to have the behaviors exactly like those displayed on the top-left image. The circlemarker should be integrated with the point marker.
If I got you right, I think there are two possibilities:
Use the same cluster
Add your markers from or_stat_marker to the openreach_cluster and not to another cluster to de-/activate them at the same time with the same button
# was
openreach_cluster.add_child(or_marker)
openreach_status.add_child(or_stat_marker)
# try this
openreach_cluster.add_child(or_marker)
openreach_cluster.add_child(or_stat_marker)
Use marker subgroups
In this case you will have three checkmarks, one parent and two childs for each markercluster which gives full control to visibility
UPDATE: If you add the control=False option to the subgroup you will only see the parent group in LayerControl which then show/hide both groups. But the matter of markers "spreading for visibility" still is a problem I think
Another UPDATE: You are able to deactivate the clustering according to the map zoom level or even at all by using disableClusteringAtZoom option (use True or an int number for zoom level). See here for reference
# markergroups in layercontrol
mc = folium.plugins.MarkerCluster(name='OR',
overlay=True,
control=True,
show=True,
disableClusteringAtZoom=15) # choose zoom lvl to your needs
mc.add_to(map)
sub1 = folium.plugins.FeatureGroupSubGroup(mc, name='openreach_cluster', control=False, show=True) # False --> deactivated at start
sub1.add_to(map)
sub2 = folium.plugins.FeatureGroupSubGroup(mc, name='openreach_status', control=False)
sub2.add_to(map)
# the layercontrol itself
lc = folium.map.LayerControl(collapsed=False)
lc.add_to(map)
# ...
for i, row in df.iterrows():
# ...
or_marker = folium.Marker(...)
or_marker.add_to(sub1)
or_stat_marker = folium.CircleMarker(...)
or_stat_marker.add_to(sub2)
My Result:
By following the second approach described I get a map shown here. There is one checkmark "Segment Markers" which shows/hides the markers including the circles. They don't move around when clicked and are fully shown when zoomed in to a specific lvl by using disableClusteringAtZoom.
Sorry this didn't work for your problem, I really don't know why ..
im trying to set a report default background color but it appears black instead of blue, can anyone help?
Why is that? do i need to use another format that is not hex? iv tryied with other format and still nothing.
docpath="/Users/ricardosimoes/Desktop/DESKTOP/Jira/my_word_file.docx"
mydoc = docx.Document()
section_h = mydoc.sections[0]
header = section_h.header
styles = mydoc.styles
style = styles.add_style('Tahoma',WD_STYLE_TYPE.PARAGRAPH)
style.font.name = 'Tahoma'
style.font.size = Pt(11)
shd = OxmlElement('w:background')
# Add attributes to the xml element
shd.set(qn('w:color'), '#0000FF')
shd.set(qn('w:themeColor'), 'text1')
shd.set(qn('w:themeTint'), 'F2')
# Add background element at the start of Document.xml using below
mydoc.element.insert(0, shd)
# Add displayBackgroundShape element to setting.xml
shd1 = OxmlElement('w:displayBackgroundShape')
mydoc.settings.element.insert(0, shd1)
paragraph_h = header.paragraphs[0]
runheader = paragraph_h.add_run()
runheader.add_picture("client_report/report_img/titulo.png", width=docx.shared.Inches(5), height=docx.shared.Inches(1))
mydoc.add_picture("client_report/report_img/bottom.png", width=docx.shared.Inches(5),
height=docx.shared.Inches(1))
mydoc.save(docpath)
The snippet seems to supply all three of the w:color, w:themeColor and w:themeTint attributes, and the latter two override the first one. The ECMA-376 standard says about the w:color attribute:
If the background specifies the use of a theme color via the themeColor attribute, this value is ignored. [Note: Applications are discouraged from specifying both the color and themeColor attributes on the same parent element. end note]
I haven't tested this, but removing themeColor and themeTint (and dropping the # from 0000FF) should cause the blue color to show up.
I'm trying to create and place a series of sketches from a tablet to a blender file. I know where they should go in 3d-space. I am trying to construct a python script to input a file name and coordinates then place thing at location.
From some code I found.. I think this is very close. The script leaves off at allowing the user to draw. Instead I want to import a file.
import bpy
context = bpy.context
space = context.space_data
# Create material for grease pencil
if "Bright Material" in bpy.data.materials.keys():
gp_mat = bpy.data.materials["Bright Material"]
else:
gp_mat = bpy.data.materials.new("Bright Material")
if not gp_mat.is_grease_pencil:
bpy.data.materials.create_gpencil_data(gp_mat)
gp_mat.grease_pencil.color = (1, 0, 0.818649, 1)
# Add grease pencil object
gp_data = bpy.data.grease_pencils.new("Bright Pencil")
gp_ob = bpy.data.objects.new("Bright Pencil", gp_data)
context.scene.collection.objects.link(gp_ob)
if space.local_view:
gp_ob.local_view_set(space, True)
for ob in context.selected_objects:
ob.select_set(False)
gp_ob.select_set(True)
context.view_layer.objects.active = gp_ob
bpy.ops.object.mode_set(mode='PAINT_GPENCIL')
# Assign the material to the grease pencil for drawing
gp_data.materials.append(gp_mat)
next instead of bpy.ops.gpencil.draw(wait_for_input=False)
can I do something like bpy.context.image_paint_object
or bpy.ops.object.load_reference_image
or otherwise import an SVG image and set the rotation of it? can I add a frame number for animation?
I've encountered something very strange when having a function which generates an NdOverlay of Points to a DynamicMap, where the function is tied to panel widgets (I don't think the panel widgets are important).
The below code is a working example which produces the expected behavior. Whenever you change the widget values a new plot is generated with two sets of Points overlaid, with different colors and respective legend entries. Image shown below code.
a_widget = pn.widgets.Select(name='A', options=[1,2,3,4])
b_widget = pn.widgets.IntSlider(name='B', start=10, end=20, value=10)
widget_box = pn.WidgetBox(a_widget, b_widget, align='center')
#pn.depends(a=a_widget.param.value, b=b_widget.param.value)
def get_points(a, b):
return hv.NdOverlay({x: hv.Points(np.random.rand(10,10)) for x in range(1,3)})
points = hv.DynamicMap(get_points)
pn.Row(widget_box, points)
The second example shown below, is meant to demonstrate that in certain situations you might want to just simply return an empty plot and the way that I've done it in this example is done in the same way as in this example: http://holoviews.org/gallery/demos/bokeh/box_draw_roi_editor.html#bokeh-gallery-box-draw-roi-editor
The result of this code is an empty plot as expected when a == 1, but when a has values other than 1, the result is quite strange as illustrated in the image below the code.
The points all have the same color
When changing the slider for instance, some points are frozen and never changes, which is not the case in the above working example.
a_widget = pn.widgets.Select(name='A', options=[1,2,3,4])
b_widget = pn.widgets.IntSlider(name='B', start=10, end=20, value=10)
widget_box = pn.WidgetBox(a_widget, b_widget, align='center')
#pn.depends(a=a_widget.param.value, b=b_widget.param.value)
def get_points(a, b):
if a == 1:
return hv.NdOverlay({None: hv.Points([])})
else:
return hv.NdOverlay({x: hv.Points(np.random.rand(10,10)) for x in range(1,3)})
points = hv.DynamicMap(get_points)
pn.Row(widget_box, points)
While I can not help the observed issue with NdOverlay, creating plots with or without content can be done with the help of Overlay.
As b_widget is never used in your code, I removed it for simplicity.
a_widget = pn.widgets.Select(name='A', options=[1,2,3,4])
widget_box = pn.WidgetBox(a_widget, align='center')
#pn.depends(a=a_widget.param.value)
def get_points(a):
images = []
if a == 3:
images.append(hv.Points(np.random.rand(10,10), label='None'))
else:
for x in range(1,3):
images.append(hv.Points(np.random.rand(10,10), label=str(x)))
return hv.Overlay(images)
points = hv.DynamicMap(get_points)
pn.Row(widget_box, points)
The way how to use NdOverlay that is described in the documentation for NdOverlay is different to your approach, this might be a reason for the observed problems.
Anyway, to narrow down which part of the code is responsible for the observed issue, I removed all code that is not necessary to reproduce it.
For clarity, I renamed the values of a, and I also made sure, that a start value for a is provided.
It turned out while testing the code, that the if-else-statement is neither important, so I removed that too.
And just to make sure, that variables behave like expected, I added some print-statements.
This gives the following minimal reproducable example:
a_widget = pn.widgets.Select(name='A', value='Test', options=['Test','Test1', 'Test2'])
#pn.depends(a=a_widget.param.value)
def get_points(a):
dict_ = {}
dict_[str(a)] = hv.Points(np.random.rand(10,10))
print(dict_)
overlay = hv.NdOverlay(dict_)
print(overlay)
return overlay
points = hv.DynamicMap(get_points)
# using the server approach here to see the outpout of the
# print-statements
app = pn.Row(a_widget, points)
app.app()
When running this code, and choosing the different options in the select widget, it turns out that option Test is not updated, once one of the options Test1 and Test3 have been choosen.
When we change the default value in the first line like this
a_widget = pn.widgets.Select(name='A', value='Test2', options=['Test','Test1', 'Test2'])
now Test2 is not updated correctly.
So it looks like this is an issue of DynamicMap using NdOverlay.
So I suggest you report this issue to the developers (if not already done), either wait for new release or use a different approach (e.g. as shown above).
From this question I learned how to color Python. I figured out all the color codes, don't worry.
Anyway, the answer that worked for me was the ctypes one by orip. It's a bit tiresome to have to have to type ctypes.windll.kernel32.SetConsoleTextAttribute(handle, AQUA) every time I want to color text. Is there a way to convert it into a function? I'm not sure how to send variables through functions, and not sure how to implement them, even if I did.
Thanks in advance! -ghostmancer
All that matters for me is that it works for me - I'm not planning to give my script away.
My colors:
BLACK = 0x0000
BLUE = 0x0001
GREEN = 0x0002
RED = 0x0004
PURPLE = 0x0005
YELLOW = 0x0006
WHITE = 0x0007
GRAY = 0x0008
GREY = 0x0008
AQUA = 0x0009 #Very Blue
ummm... if i understand right ...
def a_func(handle,color):
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
a_func(handle,AQUA)
or even better
colorFunc = ctypes.windll.kernel32.SetConsoleTextAttribute
colorFunc(handle,AQUA)
no need to create a new function with def or lambda, just assign the function with a long name to a shorter name, e.g:
textcolor = ctypes.windll.kernel32.SetConsoleTextAttribute
textcolor(handle, color)
One way is
def textcolor(handle, color):
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
which you call like so:
textcolor(handle, AQUA)
You can use:
f=lambda handle,color:ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
and, call f(<Actual handle object>, <color>) wherever you want.
e.g. f(handle, AQUA) would be the required call
Because I see the variable 'handle' everywhere without being defined and for anyone who wonders, here is a way to get it, as far as stdout is concerned, so that we can used it with ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color):
STD_OUTPUT_HANDLE = -11
handle = ctypes.windll.kernel32.GetStdHandle(-STD_OUTPUT_HANDLE)